All Projects → HugoDF → mock-express-request-response

HugoDF / mock-express-request-response

Licence: MIT license
Mock/stub the Express request/response objects using Jest or sinon

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to mock-express-request-response

ts-mock-imports
Intuitive mocking library for Typescript class imports
Stars: ✭ 103 (+66.13%)
Mutual labels:  sinon, sinon-stub
Sinon
Test spies, stubs and mocks for JavaScript.
Stars: ✭ 8,828 (+14138.71%)
Mutual labels:  sinon
Js Stack From Scratch
🛠️⚡ Step-by-step tutorial to build a modern JavaScript stack.
Stars: ✭ 18,814 (+30245.16%)
Mutual labels:  sinon
vue-popover
Reusable popover component for Vue
Stars: ✭ 22 (-64.52%)
Mutual labels:  sinon
cucumber-steps
🥒 Quick start for testing with Cucumber.js
Stars: ✭ 15 (-75.81%)
Mutual labels:  sinon
sinon-mongoose
Extend Sinon stubs for Mongoose methods to test chained methods easily
Stars: ✭ 87 (+40.32%)
Mutual labels:  sinon
react-testing-mocha-chai-enzyme
A solid test setup for React components with Mocha, Chai, Sinon, Enzyme in a Webpack/Babel application.
Stars: ✭ 48 (-22.58%)
Mutual labels:  sinon
js-stack-from-scratch
🌺 Russian translation of "JavaScript Stack from Scratch" from the React-Theming developers https://github.com/sm-react/react-theming
Stars: ✭ 394 (+535.48%)
Mutual labels:  sinon
floss
Unit-testing for those hard to reach places
Stars: ✭ 26 (-58.06%)
Mutual labels:  sinon
corewar
Typescript implementation of red code parser and core simulator for the classic game Corewar
Stars: ✭ 46 (-25.81%)
Mutual labels:  sinon
mock-spy-module-import
JavaScript import/require module testing do's and don'ts with Jest
Stars: ✭ 40 (-35.48%)
Mutual labels:  jest-mocking
firestore-jest-mock
Jest Helper library for mocking Cloud Firestore
Stars: ✭ 128 (+106.45%)
Mutual labels:  jest-mocking
jest-playground
Playing around with Jest - Subscribe to my YouTube channel: https://bit.ly/CognitiveSurge
Stars: ✭ 24 (-61.29%)
Mutual labels:  jest-mocking

Mock Express request/response with Jest or sinon

Full post at https://codewithhugo.com/express-request-response-mocking/

Requirements

  • Node 10
  • npm 6

Setup

  1. Clone the repository
  2. Run npm install
  3. Run npm start (to start the application locally) or npm t to run the tests.

npm scripts

  • npm t (npm run test) will run both ava and jest-based test suites.
  • npm start will start the application server (default PORT: 3000, can override using PORT environment variable)
  • npx jest will run just jest-based tests
  • npx ava will run just ava-based tests

Requests

Login

curl --request POST \
  --url http://localhost:3000/session \
  --header 'content-type: application/json' \
  --data '{
	"username": "hugo",
	"password": "boss"
}' -v

Sample Successful (200) Response:

