mockaton 8.3.1 → 8.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/Mockaton.test.js +17 -7
- package/src/ProxyRelay.js +5 -3
package/README.md
CHANGED
package/package.json
CHANGED
package/src/Mockaton.test.js
CHANGED
|
@@ -9,6 +9,7 @@ import { writeFileSync, mkdtempSync, mkdirSync } from 'node:fs'
|
|
|
9
9
|
import { Config } from './Config.js'
|
|
10
10
|
import { mimeFor } from './utils/mime.js'
|
|
11
11
|
import { Mockaton } from './Mockaton.js'
|
|
12
|
+
import { readBody } from './utils/http-request.js'
|
|
12
13
|
import { Commander } from './Commander.js'
|
|
13
14
|
import { parseFilename } from './Filename.js'
|
|
14
15
|
import { CorsHeader } from './utils/http-cors.js'
|
|
@@ -421,19 +422,28 @@ async function testInvalidFilenamesAreIgnored() {
|
|
|
421
422
|
|
|
422
423
|
async function testEnableFallbackSoRoutesWithoutMocksGetRelayed() {
|
|
423
424
|
await describe('Fallback', async () => {
|
|
424
|
-
const fallbackServer = createServer((
|
|
425
|
-
response.
|
|
426
|
-
|
|
427
|
-
|
|
425
|
+
const fallbackServer = createServer(async (req, response) => {
|
|
426
|
+
response.writeHead(423, {
|
|
427
|
+
'custom_header': 'my_custom_header',
|
|
428
|
+
'set-cookie': [
|
|
429
|
+
'cookieA=A',
|
|
430
|
+
'cookieB=B'
|
|
431
|
+
]
|
|
432
|
+
})
|
|
433
|
+
response.end(await readBody(req)) // echoes they req body payload
|
|
428
434
|
})
|
|
429
435
|
await promisify(fallbackServer.listen).bind(fallbackServer, 0, '127.0.0.1')()
|
|
430
436
|
|
|
431
437
|
await commander.setProxyFallback(`http://localhost:${fallbackServer.address().port}`)
|
|
432
438
|
await it('Relays to fallback server', async () => {
|
|
433
|
-
const res = await request('/non-existing-mock'
|
|
434
|
-
|
|
439
|
+
const res = await request('/non-existing-mock', {
|
|
440
|
+
method: 'POST',
|
|
441
|
+
body: 'text_body'
|
|
442
|
+
})
|
|
435
443
|
equal(res.status, 423)
|
|
436
|
-
equal(
|
|
444
|
+
equal(res.headers.get('custom_header'), 'my_custom_header')
|
|
445
|
+
equal(res.headers.get('set-cookie'), ['cookieA=A', 'cookieB=B'].join(', '))
|
|
446
|
+
equal(await res.text(), 'text_body')
|
|
437
447
|
fallbackServer.close()
|
|
438
448
|
})
|
|
439
449
|
})
|
package/src/ProxyRelay.js
CHANGED
|
@@ -10,12 +10,14 @@ export async function proxy(req, response) {
|
|
|
10
10
|
const proxyResponse = await fetch(Config.proxyFallback + req.url, {
|
|
11
11
|
method: req.method,
|
|
12
12
|
headers: req.headers,
|
|
13
|
-
body: req.method === 'GET' || req.method === 'HEAD'
|
|
13
|
+
body: req.method === 'GET' || req.method === 'HEAD'
|
|
14
14
|
? undefined
|
|
15
15
|
: await readBody(req)
|
|
16
16
|
})
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
|
|
18
|
+
const headers = Object.fromEntries(proxyResponse.headers)
|
|
19
|
+
headers['set-cookie'] = proxyResponse.headers.getSetCookie() // parses multiple into an array
|
|
20
|
+
response.writeHead(proxyResponse.status, headers)
|
|
19
21
|
const body = await proxyResponse.text()
|
|
20
22
|
response.end(body)
|
|
21
23
|
|