|
| 1 | +--- |
| 2 | +permalink: /helpers/MockServer |
| 3 | +editLink: false |
| 4 | +sidebar: auto |
| 5 | +title: MockServer |
| 6 | +--- |
| 7 | + |
| 8 | +<!-- Generated by documentation.js. Update this documentation by updating the source code. --> |
| 9 | + |
| 10 | +## MockServer |
| 11 | + |
| 12 | +MockServer |
| 13 | + |
| 14 | +The MockServer Helper in CodeceptJS empowers you to mock any server or service via HTTP or HTTPS, making it an excellent tool for simulating REST endpoints and other HTTP-based APIs. |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +## Configuration |
| 19 | + |
| 20 | +This helper should be configured in codecept.conf.(js|ts) |
| 21 | + |
| 22 | +Type: [object][1] |
| 23 | + |
| 24 | +### Properties |
| 25 | + |
| 26 | +- `port` **[number][2]?** Mock server port |
| 27 | +- `host` **[string][3]?** Mock server host |
| 28 | +- `httpsOpts` **[object][1]?** key & cert values are the paths to .key and .crt files |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +#### Examples |
| 33 | + |
| 34 | +You can seamlessly integrate MockServer with other helpers like REST or Playwright. Here's a configuration example inside the `codecept.conf.js` file: |
| 35 | + |
| 36 | +```javascript |
| 37 | +{ |
| 38 | + helpers: { |
| 39 | + REST: {...}, |
| 40 | + MockServer: { |
| 41 | + // default mock server config |
| 42 | + port: 9393, |
| 43 | + host: '0.0.0.0', |
| 44 | + httpsOpts: { |
| 45 | + key: '', |
| 46 | + cert: '', |
| 47 | + }, |
| 48 | + }, |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +#### Adding Interactions |
| 54 | + |
| 55 | +Interactions add behavior to the mock server. Use the `I.addInteractionToMockServer()` method to include interactions. It takes an interaction object as an argument, containing request and response details. |
| 56 | + |
| 57 | +```javascript |
| 58 | +I.addInteractionToMockServer({ |
| 59 | + request: { |
| 60 | + method: 'GET', |
| 61 | + path: '/api/hello' |
| 62 | + }, |
| 63 | + response: { |
| 64 | + status: 200, |
| 65 | + body: { |
| 66 | + 'say': 'hello to mock server' |
| 67 | + } |
| 68 | + } |
| 69 | +}); |
| 70 | +``` |
| 71 | + |
| 72 | +#### Request Matching |
| 73 | + |
| 74 | +When a real request is sent to the mock server, it matches the received request with the interactions. If a match is found, it returns the specified response; otherwise, a 404 status code is returned. |
| 75 | + |
| 76 | +- Strong match on HTTP Method, Path, Query Params & JSON body. |
| 77 | +- Loose match on Headers. |
| 78 | + |
| 79 | +##### Strong Match on Query Params |
| 80 | + |
| 81 | +You can send different responses based on query parameters: |
| 82 | + |
| 83 | +```javascript |
| 84 | +I.addInteractionToMockServer({ |
| 85 | + request: { |
| 86 | + method: 'GET', |
| 87 | + path: '/api/users', |
| 88 | + queryParams: { |
| 89 | + id: 1 |
| 90 | + } |
| 91 | + }, |
| 92 | + response: { |
| 93 | + status: 200, |
| 94 | + body: 'user 1' |
| 95 | + } |
| 96 | +}); |
| 97 | + |
| 98 | +I.addInteractionToMockServer({ |
| 99 | + request: { |
| 100 | + method: 'GET', |
| 101 | + path: '/api/users', |
| 102 | + queryParams: { |
| 103 | + id: 2 |
| 104 | + } |
| 105 | + }, |
| 106 | + response: { |
| 107 | + status: 200, |
| 108 | + body: 'user 2' |
| 109 | + } |
| 110 | +}); |
| 111 | +``` |
| 112 | + |
| 113 | +- GET to `/api/users?id=1` will return 'user 1'. |
| 114 | +- GET to `/api/users?id=2` will return 'user 2'. |
| 115 | +- For all other requests, it returns a 404 status code. |
| 116 | + |
| 117 | +##### Loose Match on Body |
| 118 | + |
| 119 | +When `strict` is set to false, it performs a loose match on query params and response body: |
| 120 | + |
| 121 | +```javascript |
| 122 | +I.addInteractionToMockServer({ |
| 123 | + strict: false, |
| 124 | + request: { |
| 125 | + method: 'POST', |
| 126 | + path: '/api/users', |
| 127 | + body: { |
| 128 | + name: 'john' |
| 129 | + } |
| 130 | + }, |
| 131 | + response: { |
| 132 | + status: 200 |
| 133 | + } |
| 134 | +}); |
| 135 | +``` |
| 136 | + |
| 137 | +- POST to `/api/users` with the body containing `name` as 'john' will return a 200 status code. |
| 138 | +- POST to `/api/users` without the `name` property in the body will return a 404 status code. |
| 139 | + |
| 140 | +Happy testing with MockServer in CodeceptJS! 🚀 |
| 141 | + |
| 142 | +## Methods |
| 143 | + |
| 144 | +### Parameters |
| 145 | + |
| 146 | +- `passedConfig` |
| 147 | + |
| 148 | +### addInteractionToMockServer |
| 149 | + |
| 150 | +An interaction adds behavior to the mock server |
| 151 | + |
| 152 | +```js |
| 153 | +I.addInteractionToMockServer({ |
| 154 | + request: { |
| 155 | + method: 'GET', |
| 156 | + path: '/api/hello' |
| 157 | + }, |
| 158 | + response: { |
| 159 | + status: 200, |
| 160 | + body: { |
| 161 | + 'say': 'hello to mock server' |
| 162 | + } |
| 163 | + } |
| 164 | +}); |
| 165 | +``` |
| 166 | + |
| 167 | +```js |
| 168 | +// with query params |
| 169 | +I.addInteractionToMockServer({ |
| 170 | + request: { |
| 171 | + method: 'GET', |
| 172 | + path: '/api/hello', |
| 173 | + queryParams: { |
| 174 | + id: 2 |
| 175 | + } |
| 176 | + }, |
| 177 | + response: { |
| 178 | + status: 200, |
| 179 | + body: { |
| 180 | + 'say': 'hello to mock server' |
| 181 | + } |
| 182 | + } |
| 183 | +}); |
| 184 | +``` |
| 185 | + |
| 186 | +#### Parameters |
| 187 | + |
| 188 | +- `interaction` **(CodeceptJS.MockInteraction | [object][1])** add behavior to the mock server |
| 189 | + |
| 190 | +Returns **any** void |
| 191 | + |
| 192 | +### startMockServer |
| 193 | + |
| 194 | +Start the mock server |
| 195 | + |
| 196 | +#### Parameters |
| 197 | + |
| 198 | +- `port` **[number][2]?** start the mock server with given port |
| 199 | + |
| 200 | +Returns **any** void |
| 201 | + |
| 202 | +### stopMockServer |
| 203 | + |
| 204 | +Stop the mock server |
| 205 | + |
| 206 | +Returns **any** void |
| 207 | + |
| 208 | +[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object |
| 209 | + |
| 210 | +[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number |
| 211 | + |
| 212 | +[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String |
0 commit comments