Skip to content

Commit

Permalink
Extract FakeRequest.passthrough()
Browse files Browse the repository at this point in the history
Anytime send is called, the arguments are saved in the instance. They can subsequently be used in the `.passthrough()` method.
  • Loading branch information
happycollision committed Dec 18, 2019
1 parent ee5519c commit 9984ed2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function interceptor(ctx) {

// extend
FakeRequest.prototype.send = function send() {
this.sendArguments = arguments;
if (!ctx.pretender.running) {
throw new Error('You shut down a Pretender instance while there was a pending request. ' +
'That request just tried to complete. Check to see if you accidentally shut down ' +
Expand All @@ -97,13 +98,22 @@ function interceptor(ctx) {
FakeXMLHttpRequest.prototype.send.apply(this, arguments);

if (ctx.pretender.checkPassthrough(this)) {
var xhr = createPassthrough(this);
xhr.send.apply(xhr, arguments);
this.passthrough();
} else {
ctx.pretender.handleRequest(this);
}
};

FakeRequest.prototype.passthrough = function passthrough() {
if (!this.sendArguments) {
throw new Error('You attempted to passthrough a FakeRequest that was never sent. ' +
'Call `.send()` on the original request first');
}
var xhr = createPassthrough(this);
xhr.send.apply(xhr, this.sendArguments);
return xhr;
};


function createPassthrough(fakeXHR) {
// event types to handle on the xhr
Expand Down
17 changes: 17 additions & 0 deletions test/passthrough_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,21 @@ describe('passthrough requests', function(config) {

xhr.send();
});

it('allows a passthrough on an unhandledRequest via request.passthrough()', function(assert) {
var done = assert.async();

this.pretender.unhandledRequest = function(_verb, _path, request) {
request.passthrough();
};

$.ajax({
url: '/some/path',
error: function(xhr) {
assert.equal(xhr.status, 404);
done();
}
});
});

});

0 comments on commit 9984ed2

Please sign in to comment.