-
Notifications
You must be signed in to change notification settings - Fork 0
/
statusHTTP_test.go
77 lines (58 loc) · 1.92 KB
/
statusHTTP_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package statusHTTP
import (
"encoding/json"
"testing"
"errors"
"net/http/httptest"
)
func DefaultTest(t *testing.T) {
rqrs := httptest.NewRecorder()
var statusHTTPResponse statusHTTPError
statusCode := 500
errorType := statusCodes[statusCode]
message := errorType
statusHTTP(rqrs, statusCode)
if err := json.Unmarshal(rqrs.Body.Bytes(), &statusHTTPResponse); err != nil {
t.Errorf("Not a valid Response!: %v", err)
}
if statusHTTPResponse.ErrorType != errorType || statusHTTPResponse.Message != message || statusHTTPResponse.StatusCode != statusCode {
t.Fail()
}
if rqrs.Code != statusCode {
t.Errorf("wrong status code!: got %v need %v", rqrs.Code, statusCode)
}
}
func TestMessage(t *testing.T) {
rqrs := httptest.NewRecorder()
var statusHTTPResponse statusHTTPError
statusCode := 500
errorType := statusCodes[statusCode]
message := "Some random msg for test"
statusHTTP(rqrs, statusCode, message)
if err := json.Unmarshal(rqrs.Body.Bytes(), &statusHTTPResponse); err != nil {
t.Errorf("Invalid Response!: %v", err)
}
if statusHTTPResponse.ErrorType != errorType || statusHTTPResponse.Message != message || statusHTTPResponse.StatusCode != statusCode {
t.Fail()
}
if rqrs.Code != statusCode {
t.Errorf("Wrong status code: got %v need %v", rqrs.Code, statusCode)
}
}
func TestErrorMessage(t *testing.T) {
rqrs := httptest.NewRecorder()
var statusHTTPResponse statusHTTPError
statusCode := 500
errorType := statusCodes[statusCode]
message := errors.New("Random msg for error")
statusHTTP(rqrs, statusCode, message)
if err := json.Unmarshal(rqrs.Body.Bytes(), &statusHTTPResponse); err != nil {
t.Errorf("Invalid Response!: %v", err)
}
if statusHTTPResponse.ErrorType != errorType || statusHTTPResponse.Message != message.Error() || statusHTTPResponse.StatusCode != statusCode {
t.Fail()
}
if rqrs.Code != statusCode {
t.Errorf("Wrong status code: got %v need %v", rqrs.Code, statusCode)
}
}