2019-02-10 05:20:37 +07:00
|
|
|
import Request from 'request-promise';
|
|
|
|
import Fetch from 'node-fetch';
|
|
|
|
import Assert from 'assert';
|
|
|
|
import { StatusCodeError } from 'request-promise/errors';
|
|
|
|
|
|
|
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
|
|
|
|
|
2019-04-11 02:27:18 +07:00
|
|
|
export async function GET_ExpectError(url: string, headers: {[key: string]: string}, statusCode: number) {
|
2019-02-10 05:20:37 +07:00
|
|
|
try {
|
|
|
|
await Request.get(url, {
|
|
|
|
json: true,
|
|
|
|
rejectUnauthorized: false,
|
2019-04-11 02:27:18 +07:00
|
|
|
headers: headers,
|
2019-02-10 05:20:37 +07:00
|
|
|
});
|
|
|
|
throw new Error('No response');
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof StatusCodeError) {
|
2019-02-14 05:04:57 +07:00
|
|
|
Assert.equal(e.statusCode, statusCode);
|
2019-02-10 05:20:37 +07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-14 05:04:57 +07:00
|
|
|
// Sent a GET request to the url and expect a 401
|
2019-04-11 02:27:18 +07:00
|
|
|
export async function GET_Expect401(url: string, headers: {[key: string]: string} = {}) {
|
|
|
|
return await GET_ExpectError(url, headers, 401);
|
2019-02-14 05:04:57 +07:00
|
|
|
}
|
|
|
|
|
2019-04-11 02:27:18 +07:00
|
|
|
export async function GET_Expect502(url: string, headers: {[key: string]: string} = {}) {
|
|
|
|
return await GET_ExpectError(url, headers, 502);
|
2019-02-14 05:04:57 +07:00
|
|
|
}
|
|
|
|
|
2019-02-10 05:20:37 +07:00
|
|
|
export async function POST_Expect401(url: string, body?: any) {
|
|
|
|
try {
|
|
|
|
await Request.post(url, {
|
|
|
|
json: true,
|
|
|
|
rejectUnauthorized: false,
|
|
|
|
body
|
|
|
|
});
|
|
|
|
throw new Error('No response');
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof StatusCodeError) {
|
|
|
|
Assert.equal(e.statusCode, 401);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-11 02:27:18 +07:00
|
|
|
export async function GET_ExpectRedirect(url: string, redirectionUrl: string, headers: {[key: string]: string} = {}) {
|
|
|
|
const response = await Fetch(url, {redirect: 'manual', headers: headers});
|
2019-02-10 05:20:37 +07:00
|
|
|
|
|
|
|
if (response.status == 302) {
|
|
|
|
const body = await response.text();
|
|
|
|
Assert.equal(body, 'Found. Redirecting to ' + redirectionUrl);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('No redirect');
|
|
|
|
}
|