generated from shahariaazam/go-pkg
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpmama_test.go
119 lines (110 loc) · 3.49 KB
/
httpmama_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
package httpmama
import (
"io/ioutil"
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCreateTestServer(t *testing.T) {
testEndpoints := []TestEndpoint{
{
Path: "/foo",
ResponseString: "hello, world!",
ResponseHeader: http.Header{"Content-Type": []string{"text/plain"}},
},
{
Path: "/bar",
ResponseString: "goodbye, world!",
ResponseHeader: http.Header{"Content-Type": []string{"text/plain"}},
},
{
Path: "/user",
ResponseString: "hello, john!",
ResponseHeader: http.Header{"Content-Type": []string{"text/plain"}},
QueryParams: url.Values{"name": []string{"john"}},
},
{
Path: "/user",
ResponseString: "hello, doe!",
ResponseHeader: http.Header{"Content-Type": []string{"text/plain"}},
QueryParams: url.Values{"name": []string{"doe"}},
},
{
Path: "/user",
ResponseString: "hello, doe! Age: 30",
ResponseHeader: http.Header{"Content-Type": []string{"text/plain"}},
QueryParams: url.Values{"name": []string{"doe"}, "age": []string{"30"}},
},
{
Path: "/return-json",
ResponseString: `{"hello": "world"}`,
ResponseHeader: http.Header{"Content-Type": []string{"application/json"}, "Some-Other-Header": []string{"some-other-header"}},
},
}
testCases := []struct {
name string
endpoints []TestEndpoint
requestPath string
expectedBody string
expectedHeaders map[string]string
expectedStatusCode int
}{
{
name: "single endpoint",
requestPath: "/foo",
expectedBody: "hello, world!",
expectedHeaders: map[string]string{"Content-Type": "text/plain"},
},
{
name: "endpoint with single query params",
requestPath: "/user?" + url.Values{"name": []string{"doe"}}.Encode(),
expectedBody: "hello, doe!",
expectedHeaders: map[string]string{"Content-Type": "text/plain"},
},
{
name: "endpoint with multiple query params",
requestPath: "/user?" + url.Values{"name": []string{"doe"}, "age": []string{"30"}}.Encode(),
expectedBody: "hello, doe! Age: 30",
expectedHeaders: map[string]string{"Content-Type": "text/plain"},
},
{
name: "non-existent endpoint should return 404",
requestPath: "/something/else",
expectedBody: "404 page not found\n",
expectedHeaders: map[string]string{"Content-Type": "text/plain; charset=utf-8"},
},
{
name: "returning json with correct header",
requestPath: "/return-json",
expectedBody: `{"hello": "world"}`,
expectedHeaders: map[string]string{"Content-Type": "application/json"},
},
{
name: "return multiple header",
requestPath: "/return-json",
expectedBody: `{"hello": "world"}`,
expectedHeaders: map[string]string{"Content-Type": "application/json", "Some-Other-Header": "some-other-header"},
},
}
config := ServerConfig{TestEndpoints: testEndpoints}
server := NewTestServer(config)
defer server.Close()
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
resp, err := http.Get(server.URL + tc.requestPath)
if err != nil {
t.Errorf("error making GET request: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("error reading response body: %v", err)
}
assert.Equal(t, tc.expectedBody, string(body))
for hk, hv := range tc.expectedHeaders {
assert.Equal(t, hv, resp.Header.Get(hk))
}
})
}
}