mirror of https://github.com/lukechilds/got.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
2.8 KiB
132 lines
2.8 KiB
import test from 'ava';
|
|
import pem from 'pem';
|
|
import pify from 'pify';
|
|
import got from '../';
|
|
import {createServer, createSSLServer} from './helpers/server';
|
|
|
|
let http;
|
|
let https;
|
|
let key;
|
|
let cert;
|
|
let caRootKey;
|
|
let caRootCert;
|
|
|
|
const pemP = pify(pem, Promise);
|
|
|
|
test.before('setup', async () => {
|
|
const caKeys = await pemP.createCertificate({days: 1, selfSigned: true});
|
|
|
|
caRootKey = caKeys.serviceKey;
|
|
caRootCert = caKeys.certificate;
|
|
|
|
const keys = await pemP.createCertificate({
|
|
serviceCertificate: caRootCert,
|
|
serviceKey: caRootKey,
|
|
serial: Date.now(),
|
|
days: 500,
|
|
country: '',
|
|
state: '',
|
|
locality: '',
|
|
organization: '',
|
|
organizationUnit: '',
|
|
commonName: 'sindresorhus.com'
|
|
});
|
|
|
|
key = keys.clientKey;
|
|
cert = keys.certificate;
|
|
|
|
https = await createSSLServer({key, cert});
|
|
|
|
https.on('/', (req, res) => {
|
|
res.end('https');
|
|
});
|
|
|
|
http = await createServer();
|
|
|
|
http.on('/', (req, res) => {
|
|
res.end('reached');
|
|
});
|
|
|
|
http.on('/finite', (req, res) => {
|
|
res.writeHead(302, {
|
|
location: `${http.url}/`
|
|
});
|
|
res.end();
|
|
});
|
|
|
|
http.on('/endless', (req, res) => {
|
|
res.writeHead(302, {
|
|
location: `${http.url}/endless`
|
|
});
|
|
res.end();
|
|
});
|
|
|
|
http.on('/relative', (req, res) => {
|
|
res.writeHead(302, {
|
|
location: '/'
|
|
});
|
|
res.end();
|
|
});
|
|
|
|
http.on('/relativeQuery?bang', (req, res) => {
|
|
res.writeHead(302, {
|
|
location: '/'
|
|
});
|
|
res.end();
|
|
});
|
|
|
|
http.on('/httpToHttps', (req, res) => {
|
|
res.writeHead(302, {
|
|
location: https.url
|
|
});
|
|
res.end();
|
|
});
|
|
|
|
await http.listen(http.port);
|
|
await https.listen(https.port);
|
|
});
|
|
|
|
test('follows redirect', async t => {
|
|
t.is((await got(`${http.url}/finite`)).body, 'reached');
|
|
});
|
|
|
|
test('relative redirect works', async t => {
|
|
t.is((await got(`${http.url}/relative`)).body, 'reached');
|
|
});
|
|
|
|
test('throws on endless redirect', async t => {
|
|
try {
|
|
await got(`${http.url}/endless`);
|
|
t.fail('Exception was not thrown');
|
|
} catch (err) {
|
|
t.is(err.message, 'Redirected 10 times. Aborting.');
|
|
}
|
|
});
|
|
|
|
test('query in options are not breaking redirects', async t => {
|
|
t.is((await got(`${http.url}/relativeQuery`, {query: 'bang'})).body, 'reached');
|
|
});
|
|
|
|
test('hostname+path in options are not breaking redirects', async t => {
|
|
t.is((await got(`${http.url}/relative`, {hostname: http.host, path: '/relative'})).body, 'reached');
|
|
});
|
|
|
|
test('redirect only GET and HEAD requests', async t => {
|
|
try {
|
|
await got(`${http.url}/relative`, {body: 'wow'});
|
|
t.fail('Exception was not thrown');
|
|
} catch (err) {
|
|
t.is(err.message, 'Response code 302 (Found)');
|
|
t.is(err.path, '/relative');
|
|
t.is(err.statusCode, 302);
|
|
}
|
|
});
|
|
|
|
test('redirects from http to https works', async t => {
|
|
t.ok((await got(`${http.url}/httpToHttps`, {rejectUnauthorized: false})).body);
|
|
});
|
|
|
|
test.after('cleanup', async () => {
|
|
await http.close();
|
|
await https.close();
|
|
});
|
|
|