-
Notifications
You must be signed in to change notification settings - Fork 119
fix: Handle requester errors with attached responses #1537
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
Open
deve-sh
wants to merge
3
commits into
develop
Choose a base branch
from
fix/handle-requester-errors-with-responses
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+100
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ var _ = require('lodash'), | |
| EventEmitter = require('events'), | ||
| now = require('performance-now'), | ||
| sdk = require('postman-collection'), | ||
| requests = require('./request-wrapper'), | ||
| requestWrapperModule = require('./request-wrapper'), | ||
| dryRun = require('./dry-run'), | ||
| SSEProcessor = require('./sse-processor'), | ||
|
|
||
|
|
@@ -435,20 +435,31 @@ class Requester extends EventEmitter { | |
| return onEnd(new Error(ERROR_RESTRICTED_ADDRESS + hostname)); | ||
| } | ||
|
|
||
| const requests = requestWrapperModule.request || requestWrapperModule; | ||
|
Collaborator
Author
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. Added for testability via Sinon stubbing |
||
|
|
||
| return requests(request, requestOptions, onStart, onData, function (err, res, resBody, debug) { | ||
| // prepare history from request debug data | ||
| var history = getExecutionHistory(debug), | ||
| responseTime, | ||
| response; | ||
|
|
||
| if (err) { | ||
| if (err && !err.res) { | ||
| // bubble up http errors | ||
| // @todo - Should we send an empty sdk Response here? | ||
| // | ||
| // Sending `history` object even in case of error | ||
| return onEnd(err, undefined, history); | ||
| } | ||
|
|
||
| // If the error has a response attached, use it to populate the response instead of treating | ||
| // it as a crash. | ||
| if (err && err.res) { | ||
| res = err.res; | ||
|
|
||
| if (Buffer.isBuffer(err.res.body)) { | ||
| resBody = err.res.body; | ||
| } | ||
| } | ||
|
|
||
| // Calculate the time taken for us to get the response. | ||
| responseTime = Date.now() - startTime; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| const sdk = require('postman-collection'), | ||
| sinon = require('sinon').createSandbox(), | ||
| requestWrapper = require('../../../lib/requester/request-wrapper'); | ||
|
|
||
| (typeof window === 'undefined' ? describe : describe.skip)('handling of error with response', function () { | ||
| before(function (done) { | ||
| const fakeRequester = sinon.fake((_request, _requestOptions, onStart, _onData, callback) => { | ||
| const error = new Error('test error'), | ||
| mockResponse = { | ||
| status: 407, | ||
| statusMessage: 'Proxy Authentication Required', | ||
| body: Buffer.from('Proxy Authentication Required - Response Body from Proxy'), | ||
| headers: [], | ||
| request: { | ||
| _debug: [{ | ||
| request: { | ||
| method: 'GET', | ||
| href: 'https://postman-echo.com/get', | ||
| headers: [], | ||
| httpVersion: '1.1' | ||
| }, | ||
| response: { | ||
| downloadedBytes: 100 | ||
| } | ||
| }] | ||
| } | ||
| }; | ||
|
|
||
| mockResponse.getAllResponseHeaders = () => { | ||
| return mockResponse.headers; | ||
| }; | ||
|
|
||
| error.res = mockResponse; | ||
|
|
||
| onStart(error.res); // Calling of onStart via the 'response' event is handled by the postman-request library | ||
| callback(error, null, null, mockResponse.request._debug); | ||
| }); | ||
|
|
||
| sinon.stub(requestWrapper, 'request').callsFake(fakeRequester); | ||
|
|
||
| done(); | ||
| }); | ||
|
|
||
| after(function () { | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| it('should handle error with an attached response', function (done) { | ||
| this.run({ collection: { item: { request: 'https://postman-echo.com/get' } } }, function (err, results) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
|
|
||
| const testrun = results; | ||
|
|
||
| sinon.assert.calledOnce(testrun.responseStart); | ||
| sinon.assert.calledOnce(testrun.response); | ||
| sinon.assert.calledOnce(testrun.done); | ||
|
|
||
| // Even though the callback to runtime returned an error, we handle the response attached to it | ||
| // and don't consider it as a crash. | ||
|
|
||
| let responseStartCalledWith = testrun.responseStart.getCall(0).args, | ||
| responseCalledWith = testrun.response.getCall(0).args, | ||
| doneCalledWith = testrun.done.getCall(0).args; | ||
|
|
||
| expect(responseStartCalledWith[0]).to.be.equal(null); // Not an error | ||
| expect(responseStartCalledWith[2]).to.be.instanceOf(sdk.Response); | ||
| expect(responseStartCalledWith[2].code).to.be.equal(407); | ||
|
|
||
| expect(responseCalledWith[0]).to.be.equal(null); // Not an error | ||
| expect(responseCalledWith[2]).to.be.instanceOf(sdk.Response); | ||
| expect(responseCalledWith[2].text()).to.be.equal('Proxy Authentication Required' + | ||
| ' - Response Body from Proxy'); | ||
| expect(responseCalledWith[2].code).to.be.equal(407); | ||
|
|
||
| expect(doneCalledWith[0]).to.be.equal(null); // Not an error | ||
|
|
||
| done(); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is done for adding testability + Keeping this module backwards compatible with the browser version of this.