-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller_test.go
99 lines (94 loc) · 2.23 KB
/
controller_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
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetByParams(t *testing.T) {
const host = "http://test"
tests := []struct {
name string
path string
statusCode int
}{
{
"With empty path",
"",
400,
},
{
"Without parameters",
"/fizz-buzz",
400,
},
{
"With default parameters",
"/fizz-buzz/?int1=3&int2=5&limit=100&str1=fizz&str2=buzz",
200,
},
{
"With bad parameters",
"/fizz-buzz/?int2=5&limit=100&str1=fizz&str2=buzz",
400,
},
}
storage := NewInMemory()
service := NewFizzBuzzService(storage)
controller := NewController(service)
for _, tc := range tests {
url := fmt.Sprintf("%s%s", host, tc.path)
request, err := http.NewRequest(http.MethodGet, url, nil)
assert.NoError(t, err)
response := httptest.NewRecorder()
handler := http.HandlerFunc(controller.GetByParams)
handler.ServeHTTP(response, request)
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.statusCode, response.Code)
})
}
}
func TestStats(t *testing.T) {
tests := []struct {
name string
statusCode int
want string
}{
{
"Without stats parameters",
200,
"[]\n",
},
{
"With stats parameters",
200,
"[{\"Hits\":1,\"Params\":{\"Int1\":3,\"Int2\":5,\"Limit\":100,\"Str1\":\"fizz\",\"Str2\":\"buzz\"}}]\n",
},
}
storage := NewInMemory()
service := NewFizzBuzzService(storage)
controller := NewController(service)
for _, tc := range tests {
if tc.want != "[]\n" {
callFizzBuzz(t, controller)
}
request, err := http.NewRequest(http.MethodGet, "", nil)
assert.NoError(t, err)
response := httptest.NewRecorder()
handler := http.HandlerFunc(controller.Stats)
handler.ServeHTTP(response, request)
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.statusCode, response.Code)
assert.Equal(t, tc.want, response.Body.String())
})
}
}
func callFizzBuzz(t *testing.T, controller IController) {
t.Helper()
request, err := http.NewRequest(http.MethodGet, "/fizz-buzz/?int1=3&int2=5&limit=100&str1=fizz&str2=buzz", nil)
assert.NoError(t, err)
response := httptest.NewRecorder()
handler := http.HandlerFunc(controller.GetByParams)
handler.ServeHTTP(response, request)
}