Skip to content

Commit

Permalink
#832 Run got tests using common test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
ianwsperber committed Mar 1, 2017
1 parent c13d25f commit a40fb62
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 43 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@
"zombie": "^5.0.1"
},
"scripts": {
"test": "tap --harmony ./tests/test_*.js",
"unit": "tap --harmony ./tests/test_*.js",
"preintegration": "./tests/bin/install_sub_deps",
"integration": "tap --harmony ./tests/versioned/*/*.tap.js",
"test": "npm run unit && npm run integration",
"coverage": "nyc tap --harmony ./tests/test_*.js",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"jshint": "jshint lib/*.js",
Expand Down
78 changes: 78 additions & 0 deletions tests/versioned/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict';

const tap = require('tap');
const nock = require('../../');

/**
* Run common test suite against an HTTP client library.
*
* The makeRequest fn should be a wrapper around an
* HTTP client, which maps our arguments to those accepted by the client.
*
* The client is expected to accept an options object with props:
* - uri
* - method
* - timeout
* And return a promise, which in success has properties
* - statusCode (number)
* - body (string)
* And in failure is a standard error object.
*
* @param {Function} makeRequest Request fn
* @param {string} name Lib we're testing
*/
const runCommonTests = (makeRequest, name) => {
const test = (description, cb) => {
return tap.test(`${name}: ${description}`, cb);
};

test('basic intercept', (t) => {
nock('http://www.example.com')
.get('/')
.reply(200, 'OK');

return makeRequest({
uri: 'www.example.com'
})
.then((res) => {
t.equal(res.statusCode, 200);
t.deepEqual(res.body, 'OK');

t.end();
})
.catch(t.threw);
});

test('scope#socketDelay', (t) => {
const timeout = 100;
nock('http://www.example.com')
.get('/')
.socketDelay(timeout)
.reply(200, 'OK');

return makeRequest({
uri: 'www.example.com',
timeout: timeout / 2
})
.then((res) => {
const code = res.statusCode;
const err = code === 200
? new Error('Unexpected success')
: new Error(`Unexpected failure (${code})`);

throw err;
})
.catch((error) => {
if (error.message.match('Unexpected')) {
t.fail(`No timeout: ${error.message}`);
}

t.end();
})
.catch(t.threw);
});
};

module.exports = {
runCommonTests
};
50 changes: 8 additions & 42 deletions tests/versioned/got-latest/test.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,13 @@

const tap = require('tap');
const got = require('got');
const nock = require('../../../');
const common = require('../common');

tap.test('basic intercept', (t) => {
nock('http://www.example.com')
.get('/')
.reply(200, 'OK');
const makeRequest = (options) => {
return got(options.uri, {
method: options.method,
timeout: options.timeout
});
};

got('www.example.com')
.then((response) => {
t.equal(response.statusCode, 200);
t.deepEqual(response.body, 'OK');

t.end();
})
.catch((err) => {
t.fail(err.message);
t.end();
})
});

tap.test('scope#socketDelay', (t) => {
const timeout = 500;
nock('http://www.example.com')
.get('/')
.socketDelay(timeout)
.reply(200, 'OK');

got('www.example.com', { timeout: timeout - 100 })
.then((response) => {
const code = response.statusCode;
const err = code === 200
? new Error('Unexpected success')
: new Error(`Unexpected failure (${code})`);

throw err;
})
.catch((error) => {
if (error.message.match('Unexpected')) {
t.fail(`No timeout: ${error.message}`);
}

t.end();
});
});
common.runCommonTests(makeRequest, 'got@latest');

0 comments on commit a40fb62

Please sign in to comment.