-
Notifications
You must be signed in to change notification settings - Fork 0
/
mockhttp_test.go
136 lines (119 loc) · 3.07 KB
/
mockhttp_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
package mockhttp
import (
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/nicklarsennz/mockhttp/responders"
"github.com/pkg/errors"
)
func TestMatchResponse(t *testing.T) {
req, err := http.NewRequest("GET", "http://anything/blah", nil)
if err != nil {
t.Error(errors.Wrap(err, "http.NewRequest").Error())
}
config := &responders.ResponderConfig{
Responders: []responders.Responder{
{
When: responders.WhenRequest{
Http: responders.WhenHttp{
Method: "GET",
Path: "/blah",
},
},
Then: responders.ThenResponse{
Http: responders.ThenHttp{
Status: 200,
Message: "OK",
},
Headers: map[string]string{"Content-Type": "text/plain"},
Body: "Hello",
},
},
},
}
var response *http.Response
response = MatchResponse(req, config)
if response == nil {
t.Fatalf("nil response")
}
// Check HTTP Status
expectedStatus := 200
actualStatus := response.StatusCode
if expectedStatus != actualStatus {
t.Errorf("\nexpected Status: '%d'\ngot Status: '%d', %s", expectedStatus, actualStatus, response.Status)
}
// Check Headers
expectedContentType := "text/plain"
actualContentType := response.Header.Get("Content-Type")
if expectedContentType != actualContentType {
t.Errorf("\nexpected Content-Type: '%s'\ngot Content-Type: '%s'", expectedContentType, actualContentType)
}
// Check Body
body, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Errorf(errors.Wrap(err, "ioutil.ReadAll").Error())
}
expectedBody := "Hello"
actualBody := strings.TrimSpace(string(body))
if expectedBody != actualBody {
t.Errorf("\nexpected body: '%s'\nactual body: '%s'", expectedBody, actualBody)
}
}
func TestMatchResponseQueryParam(t *testing.T) {
req, err := http.NewRequest("GET", "http://anything/api?offset=4", nil)
if err != nil {
t.Error(errors.Wrap(err, "http.NewRequest").Error())
}
config := &responders.ResponderConfig{
Responders: []responders.Responder{
{
When: responders.WhenRequest{
Http: responders.WhenHttp{
Method: "GET",
Path: "/api",
},
},
Then: responders.ThenResponse{
Http: responders.ThenHttp{
Status: 200,
Message: "OK",
},
Headers: map[string]string{"Content-Type": "text/plain"},
Body: "Hello",
},
},
{
When: responders.WhenRequest{
Http: responders.WhenHttp{
Method: "GET",
Path: "/api?offset=4",
},
},
Then: responders.ThenResponse{
Http: responders.ThenHttp{
Status: 200,
Message: "OK",
},
Headers: map[string]string{"Content-Type": "text/plain"},
Body: "Goodbye",
},
},
},
}
var response *http.Response
response = MatchResponse(req, config)
if response == nil {
t.Fatalf("nil response")
}
// Check Body
body, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Errorf(errors.Wrap(err, "ioutil.ReadAll").Error())
}
expectedBody := "Goodbye"
actualBody := strings.TrimSpace(string(body))
if expectedBody != actualBody {
t.Errorf("\nexpected body: '%s'\nactual body: '%s'", expectedBody, actualBody)
}
}