Skip to content

Commit

Permalink
Merge 7ce26ea into 0e0eefd
Browse files Browse the repository at this point in the history
  • Loading branch information
Giotino committed Sep 13, 2020
2 parents 0e0eefd + 7ce26ea commit a51ccfd
Show file tree
Hide file tree
Showing 15 changed files with 254 additions and 164 deletions.
42 changes: 37 additions & 5 deletions .travis.yml
@@ -1,8 +1,40 @@
language: node_js
node_js:
- '14'
- '13'
- '12'
- '10'

after_success:
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'

jobs:
include:
- os: linux
dist: focal
node_js: '14'
- os: linux
dist: focal
node_js: '12'
- os: linux
dist: focal
node_js: '10'
- os: linux
dist: bionic
node_js: '14'
- os: linux
dist: bionic
node_js: '12'
- os: linux
dist: bionic
node_js: '10'
- os: windows
node_js: '14'
- os: windows
node_js: '12'
- os: windows
node_js: '10'
- os: osx
osx_image: xcode12
node_js: '14'
- os: osx
osx_image: xcode12
node_js: '12'
- os: osx
osx_image: xcode12
node_js: '10'
137 changes: 67 additions & 70 deletions test/agent.ts
@@ -1,35 +1,9 @@
import {Agent as HttpAgent} from 'http';
import {Agent as HttpsAgent} from 'https';
import {Socket} from 'net';
import {TLSSocket} from 'tls';
import test, {Constructor} from 'ava';
import sinon = require('sinon');
import {ExtendedTestServer} from './helpers/types';
import withServer from './helpers/with-server';

const prepareServer = (server: ExtendedTestServer): void => {
server.get('/', (request, response) => {
if (request.socket instanceof TLSSocket) {
response.end('https');
} else {
response.end('http');
}
});

server.get('/httpsToHttp', (_request, response) => {
response.writeHead(302, {
location: server.url
});
response.end();
});

server.get('/httpToHttps', (_request, response) => {
response.writeHead(302, {
location: server.sslUrl
});
response.end();
});
};
import withServer, {withHttpsServer} from './helpers/with-server';

