Permalink
Browse files

Add content length header for non-GET requests

  • Loading branch information...
1 parent 597c30b commit cb1124a71b62ea5d11e62d434759938ea854e51a Sho Kuwamoto committed Oct 24, 2012
Showing with 18 additions and 4 deletions.
  1. +1 −1 README.md
  2. +3 −2 lib/options.js
  3. +9 −0 lib/testcase.js
  4. +1 −1 package.json
  5. +4 −0 tests/falkor_test.js
View
@@ -91,7 +91,7 @@ Helper for setting the cookie header. Can be called multiple times to set multi
#### .withPayload(string)
-Specifies a string to be sent as the request payload/body. Will be sent regardless of HTTP method.
+Specifies a string (assumed to be utf8) to be sent as the request payload/body. Will be sent regardless of HTTP method.
#### .withFormEncodedPayload(object)
View
@@ -90,11 +90,12 @@ Options.prototype.withCookie = function (name, value, options) {
/**
* Sets the request payload.
- * @param {string} body A string.
+ * @param {string} body A string (assumed to be utf8)
* @return {Options} The instance.
*/
Options.prototype.withPayload = function (payload) {
this._payload = payload
+ this.withHeader('Content-Length', Buffer.byteLength(payload, 'utf8'))
return this
}
@@ -163,7 +164,7 @@ Options.prototype.expectStatusCode = function (statusCode) {
Options.prototype.expectHeader = function (name, value) {
name = name.toLowerCase() // node lowercases headers.
this.evaluate(function (test, res) {
- test.equals(res.headers[name], value, 'Expected "' + name + '" header to be "' + value + '"')
+ test.equals(res.headers[name], value, 'Expected "' + name + '" header to be "' + value + '", found ' + JSON.stringify(res.headers))
})
return this
}
View
@@ -108,6 +108,15 @@ TestCase.prototype.run = function () {
, headers: this.getHeaders()
}
+ if (options.method === 'PUT' || options.method === 'POST') {
+ // When there is no payload, a content length may not have
+ // been set. This causes problems for certain servers (e.g., nginx),
+ // so we set it here.
+ if (!options.headers['Content-Length'] && !this._payload) {
+ options.headers['Content-Length'] = 0
+ }
+ }
+
var httpLib = options.port == 443 ? https : http
// Sends the request, with an optional payload.
View
@@ -1,6 +1,6 @@
{
"name": "falkor",
- "version": "1.1.3",
+ "version": "1.1.4",
"keywords": [
"javascript",
"nodeunit",
View
@@ -101,6 +101,7 @@ exports.testWithFormEncodedPayload = function (test) {
mockTest.verifyResponse(nock('http://falkor.fake')
.matchHeader('Content-Type', 'application/x-www-form-urlencoded')
+ .matchHeader('Content-Length', 14)
.post('/testformencoded', 'a=%3D%26&b=xxx')
.reply(200, ''))
@@ -410,6 +411,7 @@ exports.testTemplates = function (test) {
.put('/templated', 'data')
.matchHeader('Cookie', 'username=dan')
.matchHeader('TestHeader', '1234')
+ .matchHeader('Content-Length', 4)
.reply(200, 'prefix result'))
template.newTestCase('http://falkor.fake/templated')
@@ -434,11 +436,13 @@ exports.testTemplateOverrides = function (test) {
mockTest.verifyResponse(nock('http://falkor.fake')
.post('/templated', 'data')
.matchHeader('TestHeader', '1234')
+ .matchHeader('Content-Length', 4)
.reply(200, ''))
mockTest.verifyResponse(nock('http://falkor.fake')
.post('/templated', 'data override')
.matchHeader('TestHeader', '9876')
+ .matchHeader('Content-Length', 13)
.reply(200, ''))

0 comments on commit cb1124a

Please sign in to comment.