Exercise 2
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
Lesson Transcript
[00:00 - 00:18] We have an exercise that will help you understand and implement a Midewell function in Express. By the end of this exercise you'll be able to create unit tests for a Midewell function that checks for a B-R-Tokin in the Request header and handles different scenarios based on its validity.
[00:19 - 00:37] A Midewell function in Express is a function that sits between the initial request and the final route under. It has access to the request and response object and can perform additional operations or modifications before passing control to the next Midewell or the final handler.
[00:38 - 00:56] In this exercise you will create a unit test for a Midewell function that checks if the Request header contains a B-R-Tokin. A Midewell function receives the request, response and next function as parameters. It first checks if the Request header contains a B-R-Tokin.
[00:57 - 01:16] If the token is missing, a Midewell function will return a 400 status code indicating a bad request. As for the case where a B-R-Tokin is presentable invalid, we'll also send the response with 400 status message and this JSON.
[01:17 - 01:28] The entry points are the request, the response and the next function. The request and response are objects while the next is a function.
[01:29 - 01:48] To test our Midewell function, you need to create test cases that cover different scenarios. In this exercise you will need to focus on mocking the JWT the verify and also asserting that the next method was called.
[01:49 - 02:06] In summary, this exercise we allow you to understand and implement test doubles and test the Midewell function in Express. By creating a Midewell function that checks for a B-R-Tokin we can enhance the security and authentication of our API endpoints.
[02:07 - 02:17] Through proper testing, including mocking the JWT the verify and asserting the next function we can ensure the correct behavior of a Midewell function. See you in the next lesson.
This lesson preview is part of the Pain Free Mocking with Jest course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to Pain Free Mocking with Jest, plus 70+ \newline books, guides and courses with the \newline Pro subscription.
![Thumbnail for the \newline course Pain Free Mocking with Jest](/_next/image?url=https%3A%2F%2Fs3.amazonaws.com%2Fassets.fullstack.io%2Fn%2F20240214201947577_AkinwaleHabib_PainFreeMockingwithJest_1450.67x816_CoverImage.png&w=3840&q=75)