From 92ed73ad7f2a9ab3bb582a208f9f8eb7623092f5 Mon Sep 17 00:00:00 2001 From: Patrick Poulain Date: Sun, 15 Oct 2017 11:25:07 +0200 Subject: [PATCH] Add test to ensure that HTTP request errors are catched (#384) --- package.json | 1 + test/error.js | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/package.json b/package.json index 46a5f37..4e4077b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/error.js b/test/error.js index 0d819b0..9a8d17c 100644 --- a/test/error.js +++ b/test/error.js @@ -1,4 +1,6 @@ +import http from 'http'; import test from 'ava'; +import sinon from 'sinon'; import got from '..'; import {createServer} from './helpers/server'; @@ -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(); });