Skip to content

Commit

Permalink
Add test to ensure that HTTP request errors are catched (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
petitchevalroux authored and sindresorhus committed Oct 15, 2017
1 parent 3c79205 commit 92ed73a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"get-port": "^3.0.0",
"nyc": "^11.0.2",
"pem": "^1.4.4",
"sinon": "^4.0.0",
"slow-stream": "0.0.4",
"tempfile": "^2.0.0",
"tempy": "^0.1.0",
Expand Down
12 changes: 12 additions & 0 deletions test/error.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import http from 'http';
import test from 'ava';
import sinon from 'sinon';
import got from '..';
import {createServer} from './helpers/server';

Expand Down Expand Up @@ -43,6 +45,16 @@ test('options.body error message', async t => {
t.regex(err.message, /options\.body must be a ReadableStream, string, Buffer or plain Object/);
});

test.serial('http.request error', async t => {
const stub = sinon.stub(http, 'request').callsFake(() => {
throw new TypeError('The header content contains invalid characters');
});
const err = await t.throws(got(s.url));
t.truthy(err instanceof TypeError);
t.regex(err.message, /The header content contains invalid characters/);
stub.restore();
});

test.after('cleanup', async () => {
await s.close();
});

0 comments on commit 92ed73a

Please sign in to comment.