Skip to content
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

Support for passthrough by default #80

Closed
Turtleguyy opened this issue Jun 23, 2015 · 7 comments
Closed

Support for passthrough by default #80

Turtleguyy opened this issue Jun 23, 2015 · 7 comments

Comments

@Turtleguyy
Copy link

Currently, there doesn't seem to be a way to only use Pretender for only a couple of specific paths, while allowing everything else to behave normally.

I'm trying to use Pretender to stub out data for one or two endpoints that aren't built out yet on the server. Since I do have a whole lot of other endpoints that are built out, I'd like to tell it to go ahead and treat all unhandled requests as passthroughs. Essentially, if I didn't tell it what to do for a path, let my server handle it.

@pangratz
Copy link
Contributor

pangratz commented Jun 23, 2015

Hmm, I think it should be possible with something like this:

var server = new Pretender(function() {
  // all handled routes here ...
  this.get('/api/version', function(request) {
    return [200, {}, JSON.stringify({ version: "123" })];
  });

  // add wildcards for all other routes and pass them through
  this.get('/*youvegottacatchemall', this.passthrough);
  this.post('/*youvegottacatchemall', this.passthrough);
  this.put('/*youvegottacatchemall', this.passthrough);
  this.delete('/*youvegottacatchemall', this.passthrough);
});

EDIT: as pointed out by @NLincoln below, in newer versions, the shorter path /** works as well.

@Turtleguyy
Copy link
Author

That did it! Not sure why though. I tried this.get('/*', this.passthrough); with no luck. Why does adding text after the asterisk make it work?

Leaving the issue open for now because it'd still be a nice feature to have, even though there does seem to be a workaround.

@pangratz
Copy link
Contributor

That did it! Not sure why though. I tried this.get('/*', this.passthrough); with no luck. Why does adding text after the asterisk make it work?

I think it's because route-recognizer needs a name for the dynamic path so it can be accessed inside the handler. So that's a route-recognizer thing not related to pretender.

@trek
Copy link
Member

trek commented Jul 22, 2015

Closing, since @pangratz's solution is how you'd do this.

@trek trek closed this as completed Jul 22, 2015
@NLincoln
Copy link

NLincoln commented Feb 4, 2017

Even though this is old, it's the top result for "pretender default passthrough", and a better solution I've found is similar to the one at the top, but instead of making the url /*youvegottacatchemall, you can simply put /**, which works just as well.

@geochronology
Copy link

2020 called to confirm that @NLincoln's solution still works.

@lecstor
Copy link

lecstor commented Nov 8, 2021

We needed to support hosts as well and ended up with this little hack..

/**
 * override Pretender's checkPassthrough to implement passthrough-by-default
 * ie any requests that do not have a handler get passed through.
 */
Pretender.prototype.origCheckPassthrough = Pretender.prototype.checkPassthrough;

Pretender.prototype.checkPassthrough = function (request) {
  var verb = request.method.toUpperCase();
  var path = request.url;
  var handler = this._handlerFor(verb, path, request);

  if (handler) return this.origCheckPassthrough(request);

  this.passthroughRequests.push(request);
  this.passthroughRequest(verb, path, request);
  return true;
};

may cover #210 too, depending on use-case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants