diff --git a/doc/plugins.markdown b/doc/plugins.markdown index c29b8f0..0549c1a 100644 --- a/doc/plugins.markdown +++ b/doc/plugins.markdown @@ -70,7 +70,7 @@ A configurable redirect mechanism. **options** - - `{Array} codes` HTTP status codes to react on, defaults to `[301, 302, 308]` + - `{Array} codes` HTTP status codes to redirect, defaults to `[301, 302, 308]` - `{number} limit` The maximum number of redirects, defaults to `1` - `{boolean} sameHost` Only allow redirects to the current host, defaults to `false` - `{boolean} allowUpgrade` Allow switch from `http` to `https/2`, defaults to `true` @@ -89,7 +89,7 @@ _Note_: Undefined request options are set to plugin defaults. ### Event: 'redirect' Emitted when `request.end()` of a redirected request has been called. -`function({Object} options)` +`function({Object} options, {Object} response)` [back to top](#table-of-contents) diff --git a/lib/plugins/redirect.js b/lib/plugins/redirect.js index 9f22b23..e7d6190 100644 --- a/lib/plugins/redirect.js +++ b/lib/plugins/redirect.js @@ -13,8 +13,9 @@ function RedirectPlugin(rail, options) { this._interceptrequest = null; this._interceptresponse = null; - this.limit = options.limit || 1; this.codes = options.codes || [301, 302, 308]; + + this.limit = options.limit || 1; this.sameHost = options.sameHost || false; this.allowUpgrade = options.allowUpgrade === false ? false : true; this.allowDowngrade = options.allowDowngrade || false; @@ -107,13 +108,13 @@ RedirectPlugin.prototype._interceptResponse = function(call, options, response) }); } - function onEnd() { + function finish() { call.__clear(); call.__request(function(err2, request) { if (request) { call.__intercept('request', self._interceptrequest); request.end(); - call.emit('redirect', options); + call.emit('redirect', options, response); } else { call.__emit('response', response); } @@ -122,17 +123,16 @@ RedirectPlugin.prototype._interceptResponse = function(call, options, response) // check if buffer plugin already handled the response body if (response.buffer !== undefined) { - onEnd(); + finish(); } else { // dump obsolete response response.on('readable', function() { - // TODO: check & warn (a redirect shouldn't have a body) response.read(); }); // send next request - response.once('end', onEnd); + response.on('end', finish); } }; diff --git a/test/test-http-redirect.js b/test/test-http-redirect.js index 65abf86..34c6d45 100644 --- a/test/test-http-redirect.js +++ b/test/test-http-redirect.js @@ -82,8 +82,6 @@ suite('http:redirect', function() { assert.strictEqual(response.buffer.length, 6); assert.strictEqual(response.buffer.toString(), 'works!'); done(); - }).on('warn', function(plugin, status, message) { - console.log('warn', plugin, status, message); }).end(); }); @@ -118,6 +116,8 @@ suite('http:redirect', function() { assert.strictEqual(response.buffer.length, 6); assert.strictEqual(response.buffer.toString(), 'works!'); done(); + }).on('redirect', function(options, response) { + assert.strictEqual(response.statusCode, 302); }).end(); }); diff --git a/test/test-http-validate-retry.js b/test/test-http-validate-retry.js index 6f20460..f8c0007 100644 --- a/test/test-http-validate-retry.js +++ b/test/test-http-validate-retry.js @@ -90,8 +90,10 @@ suite('http:validate:retry', function() { [['hello', 'number', 'type', 'world']]); done(); - }).on('retry', function() { + }).on('retry', function(options, response, reason) { ++retries; + assert.strictEqual(response.statusCode, 200); + assert.strictEqual(reason, 'validate'); }).end(); });