Skip to content

Commit

Permalink
test: unit test the performRequest itself
Browse files Browse the repository at this point in the history
  • Loading branch information
honzajavorek committed Jul 30, 2018
1 parent 1965d9f commit a281db9
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 47 deletions.
42 changes: 0 additions & 42 deletions src/transaction-runner.js
Expand Up @@ -607,48 +607,6 @@ Not performing HTTP request for '${transaction.name}'.\
// An actual HTTP request, before validation hooks triggering
// and the response validation is invoked here
performRequestAndValidate(test, transaction, hooks, callback) {
// TODO
// if (transaction.request.body && this.isMultipart(transaction.request.headers)) {
// transaction.request.body = this.fixApiBlueprintMultipartBody(transaction.request.body);
// }

// Fixing API Blueprint 'multipart/form-data' bodies:
// https://github.com/apiaryio/api-blueprint/issues/401
//
// Only bodies coming from the API Blueprint parser need fixing (not bodies
// set by Dredd users in hooks) and those can only be of the UTF-8 encoding.
//
// This is a workaround of a parser bug and as such it belongs to
// dredd-transactions, which should take care of the differences between
// formats. Having the fix here means 'before*' hooks are provided with
// incorrect request bodies.
// if (isMultipart(headers)) {
// body = Buffer.from(fixApiBlueprintMultipartBody(body.toString('utf-8')));
// }

// /**
// * Detects whether given request headers indicate the request body
// * is of the 'multipart/form-data' media type.
// *
// * @param {Object} headers
// */
// function isMultipart(headers) {
// const contentType = caseless(headers).get('Content-Type');
// return contentType ? contentType.includes('multipart') : false;
// }

// /**
// * Finds newlines not preceeded by carriage returns and replaces them by
// * newlines preceeded by carriage returns.
// *
// * See https://github.com/apiaryio/api-blueprint/issues/401
// *
// * @param {String} body
// */
// function fixApiBlueprintMultipartBody(body) {
// return body.replace(/\r?\n/g, '\r\n');
// }

const uri = url.format({
protocol: transaction.protocol,
hostname: transaction.host,
Expand Down
2 changes: 1 addition & 1 deletion test/unit/performRequest/createTransactionRes-test.js
@@ -1,6 +1,6 @@
const { assert } = require('chai');

const { createTransactionRes } = require('../../../src/perform-request');
const { createTransactionRes } = require('../../../src/performRequest');


describe('performRequest.createTransactionRes()', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/performRequest/detectBodyEncoding-test.js
@@ -1,6 +1,6 @@
const { assert } = require('chai');

const { detectBodyEncoding } = require('../../../src/perform-request');
const { detectBodyEncoding } = require('../../../src/performRequest');


describe('performRequest.detectBodyEncoding()', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/performRequest/getBodyAsBuffer-test.js
@@ -1,6 +1,6 @@
const { assert } = require('chai');

const { getBodyAsBuffer } = require('../../../src/perform-request');
const { getBodyAsBuffer } = require('../../../src/performRequest');


describe('performRequest.getBodyAsBuffer()', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/performRequest/normalizeBodyEncoding-test.js
@@ -1,6 +1,6 @@
const { assert } = require('chai');

const { normalizeBodyEncoding } = require('../../../src/perform-request');
const { normalizeBodyEncoding } = require('../../../src/performRequest');


describe('performRequest.normalizeBodyEncoding()', () => {
Expand Down
137 changes: 137 additions & 0 deletions test/unit/performRequest/performRequest-test.js
@@ -0,0 +1,137 @@
const sinon = require('sinon');
const { assert } = require('chai');

const performRequest = require('../../../src/performRequest');


describe('performRequest()', () => {
const uri = 'http://example.com/42';
const uriS = 'https://example.com/42';
const transactionReq = {
method: 'POST',
headers: { 'Content-Type': 'text/plain' },
body: 'Hello'
};
const res = { statusCode: 200, headers: { 'Content-Type': 'text/plain' } };
const request = sinon.stub().callsArgWithAsync(1, null, res, Buffer.from('Bye'));
const logger = { debug: sinon.spy() };

beforeEach(() => { logger.debug.reset(); });

it('does not modify the original HTTP options object', (done) => {
const httpOptions = { json: true };
performRequest(uri, transactionReq, { http: httpOptions, request }, () => {
assert.deepEqual(httpOptions, { json: true });
done();
});
});
it('does not allow to override the hardcoded HTTP options', (done) => {
performRequest(uri, transactionReq, { http: { proxy: true }, request }, () => {
assert.isFalse(request.firstCall.args[0].proxy);
done();
});
});
it('forbids the HTTP client library to respect proxy settings', (done) => {
performRequest(uri, transactionReq, { request }, () => {
assert.isFalse(request.firstCall.args[0].proxy);
done();
});
});
it('forbids the HTTP client library to follow redirects', (done) => {
performRequest(uri, transactionReq, { request }, () => {
assert.isFalse(request.firstCall.args[0].followRedirect);
done();
});
});
it('propagates the HTTP method to the HTTP client library', (done) => {
performRequest(uri, transactionReq, { request }, () => {
assert.equal(request.firstCall.args[0].method, transactionReq.method);
done();
});
});
it('propagates the URI to the HTTP client library', (done) => {
performRequest(uri, transactionReq, { request }, () => {
assert.equal(request.firstCall.args[0].uri, uri);
done();
});
});
it('propagates the HTTP request body as a Buffer', (done) => {
performRequest(uri, transactionReq, { request }, () => {
assert.deepEqual(request.firstCall.args[0].body, Buffer.from('Hello'));
done();
});
});
it('handles exceptions when preparing the HTTP request body', (done) => {
const invalidTransactionReq = Object.assign(
{ bodyEncoding: 'latin2' },
transactionReq
);
performRequest(uri, invalidTransactionReq, { request }, (err) => {
assert.instanceOf(err, Error);
done();
});
});
it('logs before performing the HTTP request', (done) => {
performRequest(uri, transactionReq, { request, logger }, () => {
assert.equal(
logger.debug.firstCall.args[0],
`Performing HTTP request to the server under test: POST ${uri}`
);
done();
});
});
it('logs before performing the HTTPS request', (done) => {
performRequest(uriS, transactionReq, { request, logger }, () => {
assert.equal(
logger.debug.firstCall.args[0],
`Performing HTTPS request to the server under test: POST ${uriS}`
);
done();
});
});
it('logs on receiving the HTTP response', (done) => {
performRequest(uri, transactionReq, { request, logger }, () => {
assert.equal(
logger.debug.lastCall.args[0],
'Handling HTTP response from the server under test'
);
done();
});
});
it('logs on receiving the HTTPS response', (done) => {
performRequest(uriS, transactionReq, { request, logger }, () => {
assert.equal(
logger.debug.lastCall.args[0],
'Handling HTTPS response from the server under test'
);
done();
});
});
it('handles exceptions when requesting the server under test', (done) => {
const error = new Error('Ouch!');
const invalidRequest = sinon.stub().throws(error);
performRequest(uri, transactionReq, { request: invalidRequest }, (err) => {
assert.deepEqual(err, error);
done();
});
});
it('handles errors when requesting the server under test', (done) => {
const error = new Error('Ouch!');
const invalidRequest = sinon.stub().callsArgWithAsync(1, error);
performRequest(uri, transactionReq, { request: invalidRequest }, (err) => {
assert.deepEqual(err, error);
done();
});
});
it('provides the real HTTP response object', (done) => {
performRequest(uri, transactionReq, { request }, (err, real) => {
assert.deepEqual(real, {
statusCode: 200,
headers: { 'Content-Type': 'text/plain' },
body: 'Bye',
bodyEncoding: 'utf-8'
});
done();
});
});
});
2 changes: 1 addition & 1 deletion test/unit/performRequest/setContentLength-test.js
@@ -1,7 +1,7 @@
const sinon = require('sinon');
const { assert } = require('chai');

const { setContentLength } = require('../../../src/perform-request');
const { setContentLength } = require('../../../src/performRequest');


describe('performRequest.setContentLength()', () => {
Expand Down

0 comments on commit a281db9

Please sign in to comment.