|
1 | 1 | # Mock Express request/response with Jest or sinon |
| 2 | + |
| 3 | +## Requests |
| 4 | + |
| 5 | +### Login |
| 6 | + |
| 7 | +```sh |
| 8 | +curl --request POST \ |
| 9 | + --url http://localhost:3000/session \ |
| 10 | + --header 'content-type: application/json' \ |
| 11 | + --data '{ |
| 12 | + "username": "hugo", |
| 13 | + "password": "boss" |
| 14 | +}' -v |
| 15 | +``` |
| 16 | + |
| 17 | +Sample Successful (200) Response: |
| 18 | + |
| 19 | +```sh |
| 20 | +> POST /session HTTP/1.1 |
| 21 | +> Host: localhost:3000 |
| 22 | +> User-Agent: curl/7.54.0 |
| 23 | +> Accept: */* |
| 24 | +> content-type: application/json |
| 25 | +> Content-Length: 58 |
| 26 | +> |
| 27 | +* upload completely sent off: 58 out of 58 bytes |
| 28 | +< HTTP/1.1 201 Created |
| 29 | +< X-Powered-By: Express |
| 30 | +< Content-Type: application/json; charset=utf-8 |
| 31 | +< Set-Cookie: session=t_4OrqgrscRYVgGwtN0EMg.WmpPuJSiukSgV0iWS7oqg6a9rfsDTbtLcoQQiRkJyydfOjOI8HE9dP2kzcfTmRqR.1550427342962.3600000.Xajry447dwhSnzt1mXYN9SoYzd3PjTyo_Dwli5IrK6Y; path=/; expires=Fri, 15 Feb 2019 19:15:43 GMT; httponly |
| 32 | +< Date: Fri, 15 Feb 2019 18:15:42 GMT |
| 33 | +< Connection: keep-alive |
| 34 | +< Content-Length: 0 |
| 35 | +``` |
| 36 | + |
| 37 | +What interests us is `Set-Cookie: session=t_4OrqgrscRYVgGwtN0EMg...` (truncated for readability). |
| 38 | + |
| 39 | +This is an encrypted session (as created by `client-sessions`) contained in the `session` cookie. |
| 40 | + |
| 41 | +### Logout |
| 42 | + |
| 43 | +```sh |
| 44 | +curl --request DELETE \ |
| 45 | + --url http://localhost:3000/session \ |
| 46 | + --cookie session=*INSERT_OUTPUT_OF_SET_COOKIE_SESSION_LOGIN_REQUEST* \ |
| 47 | + -v |
| 48 | +``` |
| 49 | + |
| 50 | +Sample Successful Response: |
| 51 | +```sh |
| 52 | +> DELETE /session HTTP/1.1 |
| 53 | +> Host: localhost:3000 |
| 54 | +> User-Agent: curl/7.54.0 |
| 55 | +> Accept: */* |
| 56 | +> Cookie: session=t_4OrqgrscRYVgGwtN0EMg.WmpPuJSiukSgV0iWS7oqg6a9rfsDTbtLcoQQiRkJyydfOjOI8HE9dP2kzcfTmRqR.1550427342962.3600000.Xajry447dwhSnzt1mXYN9SoYzd3PjTyo_Dwli5IrK6Y |
| 57 | +> |
| 58 | +< HTTP/1.1 200 OK |
| 59 | +< X-Powered-By: Express |
| 60 | +< Content-Type: application/json; charset=utf-8 |
| 61 | +< Set-Cookie: session=97I-bC6WbilzHbqLhPJevg.vMfAWQscH6PChT-elMcYqy3vwtLcxKtTZ16X1abANHo.1550427342962.3600000.H6y03kGPA0Nd8sIJqDQHaOn4Rb377NOtOEGuGz9Ecu0; path=/; expires=Fri, 15 Feb 2019 19:15:43 GMT; httponly |
| 62 | +< Date: Fri, 15 Feb 2019 18:19:13 GMT |
| 63 | +< Connection: keep-alive |
| 64 | +< Content-Length: 0 |
| 65 | +< |
| 66 | +``` |
| 67 | + |
| 68 | +Again the interesting part of the response is `Set-Cookie: session=97I-bC6WbilzHbqLhPJevg.vMfAWQscH6PChT...` (truncated). |
| 69 | + |
| 70 | +What the application code does is not actually clear the cookie, but override the contents of the cookie. |
| 71 | + |
| 72 | +Therefore it sends back a `Set-Cookie` with this updated "session" (which is empty and `GET /session` using it will 401). |
| 73 | + |
| 74 | +### Check |
| 75 | + |
| 76 | +```sh |
| 77 | +curl --request GET \ |
| 78 | + --url http://localhost:3000/session \ |
| 79 | + --cookie session=*INSERT_OUTPUT_OF_SET_COOKIE_SESSION_LOGIN_REQUEST* \ |
| 80 | + -v |
| 81 | +``` |
| 82 | + |
| 83 | +Sample Successful (200) Response: |
| 84 | + |
| 85 | +```sh |
| 86 | +> GET /session HTTP/1.1 |
| 87 | +> Host: localhost:3000 |
| 88 | +> User-Agent: curl/7.54.0 |
| 89 | +> Accept: */* |
| 90 | +> Cookie: session=t_4OrqgrscRYVgGwtN0EMg.WmpPuJSiukSgV0iWS7oqg6a9rfsDTbtLcoQQiRkJyydfOjOI8HE9dP2kzcfTmRqR.1550427342962.3600000.Xajry447dwhSnzt1mXYN9SoYzd3PjTyo_Dwli5IrK6Y |
| 91 | +> |
| 92 | +< HTTP/1.1 200 OK |
| 93 | +< X-Powered-By: Express |
| 94 | +< Content-Type: application/json; charset=utf-8 |
| 95 | +< Content-Length: 19 |
| 96 | +< ETag: W/"13-NGIK6C7P0giZ5uHUWH1fsFMw4TY" |
| 97 | +< Date: Sun, 17 Feb 2019 18:23:33 GMT |
| 98 | +< Connection: keep-alive |
| 99 | +< |
| 100 | + |
| 101 | +{"username":"hugo"} |
| 102 | +``` |
| 103 | + |
| 104 | +It reflects the username back to us from the session cookie. |
| 105 | + |
| 106 | +Sample Fail (401) Response: |
| 107 | + |
| 108 | +```sh |
| 109 | +> GET /session HTTP/1.1 |
| 110 | +> Host: localhost:3000 |
| 111 | +> User-Agent: curl/7.54.0 |
| 112 | +> Accept: */* |
| 113 | +> Cookie: session=97I-bC6WbilzHbqLhPJevg.vMfAWQscH6PChT-elMcYqy3vwtLcxKtTZ16X1abANHo.1550427342962.3600000.H6y03kGPA0Nd8sIJqDQHaOn4Rb377NOtOEGuGz9Ecu0 |
| 114 | +> |
| 115 | +< HTTP/1.1 401 Unauthorized |
| 116 | +< X-Powered-By: Express |
| 117 | +< Content-Type: application/json; charset=utf-8 |
| 118 | +< Date: Sun, 17 Feb 2019 18:25:38 GMT |
| 119 | +< Connection: keep-alive |
| 120 | +< Content-Length: 0 |
| 121 | +< |
| 122 | +``` |
| 123 | + |
| 124 | +We're just interested in the 401 here :+1:. |
0 commit comments