-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresponse_test.go
More file actions
235 lines (200 loc) · 6.09 KB
/
response_test.go
File metadata and controls
235 lines (200 loc) · 6.09 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// SPDX-FileCopyrightText: 2025 Greenbone AG
//
// SPDX-License-Identifier: AGPL-3.0-or-later
package httpassert
import (
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestResponse(t *testing.T) {
router := setupRouter(t)
request := New(t, router)
t.Run("JsonFile assertion", func(t *testing.T) {
path := writeTempFile(t, `{"asd":"123","items":["a","b","c"]}`)
request.Post("/json").
Expect().
JsonFile(path)
})
t.Run("JsonTemplate value replacement and ignore handling with object", func(t *testing.T) {
router := http.NewServeMux()
router.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprint(w, `{"id":42,"name":"Greenbone","version":"1.0.0","tags":["alpha","beta"]}`)
require.NoError(t, err)
})
m := New(t, router)
t.Run("replaces scalar and nested array values", func(t *testing.T) {
m.Get("/api").
Expect().
JsonTemplate(`{"id":0,"name":"","version":"","tags":["x","y"]}`, map[string]any{
"$.id": 42,
"$.name": "Greenbone",
"$.tags[0]": "alpha",
"$.tags[1]": "beta",
"$.version": "1.0.0",
})
})
t.Run("ignores values marked as <IGNORE>", func(t *testing.T) {
m.Get("/api").
Expect().
JsonTemplate(`{"id":0,"name":"","version":"","tags":["x","y"]}`, map[string]any{
"$.id": 42,
"$.name": "Greenbone",
"$.version": IgnoreJsonValue, // should not cause mismatch
"$.tags[0]": "alpha",
"$.tags[1]": "beta",
})
})
t.Run("handles nested structure with mixed types", func(t *testing.T) {
router := http.NewServeMux()
router.HandleFunc("/complex", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, err := w.Write([]byte(`{"meta":{"id":10,"info":{"env":"prod","build":17}}}`))
require.NoError(t, err)
})
New(t, router).Get("/complex").
Expect().
JsonTemplate(`{"meta":{"id":0,"info":{"env":"","build":0}}}`, map[string]any{
"$.meta.id": 10,
"$.meta.info.env": "prod",
"$.meta.info.build": IgnoreJsonValue, // ignored build number
})
})
})
t.Run("JsonTemplate value replacement with array", func(t *testing.T) {
router := http.NewServeMux()
router.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprint(w, `[{"id":42,"name":"Greenbone","version":"1.0.0","tags":["alpha","beta"]}]`)
require.NoError(t, err)
})
New(t, router).Get("/api").
Expect().
JsonTemplate(`[{"id":0,"name":"","version":"","tags":["x","y"]}]`, map[string]any{
"$[0].id": 42,
"$[0].name": "Greenbone",
"$[0].tags[0]": "alpha",
"$[0].tags[1]": "beta",
"$[0].version": "1.0.0",
})
})
t.Run("GetBody returns non-empty", func(t *testing.T) {
body := request.Post("/json").
Expect().
GetBody()
if body == "" {
t.Error("expected non-empty body")
}
})
t.Run("GetJsonBodyObject unmarshal data", func(t *testing.T) {
type foo struct {
Asd string `json:"asd"`
Items []string `json:"items"`
}
asd := foo{}
request.Post("/json").
Expect().
GetJsonBodyObject(&asd)
assert.Equal(t, "123", asd.Asd)
assert.Equal(t, []string{"a", "b", "c"}, asd.Items)
})
t.Run("Log prints response", func(t *testing.T) {
request.Post("/json").
Expect().
Log()
})
t.Run("JsonTemplateFile", func(t *testing.T) {
router := http.NewServeMux()
router.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprint(w, `{"id":42,"name":"Greenbone","version":"1.0.0"}`)
require.NoError(t, err)
})
m := New(t, router)
t.Run("successfully compares JSON template file", func(t *testing.T) {
path := writeTempFile(t, `{"id":0,"name":"","version":""}`)
m.Get("/api").
Expect().
JsonTemplateFile(path, map[string]any{
"$.id": 42,
"$.name": "Greenbone",
"$.version": "1.0.0",
})
})
t.Run("handles <IGNORE> value in JsonTemplateFile", func(t *testing.T) {
path := writeTempFile(t, `{"id":0,"name":"","version":""}`)
m.Get("/api").
Expect().
JsonTemplateFile(path, map[string]any{
"$.id": 42,
"$.name": "Greenbone",
"$.version": IgnoreJsonValue, // ignored field
})
})
})
t.Run("Header", func(t *testing.T) {
router := http.NewServeMux()
router.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
})
t.Run("compare string", func(t *testing.T) {
m := New(t, router)
m.Get("/api").
Expect().
Header("Content-Type", "application/json")
})
t.Run("extract value to variable", func(t *testing.T) {
m := New(t, router)
var value string
m.Get("/api").
Expect().
Header("Content-Type", ExtractRegexTo(`application/(.*)`, &value))
assert.Equal(t, "json", value)
})
t.Run("use matcher", func(t *testing.T) {
m := New(t, router)
m.Get("/api").
Expect().
Header("Content-Type", Regex("application/[json]"))
})
})
t.Run("POST basic JSON", func(t *testing.T) {
request.Post("/json").
ContentType("application/json").
Content(`{"asd":"123"}`).
Expect().
StatusCode(http.StatusOK).
ContentType("application/json").
JsonPath("$.asd", "123").
Json(`{"asd":"123","items":["a","b","c"]}`)
})
t.Run("Get with list JSON", func(t *testing.T) {
request.Get("/json").
Expect().
StatusCode(http.StatusOK).
JsonPath("$.items[0]", "a").
JsonPathJson("$.items", `["a","b","c"]`)
})
t.Run("GET empty body", func(t *testing.T) {
request.Get("/empty").
Expect().
StatusCode(http.StatusOK).
NoContent()
})
t.Run("plain text", func(t *testing.T) {
request.Put("/text").
ContentType("text/plain").
Content("ok").
Expect().
StatusCode(http.StatusOK).
ContentType("text/plain").
Body("ok")
})
}