Skip to content

Commit f984b2a

Browse files
committed
rewrite tests
1 parent 4d6205e commit f984b2a

File tree

8 files changed

+144
-43
lines changed

8 files changed

+144
-43
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
language: node_js
22
node_js:
3+
- '6'
34
- '5'
45
- '4'
56
- '0.12'

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
var http = require('http');
44
var urlParse = require('url').parse;
5+
var EventEmitter = require('events').EventEmitter;
56
var humanize = require('humanize-number');
67
var chalk = require('chalk');
78
var monkeypatch = require('monkeypatch');
8-
var EventEmitter = require('events').EventEmitter;
99

1010
var colorCodes = {
1111
5: 'red',

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@
3232
"url-parse-lax": "^1.0.0"
3333
},
3434
"devDependencies": {
35-
"ava": "^0.7.0",
36-
"xo": "^0.11.0"
35+
"ava": "^0.15.0",
36+
"get-port": "^2.1.0",
37+
"got": "^6.3.0",
38+
"pem": "^1.8.3",
39+
"pify": "^2.3.0",
40+
"xo": "^0.15.0"
3741
},
3842
"xo": {
3943
"ignores": [

test/fn.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import test from 'ava';
21
import http from 'http';
32
import https from 'https';
43
import url from 'url';
4+
import test from 'ava';
55
import debugHttp from '../';
66

77
test.cb('calls handler on http request', t => {

test/helpers/server.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
const http = require('http');
3+
const https = require('https');
4+
const pify = require('pify');
5+
const getPort = require('get-port');
6+
const host = exports.host = 'localhost';
7+
8+
exports.createServer = function () {
9+
return getPort().then(port => {
10+
const s = http.createServer((req, resp) => s.emit(req.url, req, resp));
11+
12+
s.host = host;
13+
s.port = port;
14+
s.url = `http://${host}:${port}`;
15+
s.protocol = 'http';
16+
17+
s.listen = pify(s.listen, Promise);
18+
s.close = pify(s.close, Promise);
19+
20+
return s;
21+
});
22+
};
23+
24+
exports.createSSLServer = function (opts) {
25+
return getPort().then(port => {
26+
const s = https.createServer(opts, (req, resp) => s.emit(req.url, req, resp));
27+
28+
s.host = host;
29+
s.port = port;
30+
s.url = `https://${host}:${port}`;
31+
s.protocol = 'https';
32+
33+
s.listen = pify(s.listen, Promise);
34+
s.close = pify(s.close, Promise);
35+
36+
return s;
37+
});
38+
};

test/http.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import test from 'ava';
2+
import got from 'got';
3+
import chalk from 'chalk';
4+
import debugHttp from '../';
5+
import {createServer} from './helpers/server';
6+
7+
let s;
8+
9+
test.before('setup', async () => {
10+
s = await createServer();
11+
12+
s.on('/', (req, res) => res.end('ok'));
13+
14+
await s.listen(s.port);
15+
});
16+
17+
test('http works', async t => {
18+
debugHttp();
19+
20+
let strs = [];
21+
console.log = function (string) {
22+
strs.push(chalk.stripColor(string));
23+
};
24+
25+
await got(s.url);
26+
27+
t.is(strs.length, 2);
28+
t.is(strs[0].indexOf(` → GET ${s.url}`), 0);
29+
t.is(strs[1].indexOf(` 200 ← GET ${s.url}`), 0);
30+
});
31+
32+
test.after('cleanup', async () => {
33+
await s.close();
34+
});

test/https.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import test from 'ava';
2+
import chalk from 'chalk';
3+
import pem from 'pem';
4+
import pify from 'pify';
5+
import got from 'got';
6+
import debugHttp from '../';
7+
import {createSSLServer} from './helpers/server';
8+
9+
let s;
10+
let caRootCert;
11+
12+
const pemP = pify(pem, Promise);
13+
14+
test.before('setup', async () => {
15+
const caKeys = await pemP.createCertificate({days: 1, selfSigned: true});
16+
17+
const caRootKey = caKeys.serviceKey;
18+
caRootCert = caKeys.certificate;
19+
20+
const keys = await pemP.createCertificate({
21+
serviceCertificate: caRootCert,
22+
serviceKey: caRootKey,
23+
serial: Date.now(),
24+
days: 500,
25+
country: '',
26+
state: '',
27+
locality: '',
28+
organization: '',
29+
organizationUnit: '',
30+
commonName: 'sindresorhus.com'
31+
});
32+
33+
const key = keys.clientKey;
34+
const cert = keys.certificate;
35+
36+
s = await createSSLServer({key, cert});
37+
38+
s.on('/', (req, res) => res.end('ok'));
39+
40+
await s.listen(s.port);
41+
});
42+
43+
test('https works', async t => {
44+
debugHttp();
45+
46+
let strs = [];
47+
console.log = function (string) {
48+
strs.push(chalk.stripColor(string));
49+
};
50+
51+
await got(s.url, {
52+
strictSSL: true,
53+
ca: caRootCert,
54+
headers: {host: 'sindresorhus.com'}
55+
});
56+
57+
t.is(strs.length, 2);
58+
t.is(strs[0].indexOf(` → GET ${s.url}`), 0);
59+
});
60+
61+
test.after('cleanup', async () => {
62+
await s.close();
63+
});

test/output.js

-39
This file was deleted.

0 commit comments

Comments
 (0)