Skip to content

Commit

Permalink
test: DRY createSSLServer setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Stewmon committed Jul 25, 2018
1 parent 11a2f0f commit 344cb3f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 92 deletions.
30 changes: 1 addition & 29 deletions test/agent.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,15 @@
import util from 'util';
import {Agent as HttpAgent} from 'http';
import {Agent as HttpsAgent} from 'https';
import test from 'ava';
import pem from 'pem';
import sinon from 'sinon';
import got from '../source';
import {createServer, createSSLServer} from './helpers/server';

let http;
let https;

const createCertificate = util.promisify(pem.createCertificate);

test.before('setup', async () => {
const caKeys = await createCertificate({
days: 1,
selfSigned: true
});

const caRootKey = caKeys.serviceKey;
const caRootCert = caKeys.certificate;

const keys = await createCertificate({
serviceCertificate: caRootCert,
serviceKey: caRootKey,
serial: Date.now(),
days: 500,
country: '',
state: '',
locality: '',
organization: '',
organizationUnit: '',
commonName: 'sindresorhus.com'
});

const key = keys.clientKey;
const cert = keys.certificate;

https = await createSSLServer({key, cert}); // eslint-disable-line object-property-newline
https = await createSSLServer();
http = await createServer();

// HTTPS Handlers
Expand Down
32 changes: 30 additions & 2 deletions test/helpers/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ const util = require('util');
const http = require('http');
const https = require('https');
const getPort = require('get-port');
const pem = require('pem');

exports.host = 'localhost';
const {host} = exports;

const createCertificate = util.promisify(pem.createCertificate);

exports.createServer = async () => {
const port = await getPort();

Expand All @@ -25,17 +28,42 @@ exports.createServer = async () => {
return s;
};

exports.createSSLServer = async options => {
exports.createSSLServer = async () => {
const port = await getPort();

const s = https.createServer(options, (request, response) => {
const caKeys = await createCertificate({
days: 1,
selfSigned: true
});

const caRootKey = caKeys.serviceKey;
const caRootCert = caKeys.certificate;

const keys = await createCertificate({
serviceCertificate: caRootCert,
serviceKey: caRootKey,
serial: Date.now(),
days: 500,
country: '',
state: '',
locality: '',
organization: '',
organizationUnit: '',
commonName: 'sindresorhus.com'
});

const key = keys.clientKey;
const cert = keys.certificate;

const s = https.createServer({cert, key}, (request, response) => {
s.emit(request.url, request, response);
});

s.host = host;
s.port = port;
s.url = `https://${host}:${port}`;
s.protocol = 'https';
s.caRootCert = caRootCert;

s.listen = util.promisify(s.listen);
s.close = util.promisify(s.close);
Expand Down
35 changes: 3 additions & 32 deletions test/https.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,11 @@
import util from 'util';
import test from 'ava';
import pem from 'pem';
import got from '../source';
import {createSSLServer} from './helpers/server';

let s;
let caRootCert;

const createCertificate = util.promisify(pem.createCertificate);

test.before('setup', async () => {
const caKeys = await createCertificate({
days: 1,
selfSigned: true
});

const caRootKey = caKeys.serviceKey;
caRootCert = caKeys.certificate;

const keys = await createCertificate({
serviceCertificate: caRootCert,
serviceKey: caRootKey,
serial: Date.now(),
days: 500,
country: '',
state: '',
locality: '',
organization: '',
organizationUnit: '',
commonName: 'sindresorhus.com'
});

const key = keys.clientKey;
const cert = keys.certificate;

s = await createSSLServer({key, cert});
s = await createSSLServer();

s.on('/', (request, response) => response.end('ok'));

Expand All @@ -51,15 +22,15 @@ test('make request to https server without ca', async t => {

test('make request to https server with ca', async t => {
const {body} = await got(s.url, {
ca: caRootCert,
ca: s.caRootCert,
headers: {host: 'sindresorhus.com'}
});
t.is(body, 'ok');
});

test('protocol-less URLs default to HTTPS', async t => {
const {body, requestUrl} = await got(s.url.replace(/^https:\/\//, ''), {
ca: caRootCert,
ca: s.caRootCert,
headers: {host: 'sindresorhus.com'}
});
t.is(body, 'ok');
Expand Down
30 changes: 1 addition & 29 deletions test/redirects.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,13 @@
import util from 'util';
import {URL} from 'url';
import test from 'ava';
import pem from 'pem';
import got from '../source';
import {createServer, createSSLServer} from './helpers/server';

let http;
let https;

const createCertificate = util.promisify(pem.createCertificate);

test.before('setup', async () => {
const caKeys = await createCertificate({
days: 1,
selfSigned: true
});

const caRootKey = caKeys.serviceKey;
const caRootCert = caKeys.certificate;

const keys = await createCertificate({
serviceCertificate: caRootCert,
serviceKey: caRootKey,
serial: Date.now(),
days: 500,
country: '',
state: '',
locality: '',
organization: '',
organizationUnit: '',
commonName: 'sindresorhus.com'
});

const key = keys.clientKey;
const cert = keys.certificate;

https = await createSSLServer({key, cert});
https = await createSSLServer();
http = await createServer();

// HTTPS Handlers
Expand Down

0 comments on commit 344cb3f

Please sign in to comment.