UNPKG

postman-runtime

Version:

Underlying library of executing Postman Collections (used by Newman)

458 lines (407 loc) 19.1 kB
var expect = require('chai').expect, runtime = require('../../index'), sdk = require('postman-collection'); describe('Localhost requests', function () { // IPv6 is disabled on Travis // eslint-disable-next-line no-process-env (process.env.TRAVIS ? describe.skip : describe)('IPv6 server', function () { var server, port; beforeEach('Start IPv6 server', function (done) { var http = require('http'); server = http.createServer(); server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.on('listening', function () { port = server.address().port; done(); }); server.listen(0, '::1'); }); it('should be able to connect', function (mochaDone) { var errored = false, runner = new runtime.Runner(), rawCollection = { 'variables': [], 'info': { 'name': 'NewmanSetNextRequest', '_postman_id': 'd6f7bb29-2258-4e1b-9576-b2315cf5b77e', 'description': '', 'schema': 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json' }, 'item': [ { 'id': 'bf0a6006-c987-253a-525d-9f6be7071210', 'name': 'First Request', 'request': { 'url': 'http://localhost:' + port + '/', 'method': 'GET' } } ] }, collection = new sdk.Collection(rawCollection), testables = { iterationsStarted: [], iterationsComplete: [], itemsStarted: {}, itemsComplete: {} }, // populate during the run, and then perform tests on it, at the end. /** * Since each callback runs in a separate callstack, this helper function * ensures that any errors are forwarded to mocha * * @param func */ check = function (func) { try { func(); } catch (e) { (errored = true) && mochaDone(e); } }; runner.run(collection, { iterationCount: 1, requester: { followRedirects: false } }, function (err, run) { var runStore = {}; // Used for validations *during* the run. Cursor increments, etc. expect(err).to.be.null; run.start({ start: function (err, cursor) { check(function () { expect(err).to.be.null; expect(cursor).to.deep.include({ position: 0, iteration: 0, length: 1, cycles: 1, eof: false, empty: false, bof: true, cr: false }); expect(cursor).to.have.property('ref'); // Set this to true, and verify at the end, so that the test will fail even if this // callback is never called. testables.started = true; }); }, beforeIteration: function (err, cursor) { check(function () { expect(err).to.be.null; testables.iterationsStarted.push(cursor.iteration); runStore.iteration = cursor.iteration; }); }, iteration: function (err, cursor) { check(function () { expect(err).to.be.null; expect(cursor).to.have.property('iteration', runStore.iteration); testables.iterationsComplete.push(cursor.iteration); }); }, beforeItem: function (err, cursor, item) { check(function () { expect(err).to.be.null; testables.itemsStarted[cursor.iteration] = testables.itemsStarted[cursor.iteration] || []; testables.itemsStarted[cursor.iteration].push(item); runStore.position = cursor.position; runStore.ref = cursor.ref; }); }, item: function (err, cursor, item) { check(function () { expect(err).to.be.null; expect(cursor).to.deep.include({ position: runStore.position, ref: runStore.ref }); testables.itemsComplete[cursor.iteration] = testables.itemsComplete[cursor.iteration] || []; testables.itemsComplete[cursor.iteration].push(item); }); }, beforePrerequest: function (err, cursor, events) { check(function () { expect(err).to.be.null; // Sanity expect(cursor).to.deep.include({ iteration: runStore.iteration, position: runStore.position, ref: runStore.ref }); expect(events).to.be.empty; }); }, prerequest: function (err, cursor, results) { check(function () { expect(err).to.be.null; // Sanity expect(cursor).to.deep.include({ iteration: runStore.iteration, position: runStore.position, ref: runStore.ref }); // This collection has no pre-request scripts expect(results).to.be.empty; }); }, beforeTest: function (err, cursor) { check(function () { expect(err).to.be.null; // Sanity expect(cursor).to.deep.include({ iteration: runStore.iteration, position: runStore.position, ref: runStore.ref }); }); }, test: function (err, cursor) { check(function () { expect(err).to.be.null; // Sanity expect(cursor).to.deep.include({ iteration: runStore.iteration, position: runStore.position, ref: runStore.ref }); }); }, beforeRequest: function (err, cursor) { check(function () { expect(err).to.be.null; // Sanity expect(cursor).to.deep.include({ iteration: runStore.iteration, position: runStore.position, ref: runStore.ref }); }); }, request: function (err, cursor, response, request) { check(function () { expect(err).to.be.null; expect(request.url.toString()).to.be.ok; // Sanity expect(cursor).to.deep.include({ iteration: runStore.iteration, position: runStore.position, ref: runStore.ref }); expect(response).to.have.property('code', 200); expect(response.text()).to.equal('Hello World\n'); expect(request).to.be.ok; }); }, done: function (err) { check(function () { expect(err).to.be.null; !errored && mochaDone(); }); } }); }); }); }); describe('IPv4 server', function () { var server, port; beforeEach('Start IPv4 server', function (done) { var http = require('http'); server = http.createServer(); server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.on('listening', function () { port = server.address().port; done(); }); server.listen(0, '127.0.0.1'); }); it('should be able to connect', function (mochaDone) { var errored = false, runner = new runtime.Runner(), rawCollection = { 'variables': [], 'info': { 'name': 'NewmanSetNextRequest', '_postman_id': 'd6f7bb29-2258-4e1b-9576-b2315cf5b77e', 'description': '', 'schema': 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json' }, 'item': [ { 'id': 'bf0a6006-c987-253a-525d-9f6be7071210', 'name': 'First Request', 'request': { 'url': 'http://localhost:' + port + '/', 'method': 'GET' } } ] }, collection = new sdk.Collection(rawCollection), testables = { iterationsStarted: [], iterationsComplete: [], itemsStarted: {}, itemsComplete: {} }, // populate during the run, and then perform tests on it, at the end. /** * Since each callback runs in a separate callstack, this helper function * ensures that any errors are forwarded to mocha * * @param func */ check = function (func) { try { func(); } catch (e) { (errored = true) && mochaDone(e); } }; runner.run(collection, { iterationCount: 1, requester: { followRedirects: false } }, function (err, run) { var runStore = {}; // Used for validations *during* the run. Cursor increments, etc. expect(err).to.be.null; run.start({ start: function (err, cursor) { check(function () { expect(err).to.be.null; expect(cursor).to.deep.include({ position: 0, iteration: 0, length: 1, cycles: 1, eof: false, empty: false, bof: true, cr: false }); expect(cursor).to.have.property('ref'); // Set this to true, and verify at the end, so that the test will fail even if this // callback is never called. testables.started = true; }); }, beforeIteration: function (err, cursor) { check(function () { expect(err).to.be.null; testables.iterationsStarted.push(cursor.iteration); runStore.iteration = cursor.iteration; }); }, iteration: function (err, cursor) { check(function () { expect(err).to.be.null; expect(cursor).to.have.property('iteration', runStore.iteration); testables.iterationsComplete.push(cursor.iteration); }); }, beforeItem: function (err, cursor, item) { check(function () { expect(err).to.be.null; testables.itemsStarted[cursor.iteration] = testables.itemsStarted[cursor.iteration] || []; testables.itemsStarted[cursor.iteration].push(item); runStore.position = cursor.position; runStore.ref = cursor.ref; }); }, item: function (err, cursor, item) { check(function () { expect(err).to.be.null; expect(cursor).to.deep.include({ position: runStore.position, ref: runStore.ref }); testables.itemsComplete[cursor.iteration] = testables.itemsComplete[cursor.iteration] || []; testables.itemsComplete[cursor.iteration].push(item); }); }, beforePrerequest: function (err, cursor, events) { check(function () { expect(err).to.be.null; // Sanity expect(cursor).to.deep.include({ iteration: runStore.iteration, position: runStore.position, ref: runStore.ref }); expect(events).to.be.empty; }); }, prerequest: function (err, cursor, results) { check(function () { expect(err).to.be.null; // Sanity expect(cursor).to.deep.include({ iteration: runStore.iteration, position: runStore.position, ref: runStore.ref }); // This collection has no pre-request scripts expect(results).to.be.empty; }); }, beforeTest: function (err, cursor) { check(function () { expect(err).to.be.null; // Sanity expect(cursor).to.deep.include({ iteration: runStore.iteration, position: runStore.position, ref: runStore.ref }); }); }, test: function (err, cursor) { check(function () { expect(err).to.be.null; // Sanity expect(cursor).to.deep.include({ iteration: runStore.iteration, position: runStore.position, ref: runStore.ref }); }); }, beforeRequest: function (err, cursor) { check(function () { expect(err).to.be.null; // Sanity expect(cursor).to.deep.include({ iteration: runStore.iteration, position: runStore.position, ref: runStore.ref }); }); }, request: function (err, cursor, response, request) { check(function () { expect(err).to.be.null; expect(request.url.toString()).to.be.ok; // Sanity expect(cursor).to.deep.include({ iteration: runStore.iteration, position: runStore.position, ref: runStore.ref }); expect(response).to.have.property('code', 200); expect(response.text()).to.equal('Hello World\n'); expect(request).to.be.ok; }); }, done: function (err) { check(function () { expect(err).to.be.null; !errored && mochaDone(); }); } }); }); }); }); });