-
Notifications
You must be signed in to change notification settings - Fork 15
/
encoder_test.go
54 lines (47 loc) · 2.02 KB
/
encoder_test.go
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
53
54
package lmdrouter
import (
"errors"
"net/http"
"testing"
"github.com/jgroeneveld/trial/assert"
)
func TestHandleError(t *testing.T) {
t.Run("Handle an HTTPError", func(t *testing.T) {
res, _ := HandleError(HTTPError{
Code: http.StatusBadRequest,
Message: "Invalid input",
})
assert.Equal(t, http.StatusBadRequest, res.StatusCode, "status code must be correct")
assert.Equal(t, `{"code":400,"message":"Invalid input"}`, res.Body, "body must be correct")
})
t.Run("Handle an HTTPError when ExposeServerErrors is true", func(t *testing.T) {
ExposeServerErrors = true
res, _ := HandleError(HTTPError{
Code: http.StatusInternalServerError,
Message: "database down",
})
assert.Equal(t, http.StatusInternalServerError, res.StatusCode, "status code must be correct")
assert.Equal(t, `{"code":500,"message":"database down"}`, res.Body, "body must be correct")
})
t.Run("Handle an HTTPError when ExposeServerErrors is false", func(t *testing.T) {
ExposeServerErrors = false
res, _ := HandleError(HTTPError{
Code: http.StatusInternalServerError,
Message: "database down",
})
assert.Equal(t, http.StatusInternalServerError, res.StatusCode, "status code must be correct")
assert.Equal(t, `{"code":500,"message":"Internal Server Error"}`, res.Body, "body must be correct")
})
t.Run("Handle a general error when ExposeServerErrors is true", func(t *testing.T) {
ExposeServerErrors = true
res, _ := HandleError(errors.New("database down"))
assert.Equal(t, http.StatusInternalServerError, res.StatusCode, "status code must be correct")
assert.Equal(t, `{"code":500,"message":"database down"}`, res.Body, "body must be correct")
})
t.Run("Handle a general error when ExposeServerErrors is false", func(t *testing.T) {
ExposeServerErrors = false
res, _ := HandleError(errors.New("database down"))
assert.Equal(t, http.StatusInternalServerError, res.StatusCode, "status code must be correct")
assert.Equal(t, `{"code":500,"message":"Internal Server Error"}`, res.Body, "body must be correct")
})
}