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 Jul 18, 2019
1 parent ee5519c commit 3b54218
Showing 1 changed file with 12 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

0 comments on commit 3b54218

Please sign in to comment.