question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add "ctx.forward" for forwarding (bypassing) requests

See original GitHub issue

What

A new ctx.forward utility may be added to forward the currently captured request to its original destination (bypass).

Why

Currently bypassing is done via implicit/explicit return in the resolver. This is not intuitive and deterministic action, which also makes it hard to distinguish when the user forgot to return a mocked response, and when they meant to bypass intentionally.

With the introduction of this feature, the library could reliably print a warning when you have a handler, but return no response from the resolver—it becomes safe to assume that you forgot to do that.

API

rest.get('/user', (req, res, ctx) => {
  if (anything) {
    return ctx.forward()
  }

  return res(ctx.text('mocked'))
})

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:25
  • Comments:20 (10 by maintainers)

github_iconTop GitHub Comments

6reactions
IanVScommented, Oct 28, 2021

Doing so will not produce any onUnhandledRequest warnings, as the request was handled, just not resolved to a mocked response.

It might not be an onUnhandledRequest warning, but I do get this in my console:

[MSW] Expected a mocking resolver function to return a mocked response Object, but got: undefined. Original response is going to be used instead.

I’d love to be able to mark paths as explicitly bypassed / passthrough. I’d suggest though, instead of calling it ctx.forward(), perhaps to use the same word as mirage, and call it ctx.passthrough(), since at least to me, that makes the behavior more clear. To my mind, forward makes it seem like sending it somewhere that it wasn’t sent initially, like forwarding an email to a new recipient.

3reactions
kettanaitocommented, Sep 11, 2021

Hey, @adarnon.

There is a way to bypass any request in the handler directly right now:

rest.get('/user', (req, res, ctx) => {
  // Skip this handler and perform the request as-is
  // if the request URL contains the "skip" query parameter.
  if (req.url.searchParams.get('skip')) {
    return
  }

  return res(ctx.json({ id: 1 }))
})

Read more in Conditional response.

Both explicit and implicit return in the resolver that doesn’t return a mocked response composition (res) is treated as the instruction to perform that captured request as-is. Doing so will not produce any onUnhandledRequest warnings, as the request was handled, just not resolved to a mocked response.

What this task is about is to introduce an explicit API to state that you wish to perform the request as-is, opposed to having to rely on how the library interprets the return statement.

if (req.url.searchParams.get('skip')) {
  return res(ctx.forward())
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Bountysource
Add "ctx.forward" for forwarding (bypassing) requests.
Read more >
Strata CTX Standard Telephone User Guide
Call Forward Any Call - Set for Another Station – Enables you to forward all calls for another telephone within your telephone system....
Read more >
Servlet - forward() and sendRedirect() Method With Example
When we are forwarding the client request using the forward() ... to send the response to the other servlet bypassing the 'add' value....
Read more >
Configuring Client Forwarding Policies - Zscaler Help
Go to Administration > Client Forwarding Policy. Click Add Rule. The Add Client Forwarding Policy window appears. In the Add Client Forwarding Policy...
Read more >
Advanced Patterns — Click Documentation (5.x)
Group): def get_command(self, ctx, cmd_name): rv = click. ... Parameters (options and arguments) are forwarded to the command callbacks as you have seen....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found