@bbc/http-transport
Advanced tools
Comparing version 4.0.0 to 4.0.1
{ | ||
"name": "@bbc/http-transport", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"description": "A flexible, modular REST client built for ease-of-use and resilience.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -20,3 +20,4 @@ 'use strict'; | ||
const simpleResponseBody = 'Illegitimi non carborundum'; | ||
const responseBody = 'Illegitimi non carborundum'; | ||
const JSONResponseBody = { body: 'Illegitimi non carborundum' }; | ||
const requestBody = { | ||
@@ -28,3 +29,6 @@ foo: 'bar' | ||
}; | ||
const responseBody = requestBody; | ||
const jsonHeader = { | ||
'Content-Type': 'application/json' | ||
}; | ||
const postResponseBody = requestBody; | ||
@@ -43,4 +47,4 @@ function createContext(url, method) { | ||
nock.cleanAll(); | ||
api.get(path).reply(200, simpleResponseBody, header); | ||
httpsApi.get(path).reply(200, simpleResponseBody, header); | ||
api.get(path).reply(200, responseBody, header); | ||
httpsApi.get(path).reply(200, responseBody, header); | ||
}); | ||
@@ -61,3 +65,3 @@ | ||
assert.equal(ctx.res.statusCode, 200); | ||
assert.equal(ctx.res.body, simpleResponseBody); | ||
assert.equal(ctx.res.body, responseBody); | ||
}); | ||
@@ -74,3 +78,3 @@ }); | ||
.get(path) | ||
.reply(200, simpleResponseBody, header); | ||
.reply(200, responseBody, header); | ||
@@ -86,3 +90,3 @@ const ctx = createContext(url); | ||
assert.equal(ctx.res.statusCode, 200); | ||
assert.equal(ctx.res.body, simpleResponseBody); | ||
assert.equal(ctx.res.body, responseBody); | ||
}); | ||
@@ -92,3 +96,3 @@ }); | ||
it('makes a GET request with query strings', () => { | ||
api.get('/?a=1').reply(200, simpleResponseBody, header); | ||
api.get('/?a=1').reply(200, responseBody, header); | ||
@@ -104,3 +108,3 @@ const ctx = createContext(url); | ||
assert.equal(ctx.res.statusCode, 200); | ||
assert.equal(ctx.res.body, simpleResponseBody); | ||
assert.equal(ctx.res.body, responseBody); | ||
}); | ||
@@ -137,5 +141,5 @@ }); | ||
it('makes a PUT request with a JSON body', () => { | ||
api.put(path, requestBody).reply(201, responseBody); | ||
const ctx = createContext(url, 'put'); | ||
it('makes a POST request with a JSON body', () => { | ||
api.post(path, requestBody).reply(201, postResponseBody); | ||
const ctx = createContext(url, 'post'); | ||
ctx.req.body(requestBody); | ||
@@ -148,9 +152,9 @@ | ||
assert.equal(ctx.res.statusCode, 201); | ||
assert.deepEqual(ctx.res.body, responseBody); | ||
assert.deepEqual(ctx.res.body, postResponseBody); | ||
}); | ||
}); | ||
it('makes a POST request with a JSON body', () => { | ||
api.post(path, requestBody).reply(201, responseBody); | ||
const ctx = createContext(url, 'post'); | ||
it('makes a PUT request with a JSON body', () => { | ||
api.put(path, requestBody).reply(201, postResponseBody); | ||
const ctx = createContext(url, 'put'); | ||
ctx.req.body(requestBody); | ||
@@ -163,3 +167,3 @@ | ||
assert.equal(ctx.res.statusCode, 201); | ||
assert.deepEqual(ctx.res.body, responseBody); | ||
assert.deepEqual(ctx.res.body, postResponseBody); | ||
}); | ||
@@ -199,3 +203,3 @@ }); | ||
.delay(500) | ||
.reply(200, simpleResponseBody); | ||
.reply(200, responseBody); | ||
@@ -221,3 +225,3 @@ const ctx = createContext(url); | ||
.delay(500) | ||
.reply(200, simpleResponseBody); | ||
.reply(200, responseBody); | ||
@@ -243,3 +247,3 @@ const ctx = createContext(url); | ||
nock.cleanAll(); | ||
api.get('/').reply(200, simpleResponseBody); | ||
api.get('/').reply(200, responseBody); | ||
@@ -257,2 +261,53 @@ const ctx = createContext(url); | ||
it('if json true option is passed in, parse body as json', () => { | ||
nock.cleanAll(); | ||
api.get(path).reply(200, JSONResponseBody); | ||
const ctx = createContext(url); | ||
const options = { | ||
defaults: { | ||
json: true | ||
} | ||
}; | ||
const fetchTransport = new FetchTransport(options); | ||
return fetchTransport | ||
.execute(ctx) | ||
.catch(assert.ifError) | ||
.then(() => { | ||
assert.typeOf(ctx.res.body, 'object', 'we have an object'); | ||
}); | ||
}); | ||
it('if there is no json option passed, but the header includes json, then parse body as json', () => { | ||
nock.cleanAll(); | ||
api.get(path).reply(200, JSONResponseBody, jsonHeader); | ||
const ctx = createContext(url); | ||
const fetchTransport = new FetchTransport(); | ||
return fetchTransport | ||
.execute(ctx) | ||
.catch(assert.ifError) | ||
.then(() => { | ||
assert.typeOf(ctx.res.body, 'object', 'we have an object'); | ||
}); | ||
}); | ||
it('if there is no json option passed, and no json header, then parse body as text', () => { | ||
nock.cleanAll(); | ||
api.get(path).reply(200, responseBody); | ||
const ctx = createContext(url); | ||
const fetchTransport = new FetchTransport(); | ||
return fetchTransport | ||
.execute(ctx) | ||
.catch(assert.ifError) | ||
.then(() => { | ||
assert.typeOf(ctx.res.body, 'string', 'we have text'); | ||
}); | ||
}); | ||
it('selects httpAgent when protocol is http and agent options have been provided', () => { | ||
@@ -259,0 +314,0 @@ const ctx = createContext(url); |
382709
6667