1 import * as request from 'supertest'
3 function makeGetRequest (url: string, path: string) {
6 .set('Accept', 'application/json')
8 .expect('Content-Type', /json/)
11 function makePostUploadRequest (options: {
15 fields: { [ fieldName: string ]: any },
16 attaches: { [ attachName: string ]: any },
17 statusCodeExpected?: number
19 if (!options.statusCodeExpected) options.statusCodeExpected = 400
21 const req = request(options.url)
23 .set('Accept', 'application/json')
25 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
27 Object.keys(options.fields).forEach(field => {
28 const value = options.fields[field]
30 if (Array.isArray(value)) {
31 for (let i = 0; i < value.length; i++) {
32 req.field(field + '[' + i + ']', value[i])
35 req.field(field, value)
39 Object.keys(options.attaches).forEach(attach => {
40 const value = options.attaches[attach]
41 req.attach(attach, value)
44 return req.expect(options.statusCodeExpected)
47 function makePostBodyRequest (options: {
51 fields: { [ fieldName: string ]: any },
52 statusCodeExpected?: number
54 if (!options.statusCodeExpected) options.statusCodeExpected = 400
56 const req = request(options.url)
58 .set('Accept', 'application/json')
60 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
62 return req.send(options.fields)
63 .expect(options.statusCodeExpected)
66 function makePutBodyRequest (options: {
70 fields: { [ fieldName: string ]: any },
71 statusCodeExpected?: number
73 if (!options.statusCodeExpected) options.statusCodeExpected = 400
75 const req = request(options.url)
77 .set('Accept', 'application/json')
79 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
81 return req.send(options.fields)
82 .expect(options.statusCodeExpected)
85 // ---------------------------------------------------------------------------
89 makePostUploadRequest,