-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers_test.go
175 lines (144 loc) · 3.66 KB
/
handlers_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"bytes"
"errors"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
type mockSpeechProcessor struct {
text string
err error
}
func (m *mockSpeechProcessor) ProcessText(input io.Reader) error {
if m.err != nil {
return m.err
}
return nil
}
func (m *mockSpeechProcessor) GenerateRandomText(_ uint) string {
return m.text
}
func Test_Learn(t *testing.T) {
t.Parallel()
var validProcessor = &mockSpeechProcessor{}
var tests = []struct {
name string
req *http.Request
processor *mockSpeechProcessor
wantCode int
wantResponse string
}{
{
name: "ok",
req: getValidRequest(t, "POST", "/learn", "this is my test body"),
processor: validProcessor,
wantCode: http.StatusOK,
wantResponse: "",
},
{
name: "invalid method",
req: getValidRequest(t, "GET", "/generate"),
processor: validProcessor,
wantCode: http.StatusMethodNotAllowed,
wantResponse: "Invalid method\n",
},
{
name: "invalid content type",
req: func() *http.Request {
var r = getValidRequest(t, "POST", "/learn")
r.Header.Set("Content-Type", "text/html")
return r
}(),
processor: validProcessor,
wantCode: http.StatusUnprocessableEntity,
wantResponse: "Invalid content type\n",
},
{
name: "error - processing text",
req: getValidRequest(t, "POST", "/learn", "this is my test body"),
processor: &mockSpeechProcessor{err: errors.New("can't compute")},
wantCode: http.StatusUnprocessableEntity,
wantResponse: "Error processing text: can't compute\n",
},
}
for _, tt := range tests {
var tt = tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var recorder = httptest.NewRecorder()
var h = Handlers{
processor: tt.processor,
}
h.Learn(recorder, tt.req)
if recorder.Code != tt.wantCode {
t.Errorf("got: %v, want: %v", recorder.Code, tt.wantCode)
}
if !reflect.DeepEqual(recorder.Body.String(), tt.wantResponse) {
t.Errorf("got %v, want %v", recorder.Body.String(), tt.wantResponse)
}
})
}
}
func Test_Generate(t *testing.T) {
t.Parallel()
var tests = []struct {
name string
req *http.Request
wantCode int
wantResponse string
}{
{
name: "ok",
req: getValidRequest(t, "GET", "/generate"),
wantCode: http.StatusOK,
wantResponse: "random text is random",
},
{
name: "invalid method",
req: getValidRequest(t, "POST", "/generate"),
wantCode: http.StatusMethodNotAllowed,
wantResponse: "Invalid method\n",
},
}
for _, tt := range tests {
var tt = tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var recorder = httptest.NewRecorder()
var h = Handlers{
processor: &mockSpeechProcessor{
text: "random text is random",
},
}
h.Generate(recorder, tt.req)
if recorder.Code != tt.wantCode {
t.Errorf("got: %v, want: %v", recorder.Code, tt.wantCode)
}
if !reflect.DeepEqual(recorder.Body.String(), tt.wantResponse) {
t.Errorf("got %v, want %v", recorder.Body.String(), tt.wantResponse)
}
})
}
}
func getValidRequest(t *testing.T, method string, path string, input ...string) *http.Request {
t.Helper()
var body io.Reader
if len(input) > 0 && input[0] != "" {
var data = &bytes.Buffer{}
if _, err := data.WriteString(input[0]); err != nil {
t.Fatalf("error writing request body data: %v", err)
}
body = data
}
var validReq, err = http.NewRequest(method, path, body)
if err != nil {
t.Fatalf("error creaing request: %v", err)
}
if body != nil {
validReq.Header.Set("Content-Type", textPlainContentType)
}
return validReq
}