-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_json_test.go
More file actions
49 lines (43 loc) · 1.28 KB
/
error_json_test.go
File metadata and controls
49 lines (43 loc) · 1.28 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
package utils_test
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/loickreitmann/utils"
)
var errorCases = []struct {
name string
code int
err error
}{
{name: "forbidden error", code: http.StatusForbidden, err: errors.New("you shall not pass")},
{name: "not found error", code: http.StatusNotFound, err: errors.New("nothing to see here move along")},
{name: "service unavailable", code: http.StatusServiceUnavailable, err: errors.New("fuggehdaboutdit")},
}
func TestUtils_ErrorJSON(t *testing.T) {
var testUtils utils.Utils
for _, errorCase := range errorCases {
// ARRANGE
rr := httptest.NewRecorder()
// ACT
err := testUtils.ErrorJSON(rr, errorCase.err, errorCase.code)
// ASSERT
if err != nil {
t.Errorf("[%s]: unexpected error: %v", errorCase.name, err.Error())
}
var errorPayload utils.JSONResponse
decoder := json.NewDecoder(rr.Body)
err = decoder.Decode(&errorPayload)
if err != nil {
t.Errorf("[%s]: unable to decode json: %v", errorCase.name, err.Error())
}
if errorPayload.Data != nil {
t.Errorf("[%s]: unexpected data: %v", errorCase.name, errorPayload.Data)
}
if rr.Code != errorCase.code {
t.Errorf("[%s]: wrong status code; expected %d, got %d", errorCase.name, errorCase.code, rr.Code)
}
}
}