> POST /session HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.54.0
> Accept: */*
> content-type: application/json
> Content-Length: 58
>
* upload completely sent off: 58 out of 58 bytes
< HTTP/1.1 201 Created
< X-Powered-By: Express
< Content-Type: application/json; charset=utf-8
< Set-Cookie: session=t_4OrqgrscRYVgGwtN0EMg.WmpPuJSiukSgV0iWS7oqg6a9rfsDTbtLcoQQiRkJyydfOjOI8HE9dP2kzcfTmRqR.1550427342962.3600000.Xajry447dwhSnzt1mXYN9SoYzd3PjTyo_Dwli5IrK6Y; path=/; expires=Fri, 15 Feb 2019 19:15:43 GMT; httponly
< Date: Fri, 15 Feb 2019 18:15:42 GMT
< Connection: keep-alive
< Content-Length: 0

What interests us is Set-Cookie: session=t_4OrqgrscRYVgGwtN0EMg... (truncated for readability).

This is an encrypted session (as created by client-sessions) contained in the session cookie.

Logout

curl --request DELETE \
  --url http://localhost:3000/session \
  --cookie session=*INSERT_OUTPUT_OF_SET_COOKIE_SESSION_LOGIN_REQUEST* \
  -v

Sample Successful Response:

> DELETE /session HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.54.0
> Accept: */*
> Cookie: session=t_4OrqgrscRYVgGwtN0EMg.WmpPuJSiukSgV0iWS7oqg6a9rfsDTbtLcoQQiRkJyydfOjOI8HE9dP2kzcfTmRqR.1550427342962.3600000.Xajry447dwhSnzt1mXYN9SoYzd3PjTyo_Dwli5IrK6Y
>
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: application/json; charset=utf-8
< Set-Cookie: session=97I-bC6WbilzHbqLhPJevg.vMfAWQscH6PChT-elMcYqy3vwtLcxKtTZ16X1abANHo.1550427342962.3600000.H6y03kGPA0Nd8sIJqDQHaOn4Rb377NOtOEGuGz9Ecu0; path=/; expires=Fri, 15 Feb 2019 19:15:43 GMT; httponly
< Date: Fri, 15 Feb 2019 18:19:13 GMT
< Connection: keep-alive
< Content-Length: 0
<

Again the interesting part of the response is Set-Cookie: session=97I-bC6WbilzHbqLhPJevg.vMfAWQscH6PChT... (truncated).

What the application code does is not actually clear the cookie, but override the contents of the cookie.

Therefore it sends back a Set-Cookie with this updated "session" (which is empty and GET /session using it will 401).

Check

curl --request GET \
  --url http://localhost:3000/session \
  --cookie session=*INSERT_OUTPUT_OF_SET_COOKIE_SESSION_LOGIN_REQUEST* \
  -v

Sample Successful (200) Response:

> GET /session HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.54.0
> Accept: */*
> Cookie: session=t_4OrqgrscRYVgGwtN0EMg.WmpPuJSiukSgV0iWS7oqg6a9rfsDTbtLcoQQiRkJyydfOjOI8HE9dP2kzcfTmRqR.1550427342962.3600000.Xajry447dwhSnzt1mXYN9SoYzd3PjTyo_Dwli5IrK6Y
>
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: application/json; charset=utf-8
< Content-Length: 19
< ETag: W/"13-NGIK6C7P0giZ5uHUWH1fsFMw4TY"
< Date: Sun, 17 Feb 2019 18:23:33 GMT
< Connection: keep-alive
<

{"username":"hugo"}

It reflects the username back to us from the session cookie.

Sample Fail (401) Response:

> GET /session HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.54.0
> Accept: */*
> Cookie: session=97I-bC6WbilzHbqLhPJevg.vMfAWQscH6PChT-elMcYqy3vwtLcxKtTZ16X1abANHo.1550427342962.3600000.H6y03kGPA0Nd8sIJqDQHaOn4Rb377NOtOEGuGz9Ecu0
>
< HTTP/1.1 401 Unauthorized
< X-Powered-By: Express
< Content-Type: application/json; charset=utf-8
< Date: Sun, 17 Feb 2019 18:25:38 GMT
< Connection: keep-alive
< Content-Length: 0
<

We're just interested in the 401 here 👍.

Header Authentication

curl --request GET \
  --url http://localhost:3000/session \
  --header 'authorization: Bearer 76b1e728-1c14-43f9-aa06-6de5cbc064c2' \
  -v

Sample Success (200) Response:

> GET /session HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.54.0
> Accept: */*
> authorization: Bearer 76b1e728-1c14-43f9-aa06-6de5cbc064c2
>
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: application/json; charset=utf-8
< Content-Length: 19
< ETag: W/"13-NGIK6C7P0giZ5uHUWH1fsFMw4TY"
< Set-Cookie: session=-oSa0xHGDBZARqmdrTWjTQ.rExU9YRLm7dqNt3UfhVVTpnFnG0o_D2ZAlp-xfk3-1XOCFi_7Dc7d-MK3AguoyY8.1550437501888.3600000.tue12japVIW6kiCQ9o8UfTfYnIj5G_2auyeJXdOlOR0; path=/; expires=Sun, 17 Feb 2019 22:05:02 GMT; httponly
< Date: Sun, 17 Feb 2019 21:05:01 GMT
< Connection: keep-alive
<
{"username":"hugo"}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].