2017-09-04 14:21:47 -05:00
|
|
|
import * as request from 'supertest'
|
2018-02-13 11:17:05 -06:00
|
|
|
import { buildAbsoluteFixturePath } from '../'
|
2017-09-04 14:21:47 -05:00
|
|
|
|
2017-12-28 07:29:57 -06:00
|
|
|
function makeGetRequest (options: {
|
|
|
|
url: string,
|
|
|
|
path: string,
|
|
|
|
query?: any,
|
|
|
|
token?: string,
|
|
|
|
statusCodeExpected?: number
|
|
|
|
}) {
|
|
|
|
if (!options.statusCodeExpected) options.statusCodeExpected = 400
|
|
|
|
|
|
|
|
const req = request(options.url)
|
|
|
|
.get(options.path)
|
2017-09-04 14:21:47 -05:00
|
|
|
.set('Accept', 'application/json')
|
2017-12-28 07:29:57 -06:00
|
|
|
|
|
|
|
if (options.token) req.set('Authorization', 'Bearer ' + options.token)
|
|
|
|
if (options.query) req.query(options.query)
|
|
|
|
|
|
|
|
return req
|
2017-09-04 14:21:47 -05:00
|
|
|
.expect('Content-Type', /json/)
|
2017-12-28 07:29:57 -06:00
|
|
|
.expect(options.statusCodeExpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
return req
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.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,
|
|
|
|
token: string,
|
|
|
|
fields: { [ fieldName: string ]: any },
|
|
|
|
attaches: { [ attachName: string ]: any },
|
|
|
|
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-02-13 11:17:05 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
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,
|
|
|
|
makeDeleteRequest
|
2017-09-04 14:21:47 -05:00
|
|
|
}
|