-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert_test.go
136 lines (116 loc) · 3.1 KB
/
assert_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 congruent
import (
"net/http"
"net/url"
"strings"
"testing"
)
func TestStatus(t *testing.T) {
gu, err := url.Parse("http://localhost/")
if err != nil {
t.Error(err)
t.Fail()
}
bu, err := url.Parse("http://bad/")
if err != nil {
t.Error(err)
t.Fail()
}
mockReq := &http.Request{Method: "GET", URL: gu}
mockBadReq := &http.Request{Method: "GET", URL: bu}
responses := Responses{
&Response{mockReq, nil, nil, 200},
&Response{mockReq, nil, nil, 200},
&Response{mockReq, nil, nil, 200}}
if err := responses.StatusEqual(200); err != nil {
t.Error(err)
}
if err := responses.StatusSame(); err != nil {
t.Error(err)
}
if err := responses.StatusEqual(201); err == nil {
t.Error("Expected error, but got none!")
}
responses = Responses{
&Response{mockReq, nil, nil, 200},
&Response{mockBadReq, nil, nil, 201},
&Response{mockReq, nil, nil, 200}}
if err := responses.StatusEqual(200); err == nil {
t.Error("Expected error, but got none!")
} else {
if !strings.Contains(err.Error(), "bad") || !strings.Contains(err.Error(), "201") {
t.Errorf("Did not get expected error string, got: %v", err)
}
}
if err := responses.StatusSame(); err == nil {
t.Error("Expected error, but got none!")
} else {
if !strings.Contains(err.Error(), "bad") || !strings.Contains(err.Error(), "201") {
t.Errorf("Did not get expected error string, got: %v", err)
}
}
}
func TestBodySame(t *testing.T) {
gu, err := url.Parse("http://localhost/")
if err != nil {
t.Error(err)
t.Fail()
}
bu, err := url.Parse("http://bad/")
if err != nil {
t.Error(err)
t.Fail()
}
mockReq := &http.Request{Method: "GET", URL: gu}
mockBadReq := &http.Request{Method: "GET", URL: bu}
responses := Responses{
&Response{mockReq, nil, []byte{'a', 'b', 'c'}, 200},
&Response{mockReq, nil, []byte{'a', 'b', 'c'}, 200},
&Response{mockReq, nil, []byte{'a', 'b', 'c'}, 200}}
if err := responses.BodySame(); err != nil {
t.Error(err)
}
responses = Responses{
&Response{mockReq, nil, []byte{'a', 'b', 'c'}, 200},
&Response{mockBadReq, nil, []byte{'a', 'b', 'd'}, 200},
&Response{mockReq, nil, []byte{'a', 'b', 'c'}, 200}}
if err := responses.BodySame(); err == nil {
t.Error("Expected error, but got none!")
}
}
func TestBodyContentSame(t *testing.T) {
gu, err := url.Parse("http://localhost/")
if err != nil {
t.Error(err)
t.Fail()
}
bu, err := url.Parse("http://bad/")
if err != nil {
t.Error(err)
t.Fail()
}
gs1 := []byte(`{"a":{"b":"c"}}`)
gs2 := []byte(`
{
"a": {
"b": "c"
}
}`)
bs1 := []byte(`{"a":{"b":"d"}}`)
mockReq := &http.Request{Method: "GET", URL: gu}
mockBadReq := &http.Request{Method: "GET", URL: bu}
responses := Responses{
&Response{mockReq, nil, gs1, 200},
&Response{mockReq, nil, gs2, 200},
&Response{mockReq, nil, gs1, 200}}
if err := responses.BodyContentSame(); err != nil {
t.Error(err)
}
responses = Responses{
&Response{mockReq, nil, gs2, 200},
&Response{mockBadReq, nil, bs1, 200},
&Response{mockReq, nil, gs2, 200}}
if err := responses.BodyContentSame(); err == nil {
t.Error("Expected error, but got none!")
}
}