-
-
Notifications
You must be signed in to change notification settings - Fork 238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Callback implementation and tests for write and end #101
base: master
Are you sure you want to change the base?
Changes from 5 commits
a68265e
a7c7615
ce0f02f
9d55cfd
4a8e87f
2fdf90f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
unreleased | ||
========== | ||
|
||
* Callbacks available in `write` and `end` when supported in nodejs (requires nodejs >= 0.12.x) | ||
* deps: [email protected] | ||
* deps: compressible@~2.0.9 | ||
- Fix regex fallback to not override `compressible: false` in db | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -676,6 +676,78 @@ describe('compression()', function () { | |
.end() | ||
}) | ||
}) | ||
|
||
describe('when callbacks are used', function () { | ||
it('should call the passed callbacks in the order passed when compressing', function (done) { | ||
var hasCallbacks = false | ||
var callbackOutput = [] | ||
var server = createServer(null, function (req, res) { | ||
// hasCallback check can be removed once this module only supports node >= 0.12 and .travis.yml is updated to test on node >= 0.12 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on the plan, I would just go ahead and remove this testing conditional and remove the unsupported versions from the travis file right in this PR :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
hasCallbacks = (http.OutgoingMessage.prototype.write.length === 3 && http.OutgoingMessage.prototype.end.length === 3) | ||
res.setHeader('Content-Type', 'text/plain') | ||
res.write('Hello', null, function () { | ||
callbackOutput.push(0) | ||
}) | ||
res.write(' World', null, function () { | ||
callbackOutput.push(1) | ||
}) | ||
res.end(null, null, function () { | ||
callbackOutput.push(2) | ||
}) | ||
}) | ||
|
||
request(server) | ||
.get('/') | ||
.set('Accept-Encoding', 'gzip') | ||
.expect('Content-Encoding', 'gzip') | ||
.end(function (err) { | ||
if (err) { | ||
throw new Error(err) | ||
} | ||
if (hasCallbacks) { | ||
assert.equal(callbackOutput.length, 3) | ||
assert.deepEqual(callbackOutput, [0, 1, 2]) | ||
} | ||
done() | ||
}) | ||
}) | ||
|
||
it('should call the passed callbacks in the order passed when not compressing', function (done) { | ||
var hasCallbacks = false | ||
var callbackOutput = [] | ||
var server = createServer(null, function (req, res) { | ||
// hasCallback check can be removed once this module only supports node >= 0.12 and .travis.yml is updated to test on node >= 0.12 | ||
hasCallbacks = (http.OutgoingMessage.prototype.write.length === 3 && http.OutgoingMessage.prototype.end.length === 3) | ||
res.setHeader('Cache-Control', 'no-transform') | ||
res.setHeader('Content-Type', 'text/plain') | ||
res.write('hello,', null, function () { | ||
callbackOutput.push(0) | ||
}) | ||
res.write(' world', null, function () { | ||
callbackOutput.push(1) | ||
}) | ||
res.end(null, null, function () { | ||
callbackOutput.push(2) | ||
}) | ||
}) | ||
|
||
request(server) | ||
.get('/') | ||
.set('Accept-Encoding', 'gzip') | ||
.expect('Cache-Control', 'no-transform') | ||
.expect(shouldNotHaveHeader('Content-Encoding')) | ||
.end(function (err) { | ||
if (err) { | ||
throw new Error(err) | ||
} | ||
if (hasCallbacks) { | ||
assert.equal(callbackOutput.length, 3) | ||
assert.deepEqual(callbackOutput, [0, 1, 2]) | ||
} | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
function createServer (opts, fn) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the callback argument was added in 0.11.6, actually!