Intercept and Mock Server-Side HTTP Requests in MSW

Share this video with your friends

Social Share Links

Send Tweet
Published 7 months ago
Updated 7 months ago

To control what happens to requests in Mock Service Worker, you need to intercept them first. Write your first request handler and learn how to intercept a server-side request our application makes in Remix's loader. And mocking a response? Using a Fetch API Response instance, of course!

Instructor: [0:00] On our home page, we have a loader function that tells Remix to make a server-side request to fetch the list of featured movies, but when we go to that page, we can see a request error because we're trying to make a request against the API that doesn't exist. Let's change that with MSW.

[0:15] Go to the handlers module and import { http } from 'msw'. We will use the http object to intercept http requests. Then in the list of handlers, type http, and take a look at the methods on this object. They represent http request methods. We will choose get because we're making a get request on the server, and call that method.

[0:37] As the first argument, we need to provide the request URL we wish to intercept. I will copy it directly from the remix loader. Then as the second argument, provide a callback function that will be called whenever this request happens. For now, let's print a console message in reaction to this request.

[0:55] We have just written a request handler. It's a function that controls the network and consists of two parts, a predicate that describes which requests to intercept, and a response resolver, a function that tells MSW how to handle this request.

[1:10] Let's verify that our request handler is working. Since the request we want to intercept happens on the server, we should be looking for this console message in the server logs in our Terminal.

[1:20] Once we refresh the page, we can see the Remix server printing our message, but the FetchError still remains because we haven't handled this request in any way just yet. To respond to an intercepted request, return a new Fetch API response instance from the Response resolver.

[1:37] In this case, our application expects a JSON response with the list of movies. I'm going to copy them right there, and I'm going to provide a Content-Type response header to specify that we're returning a JSON response. Save the changes, and refresh the page.

[1:55] What happens now is that our server makes a request, and MSW intercepts it and tries to find any matching request handlers for it. When it does, it executes the Response resolver function and uses any response it returns as the actual response our application receives.

~ 4 months ago

Where can I find the JSON string for the data?

Artem Zakharchenko
Artem Zakharchenkoinstructor
~ 4 months ago

Hi. You can find all the materials, including the JSON I used for mock data, on GitHub: https://github.com/kettanaito/movie-app. Checkout the "completed" branch to switch to the final state of the application, which will include the JSON you're looking for.

Let me know if you have any other questions!

Markdown supported.
Become a member to join the discussionEnroll Today