2017-09-04 14:21:47 -05:00
|
|
|
import * as request from 'supertest'
|
2018-12-07 09:09:57 -06:00
|
|
|
import { buildAbsoluteFixturePath, root } from '../miscs/miscs'
|
2018-06-29 04:29:23 -05:00
|
|
|
import { isAbsolute, join } from 'path'
|
2019-01-29 01:37:25 -06:00
|
|
|
import { parse } from 'url'
|
|
|
|
|
2019-02-07 08:08:19 -06:00
|
|
|
function makeRawRequest (url: string, statusCodeExpected?: number, range?: string) {
|
2019-01-29 01:37:25 -06:00
|
|
|
const { host, protocol, pathname } = parse(url)
|
|
|
|
|
2019-02-07 08:08:19 -06:00
|
|
|
return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
|
2019-01-29 01:37:25 -06:00
|
|
|
}
|
2017-09-04 14:21:47 -05:00
|
|
|
|
2017-12-28 07:29:57 -06:00
|
|
|
function makeGetRequest (options: {
|
|
|
|
url: string,
|
2019-01-29 01:37:25 -06:00
|
|
|
path?: string,
|
2017-12-28 07:29:57 -06:00
|
|
|
query?: any,
|
|
|
|
token?: string,
|
2018-09-25 09:22:48 -05:00
|
|
|
statusCodeExpected?: number,
|
2019-02-07 08:08:19 -06:00
|
|
|
contentType?: string,
|
|
|
|
range?: string
|
2017-12-28 07:29:57 -06:00
|
|
|
}) {
|
|
|
|
if (!options.statusCodeExpected) options.statusCodeExpected = 400
|
2018-09-25 09:22:48 -05:00
|
|
|
if (options.contentType === undefined) options.contentType = 'application/json'
|
2017-12-28 07:29:57 -06:00
|
|
|
|
2019-01-29 01:37:25 -06:00
|
|
|
const req = request(options.url).get(options.path)
|
2017-12-28 07:29:57 -06:00
|
|
|
|
2018-09-25 09:22:48 -05:00
|
|
|
if (options.contentType) req.set('Accept', options.contentType)
|
2017-12-28 07:29:57 -06:00
|
|
|
if (options.token) req.set('Authorization', 'Bearer ' + options.token)
|
|
|
|
if (options.query) req.query(options.query)
|
2019-02-07 08:08:19 -06:00
|
|
|
if (options.range) req.set('Range', options.range)
|
2017-12-28 07:29:57 -06:00
|
|
|
|
2018-09-25 09:22:48 -05:00
|
|
|
return req.expect(options.statusCodeExpected)
|
2017-12-28 07:29:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeDeleteRequest (options: {
|
|
|
|
url: string,
|
|
|
|
path: string,
|
|
|
|
token?: string,
|
|
|
|
statusCodeExpected?: number
|
|
|
|
}) {
|
|
|
|
if (!options.statusCodeExpected) options.statusCodeExpected = 400
|
|
|
|
|
|
|
|
const req = request(options.url)
|
|
|
|
.delete(options.path)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
|
|
|
|
if (options.token) req.set('Authorization', 'Bearer ' + options.token)
|
|
|
|
|
2018-10-12 08:26:04 -05:00
|
|
|
return req.expect(options.statusCodeExpected)
|
2017-09-04 14:21:47 -05:00
|
|
|
}
|
|
|
|
|
2018-02-13 11:17:05 -06:00
|
|
|
function makeUploadRequest (options: {
|
2017-09-04 14:21:47 -05:00
|
|
|
url: string,
|
2018-02-13 11:17:05 -06:00
|
|
|
method?: 'POST' | 'PUT',
|
2017-09-04 14:21:47 -05:00
|
|
|
path: string,
|
2018-06-29 04:29:23 -05:00
|
|
|
token?: string,
|
2017-09-04 14:21:47 -05:00
|
|
|
fields: { [ fieldName: string ]: any },
|
2018-08-06 04:45:24 -05:00
|
|
|
attaches: { [ attachName: string ]: any | any[] },
|
2017-09-04 14:21:47 -05:00
|
|
|
statusCodeExpected?: number
|
|
|
|
}) {
|
|
|
|
if (!options.statusCodeExpected) options.statusCodeExpected = 400
|
|
|
|
|
2018-02-13 11:17:05 -06:00
|
|
|
let req: request.Test
|
|
|
|
if (options.method === 'PUT') {
|
|
|
|
req = request(options.url).put(options.path)
|
|
|
|
} else {
|
|
|
|
req = request(options.url).post(options.path)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.set('Accept', 'application/json')
|
2017-09-04 14:21:47 -05:00
|
|
|
|
|
|
|
if (options.token) req.set('Authorization', 'Bearer ' + options.token)
|
|
|
|
|
|
|
|
Object.keys(options.fields).forEach(field => {
|
|
|
|
const value = options.fields[field]
|
|
|
|
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
for (let i = 0; i < value.length; i++) {
|
|
|
|
req.field(field + '[' + i + ']', value[i])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
req.field(field, value)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
Object.keys(options.attaches).forEach(attach => {
|
|
|
|
const value = options.attaches[attach]
|
2018-08-06 04:45:24 -05:00
|
|
|
if (Array.isArray(value)) {
|
|
|
|
req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
|
|
|
|
} else {
|
|
|
|
req.attach(attach, buildAbsoluteFixturePath(value))
|
|
|
|
}
|
2017-09-04 14:21:47 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
return req.expect(options.statusCodeExpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
function makePostBodyRequest (options: {
|
|
|
|
url: string,
|
|
|
|
path: string,
|
|
|
|
token?: string,
|
2017-12-28 07:29:57 -06:00
|
|
|
fields?: { [ fieldName: string ]: any },
|
2017-09-04 14:21:47 -05:00
|
|
|
statusCodeExpected?: number
|
|
|
|
}) {
|
2017-12-28 07:29:57 -06:00
|
|
|
if (!options.fields) options.fields = {}
|
2017-09-04 14:21:47 -05:00
|
|
|
if (!options.statusCodeExpected) options.statusCodeExpected = 400
|
|
|
|
|
|
|
|
const req = request(options.url)
|
|
|
|
.post(options.path)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
|
|
|
|
if (options.token) req.set('Authorization', 'Bearer ' + options.token)
|
|
|
|
|
|
|
|
return req.send(options.fields)
|
|
|
|
.expect(options.statusCodeExpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
function makePutBodyRequest (options: {
|
|
|
|
url: string,
|
|
|
|
path: string,
|
2018-01-17 03:32:03 -06:00
|
|
|
token?: string,
|
2017-09-04 14:21:47 -05:00
|
|
|
fields: { [ fieldName: string ]: any },
|
|
|
|
statusCodeExpected?: number
|
|
|
|
}) {
|
|
|
|
if (!options.statusCodeExpected) options.statusCodeExpected = 400
|
|
|
|
|
|
|
|
const req = request(options.url)
|
|
|
|
.put(options.path)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
|
|
|
|
if (options.token) req.set('Authorization', 'Bearer ' + options.token)
|
|
|
|
|
|
|
|
return req.send(options.fields)
|
|
|
|
.expect(options.statusCodeExpected)
|
|
|
|
}
|
|
|
|
|
2018-07-18 02:52:46 -05:00
|
|
|
function makeHTMLRequest (url: string, path: string) {
|
|
|
|
return request(url)
|
|
|
|
.get(path)
|
|
|
|
.set('Accept', 'text/html')
|
|
|
|
.expect(200)
|
|
|
|
}
|
|
|
|
|
2018-06-29 04:29:23 -05:00
|
|
|
function updateAvatarRequest (options: {
|
|
|
|
url: string,
|
|
|
|
path: string,
|
|
|
|
accessToken: string,
|
|
|
|
fixture: string
|
|
|
|
}) {
|
|
|
|
let filePath = ''
|
|
|
|
if (isAbsolute(options.fixture)) {
|
|
|
|
filePath = options.fixture
|
|
|
|
} else {
|
2018-12-07 09:09:57 -06:00
|
|
|
filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
|
2018-06-29 04:29:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return makeUploadRequest({
|
|
|
|
url: options.url,
|
|
|
|
path: options.path,
|
|
|
|
token: options.accessToken,
|
|
|
|
fields: {},
|
|
|
|
attaches: { avatarfile: filePath },
|
|
|
|
statusCodeExpected: 200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-04 14:21:47 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2018-07-18 02:52:46 -05:00
|
|
|
makeHTMLRequest,
|
2017-09-04 14:21:47 -05:00
|
|
|
makeGetRequest,
|
2018-02-13 11:17:05 -06:00
|
|
|
makeUploadRequest,
|
2017-09-04 14:21:47 -05:00
|
|
|
makePostBodyRequest,
|
2017-12-28 07:29:57 -06:00
|
|
|
makePutBodyRequest,
|
2018-06-29 04:29:23 -05:00
|
|
|
makeDeleteRequest,
|
2019-01-29 01:37:25 -06:00
|
|
|
makeRawRequest,
|
2018-06-29 04:29:23 -05:00
|
|
|
updateAvatarRequest
|
2017-09-04 14:21:47 -05:00
|
|
|
}
|