...
 
Commits (13)
const net = require('net');
const net = require('net'),
http = require('http');
/**
* Echo raw request message to test
......@@ -56,6 +57,66 @@ function createRawEchoServer () {
return server;
}
/**
* Redirect + Echo Server
* - Follows specified redirects -> /<redirects-count>
* - Redirects with specified response code -> /status/<response-code>
* - Echos final request URL, Method, Headers and Body
*
* @example
* var s = createRedirectServer();
*
* s.listen(3000, function() {
* console.log(s.port);
* s.close();
* });
*/
function createRedirectServer () {
var server;
server = http.createServer(function (req, res) {
var hops,
data = '';
// path: /{n}
if ((/^\/\d+$/).test(req.url)) {
hops = parseInt(req.url.substring(1), 10) - 1;
// redirect until all hops are covered
res.writeHead(302, {
location: hops > 0 ? `/${hops}` : '/'
});
return res.end();
}
// path: /status/<responseCode>
else if ((/^\/status\/(\d{3})$/).test(req.url)) {
res.writeHead(parseInt(req.url.substr(-3), 10), { location: '/' });
return res.end();
}
req.on('data', function (d) { data += d; });
req.once('end', function () {
res.writeHead(200, { connection: 'close', 'Content-Type': 'application/json' });
res.end(JSON.stringify({
url: req.url,
method: req.method,
headers: req.headers,
data: data
}));
});
});
server.on('listening', function () {
server.port = this.address().port;
});
return server;
}
module.exports = {
createRawEchoServer
createRawEchoServer,
createRedirectServer
};
......@@ -9,9 +9,11 @@ var fs = require('fs'),
recursive = require('recursive-readdir'),
newman = require(pathUtils.join(__dirname, '..', 'index')),
server = require('./server').createRawEchoServer(),
echoServer = require('./server').createRawEchoServer(),
redirectServer = require('./server').createRedirectServer(),
LOCAL_TEST_ECHO_PORT = 4041,
LOCAL_TEST_REDIRECT_PORT = 4042,
/**
* The source directory containing integration test collections and folders.
......@@ -65,15 +67,22 @@ module.exports = function (exit) {
},
/**
* Start local echo server used in collections (custom HTTP method, body with GET).
* Start local server used in collections
* - echoServer = custom HTTP method, body with GET
* - redirectServer = protocol profile behavior
*
* @param {Object} suites - An set of tests, arranged by test group names as keys.
* @param {Function} next - A callback function whose invocation marks the end of the integration test run.
* @returns {*}
*/
function (suites, next) {
server.listen(LOCAL_TEST_ECHO_PORT, function (err) {
next(err, suites);
// start echoServer first
echoServer.listen(LOCAL_TEST_ECHO_PORT, function (err) {
if (err) { return next(err); }
// start redirectServer once echoServer is started
redirectServer.listen(LOCAL_TEST_REDIRECT_PORT, function (err) {
next(err, suites);
});
});
},
......@@ -130,8 +139,13 @@ module.exports = function (exit) {
console.error(_.omit(err, ['stacktrace', 'stack']), { colors: true });
}
server.close(function () {
exit(err, results);
// close echoServer
echoServer.close(function () {
// close redirectServer
redirectServer.close(function () {
// exit once both the local server are stopped
exit(err, results);
});
});
});
};
......
This diff is collapsed.
{
"info": {
"_postman_id": "9f20bc41-541b-420c-a0dc-0e25d019e241",
"name": "redirect-with-body",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [{
"name": "PUT 307 + FormData",
"event": [{
"listen": "test",
"script": {
"id": "05db1385-6c2d-4432-998c-3dfd60f06102",
"exec": [
"const responseJSON = pm.response.json();",
"",
"pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
"});",
"",
"",
"pm.test(\"Response body sanity\", function () {",
" pm.expect(responseJSON).to.have.property('url', '/');",
" pm.expect(responseJSON).to.have.property('method', 'PUT');",
" pm.expect(responseJSON).to.have.property('data');",
" pm.expect(responseJSON.data)",
" .to.include('Content-Disposition: form-data; name=\"key1\"')",
" .and.include('POSTMAN');",
"});"
],
"type": "text/javascript"
}
}],
"request": {
"method": "PUT",
"header": [],
"body": {
"mode": "formdata",
"formdata": [{
"key": "key1",
"value": "POSTMAN",
"type": "text"
}]
},
"url": {
"raw": "localhost:4042/status/307",
"host": [
"localhost"
],
"port": "4042",
"path": [
"status",
"307"
]
}
},
"response": []
},
{
"name": "POST 307 + Raw",
"event": [{
"listen": "test",
"script": {
"id": "05db1385-6c2d-4432-998c-3dfd60f06102",
"exec": [
"const responseJSON = pm.response.json();",
"",
"pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
"});",
"",
"",
"pm.test(\"Response body sanity\", function () {",
" pm.expect(responseJSON).to.have.property('url', '/');",
" pm.expect(responseJSON).to.have.property('method', 'POST');",
" pm.expect(responseJSON).to.have.property('data', 'POSTMAN');",
"});"
],
"type": "text/javascript"
}
}],
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "POSTMAN"
},
"url": {
"raw": "localhost:4042/status/307",
"host": [
"localhost"
],
"port": "4042",
"path": [
"status",
"307"
]
}
},
"response": []
},
{
"name": "POST 308 + FormData",
"event": [{
"listen": "test",
"script": {
"id": "05db1385-6c2d-4432-998c-3dfd60f06102",
"exec": [
"const responseJSON = pm.response.json();",
"",
"pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
"});",
"",
"",
"pm.test(\"Response body sanity\", function () {",
" pm.expect(responseJSON).to.have.property('url', '/');",
" pm.expect(responseJSON).to.have.property('method', 'POST');",
" pm.expect(responseJSON).to.have.property('data');",
" pm.expect(responseJSON.data)",
" .to.include('Content-Disposition: form-data; name=\"key1\"')",
" .and.include('POSTMAN');",
"});"
],
"type": "text/javascript"
}
}],
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "formdata",
"formdata": [{
"key": "key1",
"value": "POSTMAN",
"type": "text"
}]
},
"url": {
"raw": "localhost:4042/status/308",
"host": [
"localhost"
],
"port": "4042",
"path": [
"status",
"308"
]
}
},
"response": []
},
{
"name": "PUT 308 + Raw",
"event": [{
"listen": "test",
"script": {
"id": "05db1385-6c2d-4432-998c-3dfd60f06102",
"exec": [
"const responseJSON = pm.response.json();",
"",
"pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
"});",
"",
"",
"pm.test(\"Response body sanity\", function () {",
" pm.expect(responseJSON).to.have.property('url', '/');",
" pm.expect(responseJSON).to.have.property('method', 'PUT');",
" pm.expect(responseJSON).to.have.property('data', 'POSTMAN');",
"});"
],
"type": "text/javascript"
}
}],
"request": {
"method": "PUT",
"header": [],
"body": {
"mode": "raw",
"raw": "POSTMAN"
},
"url": {
"raw": "localhost:4042/status/308",
"host": [
"localhost"
],
"port": "4042",
"path": [
"status",
"308"
]
}
},
"response": []
}
]
}