| 187 | } |
| 188 | |
| 189 | func TestApplySet(t *testing.T) { |
| 190 | tests := []struct { |
| 191 | name string |
| 192 | responses string |
| 193 | opts setOptions |
| 194 | wantPos int |
| 195 | want string |
| 196 | wantErr string |
| 197 | }{ |
| 198 | { |
| 199 | name: "--json rejects a trailing array delimiter", |
| 200 | responses: `{}`, |
| 201 | opts: setOptions{name: "graphql A", at: "a", value: `1]`}, |
| 202 | wantErr: "--json is not valid JSON: unexpected data after the JSON value", |
| 203 | }, |
| 204 | { |
| 205 | name: "--json rejects a trailing object delimiter", |
| 206 | responses: `{}`, |
| 207 | opts: setOptions{name: "graphql A", at: "a", value: `{"x":1}}`}, |
| 208 | wantErr: "--json is not valid JSON: unexpected data after the JSON value", |
| 209 | }, |
| 210 | { |
| 211 | name: "--json rejects multiple values", |
| 212 | responses: `{}`, |
| 213 | opts: setOptions{name: "graphql A", at: "a", value: `1 2`}, |
| 214 | wantErr: "--json is not valid JSON: unexpected data after the JSON value", |
| 215 | }, |
| 216 | { |
| 217 | name: "--json accepts trailing whitespace", |
| 218 | responses: `{}`, |
| 219 | opts: setOptions{name: "graphql A", at: "a", value: "1 \n\t"}, |
| 220 | want: `[{"body":{"a":1}}]`, |
| 221 | }, |
| 222 | { |
| 223 | name: "creates the first response", |
| 224 | responses: `{}`, |
| 225 | opts: setOptions{name: "GET /x", status: 404, body: `{"message":"Not Found"}`, bodySet: true}, |
| 226 | want: `[{"status":404,"body":{"message":"Not Found"}}]`, |
| 227 | }, |
| 228 | { |
| 229 | name: "changes only the parts passed", |
| 230 | responses: `{"GET /x":[{"status":201,"headers":{"X-A":"1"},"body":{"a":1}}]}`, |
| 231 | opts: setOptions{name: "GET /x", headers: []string{"x-b: 2"}}, |
| 232 | want: `[{"status":201,"headers":{"X-A":"1","X-B":"2"},"body":{"a":1}}]`, |
| 233 | }, |
| 234 | { |
| 235 | name: "an empty header value removes the header", |
| 236 | responses: `{"GET /x":[{"headers":{"X-A":"1"}}]}`, |
| 237 | opts: setOptions{name: "GET /x", headers: []string{"x-a:"}}, |
| 238 | want: `[{}]`, |
| 239 | }, |
| 240 | { |
| 241 | name: "an empty body clears it", |
| 242 | responses: `{"GET /x":[{"body":{"a":1}}]}`, |
| 243 | opts: setOptions{name: "GET /x", bodySet: true}, |
| 244 | want: `[{}]`, |
| 245 | }, |
| 246 | { |