Skip to content

Commit

Permalink
test: add unit tests for conversion of response to json
Browse files Browse the repository at this point in the history
  • Loading branch information
vaughan-rich committed Sep 25, 2023
1 parent df2c4c5 commit a5af740
Showing 1 changed file with 75 additions and 20 deletions.
95 changes: 75 additions & 20 deletions test/transport/node-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ const api = nock(host);
const httpsApi = nock(httpsHost);
const path = '/';

const simpleResponseBody = 'Illegitimi non carborundum';
const responseBody = 'Illegitimi non carborundum';
const JSONResponseBody = { body: 'Illegitimi non carborundum' };
const requestBody = {
foo: 'bar'
};
const header = {
'Content-Type': 'text/html'
};
const responseBody = requestBody;
const jsonHeader = {
'Content-Type': 'application/json'
};
const postResponseBody = requestBody;

function createContext(url, method) {
method = method || 'get';
Expand All @@ -38,8 +42,8 @@ describe('Request HTTP transport', () => {
beforeEach(() => {
nock.disableNetConnect();
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);
});

afterEach(() => {
Expand All @@ -55,7 +59,7 @@ describe('Request HTTP transport', () => {
.catch(assert.ifError)
.then((ctx) => {
assert.equal(ctx.res.statusCode, 200);
assert.equal(ctx.res.body, simpleResponseBody);
assert.equal(ctx.res.body, responseBody);
});
});

Expand All @@ -67,7 +71,7 @@ describe('Request HTTP transport', () => {
}
})
.get(path)
.reply(200, simpleResponseBody, header);
.reply(200, responseBody, header);

const ctx = createContext(url);
ctx.req.addHeader('test', 'qui curat');
Expand All @@ -78,12 +82,12 @@ describe('Request HTTP transport', () => {
.catch(assert.ifError)
.then((ctx) => {
assert.equal(ctx.res.statusCode, 200);
assert.equal(ctx.res.body, simpleResponseBody);
assert.equal(ctx.res.body, responseBody);
});
});

it('makes a GET request with query strings', () => {
api.get('/?a=1').reply(200, simpleResponseBody, header);
api.get('/?a=1').reply(200, responseBody, header);

const ctx = createContext(url);
ctx.req.addQuery('a', 1);
Expand All @@ -94,7 +98,7 @@ describe('Request HTTP transport', () => {
.catch(assert.ifError)
.then((ctx) => {
assert.equal(ctx.res.statusCode, 200);
assert.equal(ctx.res.body, simpleResponseBody);
assert.equal(ctx.res.body, responseBody);
});
});

Expand Down Expand Up @@ -126,31 +130,31 @@ describe('Request HTTP transport', () => {
});
});

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);

return new FetchTransport()
.execute(ctx)
.catch(assert.ifError)
.then((ctx) => {
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);

return new FetchTransport()
.execute(ctx)
.catch(assert.ifError)
.then((ctx) => {
assert.equal(ctx.res.statusCode, 201);
assert.deepEqual(ctx.res.body, responseBody);
assert.deepEqual(ctx.res.body, postResponseBody);
});
});

Expand Down Expand Up @@ -185,7 +189,7 @@ describe('Request HTTP transport', () => {
api
.get('/')
.delay(500)
.reply(200, simpleResponseBody);
.reply(200, responseBody);

const ctx = createContext(url);
ctx.req.timeout(20);
Expand All @@ -206,7 +210,7 @@ describe('Request HTTP transport', () => {
api
.get('/')
.delay(500)
.reply(200, simpleResponseBody);
.reply(200, responseBody);

const ctx = createContext(url);

Expand All @@ -227,7 +231,7 @@ describe('Request HTTP transport', () => {

it('enables timing request by default', () => {
nock.cleanAll();
api.get('/').reply(200, simpleResponseBody);
api.get('/').reply(200, responseBody);

const ctx = createContext(url);

Expand All @@ -240,6 +244,57 @@ describe('Request HTTP transport', () => {
.catch(assert.ifError);
});

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', () => {
const ctx = createContext(url);
const options = {
Expand Down

0 comments on commit a5af740

Please sign in to comment.