|
1 | 1 | import { expect } from '@playwright/test'; |
2 | 2 |
|
3 | | -import { loadFixture } from '../../playwright/paths'; |
| 3 | +import { getFixturePath, loadFixture } from '../../playwright/paths'; |
4 | 4 | import { test } from '../../playwright/test';; |
5 | 5 |
|
6 | 6 | test.describe('pre-request features tests', async () => { |
@@ -30,7 +30,7 @@ test.describe('pre-request features tests', async () => { |
30 | 30 | // but it is rewritten here |
31 | 31 | insomnia.baseEnvironment.set('preDefinedValue', 'fromScript'); |
32 | 32 | // "customValue" is already defined in the folder environment. |
33 | | - // folder version will override the following wone |
| 33 | + // folder version will override the following one |
34 | 34 | insomnia.baseEnvironment.set('customValue', 'fromScript'); |
35 | 35 | `, |
36 | 36 | body: `{ |
@@ -217,6 +217,111 @@ test.describe('pre-request features tests', async () => { |
217 | 217 | expect(bodyJson.data).toEqual('rawContent'); |
218 | 218 | }, |
219 | 219 | }, |
| 220 | + { |
| 221 | + name: 'sendRequest custom mode', |
| 222 | + preReqScript: ` |
| 223 | + const rawReq = { |
| 224 | + url: 'http://127.0.0.1:4010/echo', |
| 225 | + method: 'POST', |
| 226 | + header: { |
| 227 | + 'Content-Type': 'text/plain', |
| 228 | + }, |
| 229 | + body: { |
| 230 | + mode: 'raw', |
| 231 | + raw: 'rawContent', |
| 232 | + }, |
| 233 | + }; |
| 234 | + const urlencodedReq = { |
| 235 | + url: 'http://127.0.0.1:4010/echo', |
| 236 | + method: 'POST', |
| 237 | + header: { |
| 238 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 239 | + }, |
| 240 | + body: { |
| 241 | + mode: 'urlencoded', |
| 242 | + urlencoded: [ |
| 243 | + { key: 'k1', value: 'v1' }, |
| 244 | + { key: 'k2', value: 'v2' }, |
| 245 | + ], |
| 246 | + }, |
| 247 | + }; |
| 248 | + const gqlReq = { |
| 249 | + url: 'http://127.0.0.1:4010/echo', |
| 250 | + method: 'POST', |
| 251 | + header: { |
| 252 | + 'Content-Type': 'application/graphql', |
| 253 | + }, |
| 254 | + body: { |
| 255 | + mode: 'graphql', |
| 256 | + graphql: { |
| 257 | + query: 'query', |
| 258 | + operationName: 'operation', |
| 259 | + variables: 'var', |
| 260 | + }, |
| 261 | + }, |
| 262 | + }; |
| 263 | + const fileReq = { |
| 264 | + url: 'http://127.0.0.1:4010/echo', |
| 265 | + method: 'POST', |
| 266 | + header: { |
| 267 | + 'Content-Type': 'application/octet-stream', |
| 268 | + }, |
| 269 | + body: { |
| 270 | + mode: 'file', |
| 271 | + file: "${getFixturePath('files/rawfile.txt')}", |
| 272 | + }, |
| 273 | + }; |
| 274 | + const formdataReq = { |
| 275 | + url: 'http://127.0.0.1:4010/echo', |
| 276 | + method: 'POST', |
| 277 | + header: { |
| 278 | + // 'Content-Type': 'multipart/form-data', |
| 279 | + }, |
| 280 | + body: { |
| 281 | + mode: 'formdata', |
| 282 | + formdata: [ |
| 283 | + { key: 'k1', type: 'text', value: 'v1' }, |
| 284 | + { key: 'k2', type: 'file', value: "${getFixturePath('files/rawfile.txt')}" }, |
| 285 | + ], |
| 286 | + }, |
| 287 | + }; |
| 288 | + const promises = [rawReq, urlencodedReq, gqlReq, fileReq, formdataReq].map(req => { |
| 289 | + return new Promise((resolve, reject) => { |
| 290 | + insomnia.sendRequest( |
| 291 | + req, |
| 292 | + (err, resp) => { |
| 293 | + if (err != null) { |
| 294 | + reject(err); |
| 295 | + } else { |
| 296 | + resolve(resp); |
| 297 | + } |
| 298 | + } |
| 299 | + ); |
| 300 | + }); |
| 301 | + }); |
| 302 | + // send request |
| 303 | + const resps = await Promise.all(promises); |
| 304 | + // set envs |
| 305 | + insomnia.environment.set('rawBody', resps[0].body); |
| 306 | + insomnia.environment.set('urlencodedBody', resps[1].body); |
| 307 | + insomnia.environment.set('gqlBody', resps[2].body); |
| 308 | + insomnia.environment.set('fileBody', resps[3].body); |
| 309 | + insomnia.environment.set('formdataBody', resps[4].body); |
| 310 | + `, |
| 311 | + body: '{ "rawBody": {{ _.rawBody }}, "urlencodedBody": {{ _.urlencodedBody }}, "gqlBody": {{ _.gqlBody }}, "fileBody": {{ _.fileBody }}, "formdataBody": {{ _.formdataBody }} }', |
| 312 | + customVerify: (bodyJson: any) => { |
| 313 | + const reqBodyJsons = JSON.parse(bodyJson.data); |
| 314 | + expect(reqBodyJsons.rawBody.data).toEqual('rawContent'); |
| 315 | + expect(reqBodyJsons.urlencodedBody.data).toEqual('k1=v1&k2=v2'); |
| 316 | + expect(JSON.parse(reqBodyJsons.gqlBody.data)).toEqual({ |
| 317 | + query: 'query', |
| 318 | + operationName: 'operation', |
| 319 | + variables: 'var', |
| 320 | + }); |
| 321 | + expect(reqBodyJsons.fileBody.data).toEqual('raw file content'); |
| 322 | + expect(reqBodyJsons.formdataBody.data).toEqual('--X-INSOMNIA-BOUNDARY\r\nContent-Disposition: form-data; name=\"k1\"\r\n\r\nv1\r\n--X-INSOMNIA-BOUNDARY\r\nContent-Disposition: form-data; name=\"k2\"; filename=\"rawfile.txt\"\r\nContent-Type: text/plain\r\n\r\nraw file content\r\n--X-INSOMNIA-BOUNDARY--\r\n'); |
| 323 | + }, |
| 324 | + }, |
220 | 325 | ]; |
221 | 326 |
|
222 | 327 | for (let i = 0; i < testCases.length; i++) { |
|
0 commit comments