Skip to content

Commit

Permalink
chore: use withHandlers, merge tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Oct 29, 2024
1 parent d45c219 commit ba81836
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
4 changes: 2 additions & 2 deletions test/traffic/fixtures/archives/response-cloning.har
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
"size": 14,
"mimeType": "text/html",
"compression": 0,
"text": "one"
"text": "first"
},
"redirectURL": "",
"headersSize": 236,
Expand Down Expand Up @@ -317,7 +317,7 @@
"size": 14,
"mimeType": "text/html",
"compression": 0,
"text": "two"
"text": "latest"
},
"redirectURL": "",
"headersSize": 236,
Expand Down
15 changes: 0 additions & 15 deletions test/traffic/response-cloning.test.ts

This file was deleted.

34 changes: 32 additions & 2 deletions test/traffic/response-order.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fromTraffic } from '../../src/traffic/from-traffic.js'
import { InspectedHandler, inspectHandlers } from '../support/inspect.js'
import { withHandlers } from '../support/with-handlers.js'
import { _toHeaders, normalizeLocalhost, readArchive } from './utils/index.js'

it('respects the response sequence when repeatedly requesting the same endpoint', async () => {
Expand All @@ -15,7 +16,7 @@ it('respects the response sequence when repeatedly requesting the same endpoint'
response: {
status: 200,
statusText: 'OK',
headers: _toHeaders(har.log.entries[0].response.headers),
headers: _toHeaders(har.log.entries[0]!.response.headers),
body: 'one',
},
},
Expand All @@ -28,9 +29,38 @@ it('respects the response sequence when repeatedly requesting the same endpoint'
response: {
status: 200,
statusText: 'OK',
headers: _toHeaders(har.log.entries[1].response.headers),
headers: _toHeaders(har.log.entries[1]!.response.headers),
body: 'two',
},
},
])
})

it('responds with the latest response for subsequent requests', async () => {
const har = readArchive('test/traffic/fixtures/archives/response-cloning.har')
const handlers = fromTraffic(har, normalizeLocalhost)

{
const response = await withHandlers(handlers, () => {
return fetch('http://localhost/resource')
})
// First, must respond with the first recorded response.
await expect(response.text()).resolves.toBe('first')
}

// Any subsequent requests receive the latest response.
{
const response = await withHandlers(handlers, () => {
return fetch('http://localhost/resource')
})
await expect(response.text()).resolves.toBe('latest')
}

// Any subsequent requests receive the latest response.
{
const response = await withHandlers(handlers, () => {
return fetch('http://localhost/resource')
})
await expect(response.text()).resolves.toBe('latest')
}
})

0 comments on commit ba81836

Please sign in to comment.