const createAgentSpy = <T extends HttpsAgent>(AgentClass: Constructor): {agent: T; spy: sinon.SinonSpy} => {
const agent: T = new AgentClass({keepAlive: true});
Expand Down Expand Up @@ -59,14 +33,14 @@ test('non-object agent option works with http', withServer, async (t, server, go
agent.destroy();
});

test('non-object agent option works with https', withServer, async (t, server, got) => {
test('non-object agent option works with https', withHttpsServer(), async (t, server, got) => {
server.get('/', (_request, response) => {
response.end('ok');
});

const {agent, spy} = createAgentSpy(HttpsAgent);

t.truthy((await got.secure({
t.truthy((await got({
https: {
rejectUnauthorized: false
},
Expand All @@ -80,53 +54,79 @@ test('non-object agent option works with https', withServer, async (t, server, g
agent.destroy();
});

test('redirects from http to https work with an agent object', withServer, async (t, server, got) => {
prepareServer(server);
test('redirects from http to https work with an agent object', withServer, async (t, serverHttp) => {
await withHttpsServer()(t, async (t, serverHttps, got) => {
serverHttp.get('/', (_request, response) => {
response.end('http');
});

const {agent: httpAgent, spy: httpSpy} = createAgentSpy(HttpAgent);
const {agent: httpsAgent, spy: httpsSpy} = createAgentSpy<HttpsAgent>(HttpsAgent);
serverHttps.get('/', (_request, response) => {
response.end('https');
});

t.truthy((await got('httpToHttps', {
https: {
rejectUnauthorized: false
},
agent: {
http: httpAgent,
https: httpsAgent
}
})).body);
t.true(httpSpy.calledOnce);
t.true(httpsSpy.calledOnce);
serverHttp.get('/httpToHttps', (_request, response) => {
response.writeHead(302, {
location: serverHttps.url
});
response.end();
});

// Make sure to close all open sockets
httpAgent.destroy();
httpsAgent.destroy();
const {agent: httpAgent, spy: httpSpy} = createAgentSpy(HttpAgent);
const {agent: httpsAgent, spy: httpsSpy} = createAgentSpy<HttpsAgent>(HttpsAgent);

t.truthy((await got('httpToHttps', {
prefixUrl: serverHttp.url,
agent: {
http: httpAgent,
https: httpsAgent
}
})).body);
t.true(httpSpy.calledOnce);
t.true(httpsSpy.calledOnce);

// Make sure to close all open sockets
httpAgent.destroy();
httpsAgent.destroy();
});
});

test('redirects from https to http work with an agent object', withServer, async (t, server, got) => {
prepareServer(server);
test('redirects from https to http work with an agent object', withHttpsServer(), async (t, serverHttps, got) => {
await withServer(t, async (t, serverHttp) => {
serverHttp.get('/', (_request, response) => {
response.end('http');
});

const {agent: httpAgent, spy: httpSpy} = createAgentSpy(HttpAgent);
const {agent: httpsAgent, spy: httpsSpy} = createAgentSpy(HttpsAgent);
serverHttps.get('/', (_request, response) => {
response.end('https');
});

t.truthy((await got.secure('httpsToHttp', {
https: {
rejectUnauthorized: false
},
agent: {
http: httpAgent,
https: httpsAgent
}
})).body);
t.true(httpSpy.calledOnce);
t.true(httpsSpy.calledOnce);
serverHttps.get('/httpsToHttp', (_request, response) => {
response.writeHead(302, {
location: serverHttp.url
});
response.end();
});

// Make sure to close all open sockets
httpAgent.destroy();
httpsAgent.destroy();
const {agent: httpAgent, spy: httpSpy} = createAgentSpy(HttpAgent);
const {agent: httpsAgent, spy: httpsSpy} = createAgentSpy(HttpsAgent);

t.truthy((await got('httpsToHttp', {
prefixUrl: serverHttps.url,
agent: {
http: httpAgent,
https: httpsAgent
}
})).body);
t.true(httpSpy.calledOnce);
t.true(httpsSpy.calledOnce);

// Make sure to close all open sockets
httpAgent.destroy();
httpsAgent.destroy();
});
});

test('socket connect listener cleaned up after request', withServer, async (t, server, got) => {
test('socket connect listener cleaned up after request', withHttpsServer(), async (t, server, got) => {
server.get('/', (_request, response) => {
response.end('ok');
});
Expand All @@ -136,10 +136,7 @@ test('socket connect listener cleaned up after request', withServer, async (t, s
// Make sure there are no memory leaks when reusing keep-alive sockets
for (let i = 0; i < 20; i++) {
// eslint-disable-next-line no-await-in-loop
await got.secure({
https: {
rejectUnauthorized: false
},
await got({
agent: {
https: agent
}
Expand Down
5 changes: 3 additions & 2 deletions test/cancel.ts
Expand Up @@ -8,10 +8,11 @@ import getStream = require('get-stream');
import {Handler} from 'express';
import got, {CancelError} from '../source';
import slowDataStream from './helpers/slow-data-stream';
import {ExtendedTestServer, GlobalClock} from './helpers/types';
import {GlobalClock} from './helpers/types';
import {ExtendedHttpTestServer} from './helpers/create-http-test-server';
import withServer, {withServerAndFakeTimers} from './helpers/with-server';

const prepareServer = (server: ExtendedTestServer, clock: GlobalClock): {emitter: EventEmitter; promise: Promise<unknown>} => {
const prepareServer = (server: ExtendedHttpTestServer, clock: GlobalClock): {emitter: EventEmitter; promise: Promise<unknown>} => {
const emitter = new EventEmitter();

const promise = new Promise((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion test/error.ts
Expand Up @@ -36,7 +36,7 @@ test('properties', withServer, async (t, server, got) => {
test('catches dns errors', async t => {
const error = await t.throwsAsync<RequestError>(got('http://doesntexist', {retry: 0}));
t.truthy(error);
t.regex(error.message, /ENOTFOUND/);
t.regex(error.message, /ENOTFOUND|EAI_AGAIN/);
t.is(error.options.url.host, 'doesntexist');
t.is(error.options.method, 'GET');
});
Expand Down
43 changes: 43 additions & 0 deletions test/helpers/create-http-test-server.ts
@@ -0,0 +1,43 @@
import http = require('http');
import net = require('net');
import express = require('express');
import pify = require('pify');
import bodyParser = require('body-parser');

export type HttpServerOptions = {
bodyParser?: express.NextFunction | false;
};

export interface ExtendedHttpTestServer extends express.Express {
http: http.Server;
url: string;
port: number;
hostname: string;
close: () => Promise<any>;
}

const createHttpTestServer = async (options: HttpServerOptions = {}): Promise<ExtendedHttpTestServer> => {
const server = express() as ExtendedHttpTestServer;
server.http = http.createServer(server);

server.set('etag', false);

if (options.bodyParser !== false) {
server.use(bodyParser.json({limit: '1mb', type: 'application/json', ...options.bodyParser}));
server.use(bodyParser.text({limit: '1mb', type: 'text/plain', ...options.bodyParser}));
server.use(bodyParser.urlencoded({limit: '1mb', type: 'application/x-www-form-urlencoded', extended: true, ...options.bodyParser}));
server.use(bodyParser.raw({limit: '1mb', type: 'application/octet-stream', ...options.bodyParser}));
}

await pify(server.http.listen.bind(server.http))();

server.port = (server.http.address() as net.AddressInfo).port;
server.url = `http://localhost:${(server.port)}`;
server.hostname = 'localhost';

server.close = async () => pify(server.http.close.bind(server.http))();

return server;
};

export default createHttpTestServer;
3 changes: 1 addition & 2 deletions test/helpers/create-https-test-server.ts
Expand Up @@ -63,8 +63,7 @@ const createHttpsTestServer = async (options: HttpsServerOptions = {}): Promise<
server.port = (server.https.address() as net.AddressInfo).port;
server.url = `https://localhost:${(server.port)}`;

server.close = async () =>
pify(server.https.close.bind(server.https))();
server.close = async () => pify(server.https.close.bind(server.https))();

return server;
};
Expand Down
5 changes: 0 additions & 5 deletions test/helpers/types.ts
@@ -1,11 +1,6 @@
import {Server} from 'http';
import {TestServer} from 'create-test-server';
import * as FakeTimers from '@sinonjs/fake-timers';
import {Got} from '../../source';

export interface ExtendedGot extends Got {
secure: Got;
}

export interface ExtendedHttpServer extends Server {
socketPath: string;
Expand Down

0 comments on commit a51ccfd

Please sign in to comment.