1 import * as request from 'supertest'
2 import { buildAbsoluteFixturePath, root } from '../miscs/miscs'
3 import { isAbsolute, join } from 'path'
4 import { parse } from 'url'
6 function makeRawRequest (url: string, statusCodeExpected?: number, range?: string) {
7 const { host, protocol, pathname } = parse(url)
9 return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
12 function makeGetRequest (options: {
17 statusCodeExpected?: number,
21 if (!options.statusCodeExpected) options.statusCodeExpected = 400
22 if (options.contentType === undefined) options.contentType = 'application/json'
24 const req = request(options.url).get(options.path)
26 if (options.contentType) req.set('Accept', options.contentType)
27 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
28 if (options.query) req.query(options.query)
29 if (options.range) req.set('Range', options.range)
31 return req.expect(options.statusCodeExpected)
34 function makeDeleteRequest (options: {
38 statusCodeExpected?: number
40 if (!options.statusCodeExpected) options.statusCodeExpected = 400
42 const req = request(options.url)
44 .set('Accept', 'application/json')
46 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
48 return req.expect(options.statusCodeExpected)
51 function makeUploadRequest (options: {
53 method?: 'POST' | 'PUT',
56 fields: { [ fieldName: string ]: any },
57 attaches: { [ attachName: string ]: any | any[] },
58 statusCodeExpected?: number
60 if (!options.statusCodeExpected) options.statusCodeExpected = 400
63 if (options.method === 'PUT') {
64 req = request(options.url).put(options.path)
66 req = request(options.url).post(options.path)
69 req.set('Accept', 'application/json')
71 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
73 Object.keys(options.fields).forEach(field => {
74 const value = options.fields[field]
76 if (Array.isArray(value)) {
77 for (let i = 0; i < value.length; i++) {
78 req.field(field + '[' + i + ']', value[i])
81 req.field(field, value)
85 Object.keys(options.attaches).forEach(attach => {
86 const value = options.attaches[attach]
87 if (Array.isArray(value)) {
88 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
90 req.attach(attach, buildAbsoluteFixturePath(value))
94 return req.expect(options.statusCodeExpected)
97 function makePostBodyRequest (options: {
101 fields?: { [ fieldName: string ]: any },
102 statusCodeExpected?: number
104 if (!options.fields) options.fields = {}
105 if (!options.statusCodeExpected) options.statusCodeExpected = 400
107 const req = request(options.url)
109 .set('Accept', 'application/json')
111 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
113 return req.send(options.fields)
114 .expect(options.statusCodeExpected)
117 function makePutBodyRequest (options: {
121 fields: { [ fieldName: string ]: any },
122 statusCodeExpected?: number
124 if (!options.statusCodeExpected) options.statusCodeExpected = 400
126 const req = request(options.url)
128 .set('Accept', 'application/json')
130 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
132 return req.send(options.fields)
133 .expect(options.statusCodeExpected)
136 function makeHTMLRequest (url: string, path: string) {
139 .set('Accept', 'text/html')
143 function updateAvatarRequest (options: {
150 if (isAbsolute(options.fixture)) {
151 filePath = options.fixture
153 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
156 return makeUploadRequest({
159 token: options.accessToken,
161 attaches: { avatarfile: filePath },
162 statusCodeExpected: 200
166 // ---------------------------------------------------------------------------