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.

Middleware for Requests

See original GitHub issue

Is your feature request related to a problem? Please describe. Middlewares would help in building dev tools/extend MSW without having to fork the repo.

Let’s say we want to “monitor” all the requests that are responded by the server. So that we can assert the requests made. Middleware/emitting responded event would help in building that.

Describe the solution you’d like Should be able to add “middleware” or event emitted.

server.middlewares(middleware)

Describe alternatives you’ve considered Right now to achieve this, we can add some logic to catch request in each handler.

let loginRequests = [];

server.use(
    rest.post('/login', (req, res, ctx) => {
        loginRequests.push(req);
        return res(ctx.json({ data: 'data'}));
    }),
);

But this would have to duplicated and we can’t reliably abstract this logic out.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
kettanaitocommented, May 2, 2021

@Secretmapper you can encapsulate this logic into a high-order resolver:

import { setupWorker, rest } from 'msw'

function withError(resolver) {
  return (req, res, ctx) => {
    // Customize the predicate and response error
    // according to your needs.
    if ('shouldError' in req.cookies) {
      return res(ctx.status(403))
    }

    return resolver(req, res, ctx)
  }
}

const worker = setupWorker(
  rest.get('/user', withError((req, res, ctx) => {
    return res(ctx.json({ success: true }))
  }))
)

worker.start()
3reactions
kettanaitocommented, Nov 30, 2020

We can establish an event-based API that could also support request start/finish notifications (#388) for users to execute their logic.

const worker = setupWorker(...)
worker.events.on('request-start', listener)
worker.events.on('request-end', listener)

You could then use this API in your tests to monitor all requests like so:

const loginRequests = []
const server = setupServer(...)

server.events.on('request-intercepted', (req) => {
  loginRequests.push(req)
})

Would this work for your use case?

Read more comments on GitHub >

github_iconTop Results From Across the Web

requests-middleware - PyPI
TL;DR: requests-middleware is a custom transport adapter that allows simple composition of HTTP interactions. The python-requests library makes excellent ...
Read more >
ASP.NET Core Middleware | Microsoft Learn
Middleware is software that's assembled into an app pipeline to handle requests and responses. Each component: ... Request delegates are used to ...
Read more >
jmcarp/requests-middleware: Composable plugins ... - GitHub
TL;DR: requests-middleware is a custom transport adapter that allows simple composition of HTTP interactions. The python-requests library makes excellent ...
Read more >
Writing middleware for use in Express apps
Middleware functions are functions that have access to the request object ( req ), the response object ( res ), and the next...
Read more >
Conditional Middleware based on request in ASP.NET Core
The top-most piece of middleware in your application will always get called for each request. This is done automatically by the framework. This...
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