Skip to content

Commit 1645767

Browse files
authored
fix: support non-configurable response status codes (#90)
1 parent a2a376f commit 1645767

File tree

6 files changed

+206
-2
lines changed

6 files changed

+206
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"vitest": "^4.0.8"
7272
},
7373
"dependencies": {
74+
"@mswjs/interceptors": "^0.40.0",
7475
"@stoplight/json": "^3.21.7",
7576
"@types/har-format": "^1.2.16",
7677
"@yellow-ticket/seed-json-schema": "^0.1.7",

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/open-api/utils/open-api-utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ResponseResolver } from 'msw'
2+
import { FetchResponse } from '@mswjs/interceptors'
23
import { OpenAPI, OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'
34
import { seedSchema } from '@yellow-ticket/seed-json-schema'
45
import { toString } from './to-string.js'
@@ -50,7 +51,7 @@ export function createResponseResolver(
5051

5152
const normalizedStatus = Number(responseStatus || '200')
5253

53-
return new Response(toBody(request, responseObject), {
54+
return new FetchResponse(toBody(request, responseObject), {
5455
status: normalizedStatus,
5556
statusText: STATUS_CODES[normalizedStatus],
5657
headers: toHeaders(request, responseObject),

src/traffic/utils/har-utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type Har from 'har-format'
2+
import { FetchResponse } from '@mswjs/interceptors'
23
import { decodeBase64String } from './base64-utils.js'
34

45
export function toHeaders(harHeaders: Array<Har.Header>): Headers {
@@ -41,7 +42,7 @@ export function toResponse(responseEntry: Har.Response): Response {
4142
}
4243

4344
const body = toResponseBody(responseEntry.content)
44-
const response = new Response(body, {
45+
const response = new FetchResponse(body, {
4546
status: responseEntry.status,
4647
statusText: responseEntry.statusText,
4748
headers: toHeaders(responseEntry.headers),
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
{
2+
"log": {
3+
"version": "1.2",
4+
"creator": {
5+
"name": "WebInspector",
6+
"version": "537.36"
7+
},
8+
"pages": [],
9+
"entries": [
10+
{
11+
"_initiator": {
12+
"type": "script",
13+
"stack": {
14+
"callFrames": [
15+
{
16+
"functionName": "eval",
17+
"scriptId": "27",
18+
"url": "",
19+
"lineNumber": 3,
20+
"columnNumber": 39
21+
},
22+
{
23+
"functionName": "eval",
24+
"scriptId": "27",
25+
"url": "",
26+
"lineNumber": 1,
27+
"columnNumber": 56
28+
},
29+
{
30+
"functionName": "evaluate",
31+
"scriptId": "20",
32+
"url": "",
33+
"lineNumber": 340,
34+
"columnNumber": 15
35+
},
36+
{
37+
"functionName": "",
38+
"scriptId": "26",
39+
"url": "",
40+
"lineNumber": 0,
41+
"columnNumber": 43
42+
}
43+
]
44+
}
45+
},
46+
"_priority": "High",
47+
"_resourceType": "websocket",
48+
"cache": {},
49+
"connection": "29",
50+
"request": {
51+
"method": "GET",
52+
"url": "http://127.0.0.1:60906/status",
53+
"httpVersion": "HTTP/1.1",
54+
"headers": [
55+
{
56+
"name": "Host",
57+
"value": "127.0.0.1:60906"
58+
},
59+
{
60+
"name": "Connection",
61+
"value": "keep-alive"
62+
},
63+
{
64+
"name": "sec-ch-ua",
65+
"value": "\"Chromium\";v=\"93\", \" Not;A Brand\";v=\"99\""
66+
},
67+
{
68+
"name": "sec-ch-ua-mobile",
69+
"value": "?0"
70+
},
71+
{
72+
"name": "User-Agent",
73+
"value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4576.0 Safari/537.36"
74+
},
75+
{
76+
"name": "sec-ch-ua-platform",
77+
"value": "\"macOS\""
78+
},
79+
{
80+
"name": "Accept",
81+
"value": "*/*"
82+
},
83+
{
84+
"name": "Origin",
85+
"value": "null"
86+
},
87+
{
88+
"name": "Sec-Fetch-Site",
89+
"value": "cross-site"
90+
},
91+
{
92+
"name": "Sec-Fetch-Mode",
93+
"value": "cors"
94+
},
95+
{
96+
"name": "Sec-Fetch-Dest",
97+
"value": "empty"
98+
},
99+
{
100+
"name": "Accept-Encoding",
101+
"value": "gzip, deflate, br"
102+
},
103+
{
104+
"name": "Accept-Language",
105+
"value": "en-US,en;q=0.9"
106+
}
107+
],
108+
"queryString": [],
109+
"cookies": [],
110+
"headersSize": 473,
111+
"bodySize": 0
112+
},
113+
"response": {
114+
"status": 101,
115+
"statusText": "Continue",
116+
"httpVersion": "HTTP/1.1",
117+
"headers": [
118+
{
119+
"name": "X-Powered-By",
120+
"value": "Express"
121+
},
122+
{
123+
"name": "Access-Control-Allow-Origin",
124+
"value": "*"
125+
},
126+
{
127+
"name": "Content-Type",
128+
"value": "text/html; charset=utf-8"
129+
},
130+
{
131+
"name": "Content-Length",
132+
"value": "11"
133+
},
134+
{
135+
"name": "ETag",
136+
"value": "W/\"b-Kq5sNclPz7QV2+lfQIuc6R7oRu0\""
137+
},
138+
{
139+
"name": "Date",
140+
"value": "Thu, 12 Aug 2021 13:35:57 GMT"
141+
},
142+
{
143+
"name": "Connection",
144+
"value": "keep-alive"
145+
}
146+
],
147+
"cookies": [],
148+
"content": {},
149+
"redirectURL": "",
150+
"headersSize": 236,
151+
"bodySize": 11,
152+
"_transferSize": 247,
153+
"_error": null
154+
},
155+
"serverIPAddress": "127.0.0.1",
156+
"startedDateTime": "2021-08-12T13:35:57.824Z",
157+
"time": 9.306000000050409,
158+
"timings": {
159+
"blocked": 0.6450000005667098,
160+
"dns": 0.0050000000000000044,
161+
"ssl": -1,
162+
"connect": 0.157,
163+
"send": 0.08600000000000002,
164+
"wait": 8.125000000210829,
165+
"receive": 0.2879999992728699,
166+
"_blocked_queueing": 0.4960000005667098
167+
}
168+
}
169+
]
170+
}
171+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { fromTraffic } from '../../src/traffic/from-traffic.js'
2+
import { readArchive, normalizeLocalhost, _toHeaders } from './utils/index.js'
3+
import { InspectedHandler, inspectHandlers } from '../support/inspect.js'
4+
5+
/**
6+
* @see https://github.com/mswjs/source/issues/54
7+
*/
8+
it('supports non-configurable response status codes', async () => {
9+
const har = readArchive(
10+
'test/traffic/fixtures/archives/response-status-101.har',
11+
)
12+
const handlers = fromTraffic(har, normalizeLocalhost)
13+
expect(await inspectHandlers(handlers)).toEqual<InspectedHandler[]>([
14+
{
15+
handler: {
16+
method: 'GET',
17+
path: 'http://localhost/status',
18+
},
19+
response: {
20+
status: 101,
21+
statusText: 'Continue',
22+
headers: _toHeaders(har.log.entries[0].response.headers),
23+
body: '',
24+
},
25+
},
26+
])
27+
})

0 commit comments

Comments
 (0)