forked from mandiant/gocrack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
52 lines (42 loc) · 1.15 KB
/
server_test.go
File metadata and controls
52 lines (42 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package web
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func init() {
// Supress the gin out while testing
gin.SetMode(gin.ReleaseMode)
}
func TestWrapAPIForError(t *testing.T) {
e := gin.New()
e.GET("/test", WrapAPIForError(func(c *gin.Context) *WebAPIError {
c.String(http.StatusOK, "Hello World")
return nil
}))
e.GET("/broken", WrapAPIForError(func(c *gin.Context) *WebAPIError {
return &WebAPIError{
StatusCode: http.StatusInternalServerError,
Err: errors.New("i am an error"),
CanErrorBeShownToUser: true,
}
}))
req, _ := http.NewRequest("GET", "/test", nil)
w := httptest.NewRecorder()
e.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "Hello World", w.Body.String())
req, _ = http.NewRequest("GET", "/broken", nil)
w = httptest.NewRecorder()
e.ServeHTTP(w, req)
assert.Equal(t, http.StatusInternalServerError, w.Code)
var ae apiError
if err := json.NewDecoder(w.Body).Decode(&ae); err != nil {
assert.Fail(t, "failed to decode", err.Error())
}
assert.Equal(t, "i am an error", ae.Error)
}