-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandlers_config_test.go
113 lines (89 loc) · 3.67 KB
/
handlers_config_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package shuttle
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
)
func TestShuttleNopConfig(t *testing.T) {
response := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/", nil)
handler := NewHandler()
handler.ServeHTTP(response, request)
Assert(t).That(response.Code).Equals(204)
}
func TestShuttleDeserialize(t *testing.T) {
response := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/", bytes.NewBufferString(`{"name":"name-here"}`))
request.Header["Content-Type"] = []string{"application/json"}
handler := NewHandler(
Options.InputModel(func() InputModel { return &FakeDeserializeInputModel{Name: "garbage"} }),
Options.DeserializeJSON(true),
)
handler.ServeHTTP(response, request)
Assert(t).That(response.Code).Equals(204)
}
func TestShuttleSkipSerialization(t *testing.T) {
response := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/", bytes.NewBufferString(`{"name":"name-here"}`))
handler := NewHandler(
Options.InputModel(func() InputModel { return &FakeDeserializeInputModel{Name: "garbage"} }),
Options.DeserializeJSON(false),
Options.SerializeJSON(false),
)
handler.ServeHTTP(response, request)
Assert(t).That(response.Code).Equals(204)
}
func TestShuttleDeserializationFailure(t *testing.T) {
response := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/", bytes.NewBufferString(`{`))
request.Header["Content-Type"] = []string{"application/json"}
handler := NewHandler(
Options.InputModel(func() InputModel { return &FakeDeserializeInputModel{Name: "garbage"} }),
Options.DeserializeJSON(true),
)
handler.ServeHTTP(response, request)
Assert(t).That(response.Code).Equals(400)
}
func TestShuttleBindFailure(t *testing.T) {
response := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/", bytes.NewBufferString(`{"name":"value"}`))
request.Header["Content-Type"] = []string{"application/json"}
bindError := InputError{Message: "bind-failure"}
handler := NewHandler(
Options.InputModel(func() InputModel { return &FakeDeserializeInputModel{Name: "garbage", bindFailure: bindError} }),
Options.DeserializeJSON(true),
)
handler.ServeHTTP(response, request)
Assert(t).That(response.Code).Equals(400)
Assert(t).That(response.Body.String()).Equals(`{"errors":[{"message":"bind-failure"}]}` + "\n")
}
func TestShuttleValidationFailure(t *testing.T) {
response := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/", nil)
handler := NewHandler(
Options.InputModel(func() InputModel { return &FakeDeserializeInputModel{Name: "garbage", validationFailure: 2} }),
)
handler.ServeHTTP(response, request)
Assert(t).That(response.Code).Equals(422)
Assert(t).That(response.Body.String()).Equals(`{"errors":[{"message":"validation-error"},{"message":"validation-error"}]}` + "\n")
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func TestInputError_Error(t *testing.T) {
input := &InputError{Message: "hello"}
Assert(t).That(input.Error()).Equals(input.Message)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type FakeDeserializeInputModel struct {
Name string `json:"name"`
bindFailure error
validationFailure int
}
func (this *FakeDeserializeInputModel) Reset() { this.Name = "" }
func (this *FakeDeserializeInputModel) Bind(*http.Request) error { return this.bindFailure }
func (this *FakeDeserializeInputModel) Validate(target []error) int {
for i := 0; i < this.validationFailure; i++ {
target[i] = InputError{Message: "validation-error"}
}
return this.validationFailure
}