Comparing version 0.1.0 to 0.1.1
# Pact History | ||
## Version 0.1.1 (2011-03-29) | ||
* You can now specify HTTP headers in request(). | ||
* Only follow relative 302 redirects. | ||
* Linted. | ||
## Version 0.1.0 (2011-02-08) | ||
* First public release. |
103
index.js
@@ -10,6 +10,6 @@ /*! | ||
*/ | ||
var assert = require("assert"); | ||
var assert = require('assert'); | ||
var http = require("./lib/http"); | ||
var STATUS_CODES = require("http").STATUS_CODES; | ||
var http = require('./lib/http'); | ||
var STATUS_CODES = require('http').STATUS_CODES; | ||
@@ -23,4 +23,6 @@ /** | ||
* Factory for server port numbers. | ||
* | ||
* @return {Number} port A new port number. | ||
*/ | ||
function getPort () { | ||
function getPort() { | ||
return _port++; | ||
@@ -34,10 +36,14 @@ } | ||
* | ||
* Any 302 redirects will be followed. | ||
* Relative 302 redirects will be followed. | ||
* | ||
* You can specify: | ||
* | ||
* - url: The path to request. Defaults to a path as the second word of a context. | ||
* - url: The path to request. Defaults to a path as the second word of | ||
* a context. | ||
* - method: Defaults to GET. | ||
* - data: Request body for POST. | ||
* - headers: HTTP request headers object. | ||
* | ||
* These are all optional. | ||
* | ||
* Instead of specifying `url` in `req`, you can make it the second word of | ||
@@ -53,3 +59,4 @@ * your context. This lets you omit `req` completely, for example: | ||
* | ||
* - body: The response body, which is an object if the response was application/json. | ||
* - body: The response body, which is an object if the response was | ||
* application/json. | ||
* - status: HTTP status code. | ||
@@ -62,16 +69,17 @@ * - headers: HTTP headers as an object, with headers in lowercase. | ||
* | ||
* @param {Object} req Request object | ||
* @return {Function} Topic function that makes the request | ||
* @param {Object} req Request object. | ||
* @return {Function} Topic function that makes the request. | ||
*/ | ||
function request (req) { | ||
function request(req) { | ||
var path, options = { | ||
host : "localhost", | ||
method : "GET" | ||
host: 'localhost', | ||
method: 'GET' | ||
}; | ||
if (req) { | ||
if ("url" in req) path = options.path = req.url; | ||
if ("data" in req) options.body = req.data; | ||
if ("method" in req) options.method = req.method; | ||
if ('url' in req) path = options.path = req.url; | ||
if ('data' in req) options.body = req.data; | ||
if ('method' in req) options.method = req.method; | ||
if ('headers' in req) options.headers = req.headers; | ||
} | ||
return function (lastTopic) { | ||
return function(lastTopic) { | ||
var vow = this; | ||
@@ -82,4 +90,4 @@ // try to find port number | ||
options.port = port; | ||
else throw new Error("Unable to determine port from topic."); | ||
if ("function" === typeof path) | ||
else throw new Error('Unable to determine port from topic.'); | ||
if ('function' === typeof path) | ||
options.path = path(lastTopic); | ||
@@ -90,17 +98,21 @@ else if (!path) | ||
options | ||
).on("response", function X (res, results) { | ||
).on('response', function X(res, results) { | ||
var err = null; | ||
if (res.statusCode === 302) { // handle redirects | ||
if (options._302) { | ||
err = "Redirect loop"; | ||
err = 'Redirect loop'; | ||
} else { | ||
options.path = res.headers.location; | ||
options._302 = true; | ||
return http.request(options).on("response", X); | ||
var location = res.headers.location; | ||
if (location.indexOf('/') === 0) { | ||
// relative path, don't handle absolute | ||
options.path = location; | ||
options._302 = true; | ||
return http.request(options).on('response', X); | ||
} | ||
} | ||
} | ||
vow.callback(err, { | ||
body : results, | ||
status : res.statusCode, | ||
headers : res.headers | ||
body: results, | ||
status: res.statusCode, | ||
headers: res.headers | ||
}); | ||
@@ -118,10 +130,11 @@ }); | ||
* | ||
* @param {Object} `http.Server` instance | ||
* @return {Function} Topic function that starts the server | ||
* @param {Object} server `http.Server` instance. | ||
* @param {Number} port Optional. Port to listen on. | ||
* @return {Function} Topic function that starts the server. | ||
*/ | ||
function httpify (server) { | ||
var port = getPort(); | ||
function httpify(server, port) { | ||
port = port || getPort(); | ||
return function() { | ||
var vows = this; | ||
server.listen(port, "127.0.0.1", function (err) { | ||
server.listen(port, '127.0.0.1', function(err) { | ||
vows.callback(err, port); | ||
@@ -138,13 +151,13 @@ }); | ||
* @see httpify | ||
* @param {Number} status Expected HTTP status code | ||
* @return {Function} Test function that checks the status | ||
* @param {Number} status Expected HTTP status code. | ||
* @return {Function} Test function that checks the status. | ||
*/ | ||
function code (status) { | ||
return function (lastTopic) { | ||
assert.strictEqual(lastTopic.status, status, | ||
"Expected " + status | ||
+ " " + STATUS_CODES[status] | ||
+ ", received: " + lastTopic.status | ||
+ " " + STATUS_CODES[lastTopic.status] | ||
+ ".\n" + lastTopic.body | ||
function code(status) { | ||
return function(lastTopic) { | ||
assert.strictEqual(lastTopic.status, status, | ||
'Expected ' + status + | ||
' ' + STATUS_CODES[status] + | ||
', received: ' + lastTopic.status + | ||
' ' + STATUS_CODES[lastTopic.status] + | ||
'.\n' + lastTopic.body | ||
); | ||
@@ -158,5 +171,5 @@ }; | ||
module.exports = { | ||
code : code, | ||
request : request, | ||
httpify : httpify | ||
}; | ||
code: code, | ||
request: request, | ||
httpify: httpify | ||
}; |
@@ -9,3 +9,3 @@ { | ||
], | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"author": "Reid Burke <me@reidburke.com> (http://reidburke.com/)", | ||
@@ -12,0 +12,0 @@ "main": "index", |
# Pact | ||
A collection of [Vows](http://vowsjs.org) macros for easy HTTP server testing. | ||
A collection of [Vows][] macros for easy HTTP server testing. | ||
@@ -73,2 +73,8 @@ Tastes great with [Express](http://expressjs.com) and [Connect](http://senchalabs.github.com/connect/). | ||
## Run self-tests | ||
Requires [Vows][]. | ||
make test | ||
## About | ||
@@ -85,1 +91,3 @@ | ||
Submit bugs and pull requests to [Pact on GitHub](http://github.com/reid/pact). | ||
[Vows]: http://vowsjs.org/ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 2 instances in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
64211
12
300
92
5