1 import * as request from 'supertest'
2 import { buildAbsoluteFixturePath } from '../miscs/miscs'
3 import { isAbsolute, join } from 'path'
5 function makeGetRequest (options: {
10 statusCodeExpected?: number
12 if (!options.statusCodeExpected) options.statusCodeExpected = 400
14 const req = request(options.url)
16 .set('Accept', 'application/json')
18 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
19 if (options.query) req.query(options.query)
22 .expect('Content-Type', /json/)
23 .expect(options.statusCodeExpected)
26 function makeDeleteRequest (options: {
30 statusCodeExpected?: number
32 if (!options.statusCodeExpected) options.statusCodeExpected = 400
34 const req = request(options.url)
36 .set('Accept', 'application/json')
38 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
41 .expect('Content-Type', /json/)
42 .expect(options.statusCodeExpected)
45 function makeUploadRequest (options: {
47 method?: 'POST' | 'PUT',
50 fields: { [ fieldName: string ]: any },
51 attaches: { [ attachName: string ]: any | any[] },
52 statusCodeExpected?: number
54 if (!options.statusCodeExpected) options.statusCodeExpected = 400
57 if (options.method === 'PUT') {
58 req = request(options.url).put(options.path)
60 req = request(options.url).post(options.path)
63 req.set('Accept', 'application/json')
65 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
67 Object.keys(options.fields).forEach(field => {
68 const value = options.fields[field]
70 if (Array.isArray(value)) {
71 for (let i = 0; i < value.length; i++) {
72 req.field(field + '[' + i + ']', value[i])
75 req.field(field, value)
79 Object.keys(options.attaches).forEach(attach => {
80 const value = options.attaches[attach]
81 if (Array.isArray(value)) {
82 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
84 req.attach(attach, buildAbsoluteFixturePath(value))
88 return req.expect(options.statusCodeExpected)
91 function makePostBodyRequest (options: {
95 fields?: { [ fieldName: string ]: any },
96 statusCodeExpected?: number
98 if (!options.fields) options.fields = {}
99 if (!options.statusCodeExpected) options.statusCodeExpected = 400
101 const req = request(options.url)
103 .set('Accept', 'application/json')
105 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
107 return req.send(options.fields)
108 .expect(options.statusCodeExpected)
111 function makePutBodyRequest (options: {
115 fields: { [ fieldName: string ]: any },
116 statusCodeExpected?: number
118 if (!options.statusCodeExpected) options.statusCodeExpected = 400
120 const req = request(options.url)
122 .set('Accept', 'application/json')
124 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
126 return req.send(options.fields)
127 .expect(options.statusCodeExpected)
130 function makeHTMLRequest (url: string, path: string) {
133 .set('Accept', 'text/html')
137 function updateAvatarRequest (options: {
144 if (isAbsolute(options.fixture)) {
145 filePath = options.fixture
147 filePath = join(__dirname, '..', '..', 'fixtures', options.fixture)
150 return makeUploadRequest({
153 token: options.accessToken,
155 attaches: { avatarfile: filePath },
156 statusCodeExpected: 200
160 // ---------------------------------------------------------------------------