Skip to content

Commit 7cad86a

Browse files
authored
fix: treat CONNECT requests as the transport mechanism (#2796)
1 parent 975cb45 commit 7cad86a

10 files changed

Lines changed: 490 additions & 13 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
"dependencies": {
141141
"@inquirer/confirm": "^6.0.11",
142142
"@msw/url": "^0.1.2",
143-
"@mswjs/interceptors": "^0.45.5",
143+
"@mswjs/interceptors": "^0.45.6",
144144
"cookie": "^2.0.1",
145145
"headers-polyfill": "^5.0.1",
146146
"is-node-process": "^1.2.0",

‎pnpm-lock.yaml‎

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

‎src/core/experimental/frames/http-frame.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { FetchRequest } from '@mswjs/interceptors'
12
import { http } from '#http/http'
23
import { graphql } from '../../../graphql'
34
import { ws } from '../../../ws'
@@ -169,6 +170,54 @@ test('resolves a non-matching request', async () => {
169170
])
170171
})
171172

173+
/**
174+
* @todo Revisit the `http.all()` handler below once `http.connect()`
175+
* is supported: a matching CONNECT handler must take precedence.
176+
*/
177+
test('passes through an unhandled CONNECT request without applying the unhandled frame strategy', async () => {
178+
class HttpFrame extends HttpNetworkFrame {
179+
respondWith = vi.fn()
180+
passthrough = vi.fn()
181+
errorWith = vi.fn()
182+
}
183+
184+
const frame = new HttpFrame({
185+
request: new FetchRequest('http://localhost/127.0.0.1:443', {
186+
method: 'CONNECT',
187+
}),
188+
})
189+
const { events } = spyOnNetworkFrame(frame)
190+
const unhandledFrameCallback = vi.fn()
191+
192+
const matches = await frame.resolve(
193+
[
194+
http.all('*', () => {
195+
return new Response(null, { status: 204 })
196+
}),
197+
],
198+
unhandledFrameCallback,
199+
{ quiet: true },
200+
)
201+
202+
expect.soft(matches).toBe(null)
203+
expect.soft(frame.passthrough).toHaveBeenCalledOnce()
204+
expect.soft(frame.respondWith).not.toHaveBeenCalled()
205+
expect.soft(frame.errorWith).not.toHaveBeenCalled()
206+
expect.soft(unhandledFrameCallback).not.toHaveBeenCalled()
207+
expect.soft(events).toEqual([
208+
expect.objectContaining({
209+
type: 'request:start',
210+
requestId: frame.data.id,
211+
request: frame.data.request,
212+
}),
213+
expect.objectContaining({
214+
type: 'request:end',
215+
requestId: frame.data.id,
216+
request: frame.data.request,
217+
}),
218+
])
219+
})
220+
172221
test('resolves a matched passthrough', async () => {
173222
class HttpFrame extends HttpNetworkFrame {
174223
respondWith = vi.fn()

‎src/core/experimental/frames/http-frame.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type NetworkFrameResolutionContext,
77
} from './network-frame'
88
import { toPublicUrl } from '../../utils/request/to-public-url'
9+
import { isStringEqual } from '../../utils/internal/is-string-equal'
910
import { executeHandlers } from '../../utils/execute-handlers'
1011
import { storeResponseCookies } from '../../utils/request/store-response-cookies'
1112
import { shouldBypassRequest } from '../request-utils'
@@ -191,6 +192,24 @@ export abstract class HttpNetworkFrame extends NetworkFrame<
191192

192193
// No matching handlers.
193194
if (lookupResult == null) {
195+
/**
196+
* @note Unhandled CONNECT requests establish a tunnel through a proxy.
197+
* They are transport, not the request the developer cares about, so
198+
* they pass through silently regardless of the unhandled frame strategy.
199+
* The request sent through the tunnel gets its own resolution.
200+
*/
201+
if (isStringEqual(request.method, 'CONNECT')) {
202+
this.events.emit(
203+
new RequestEvent('request:end', {
204+
requestId,
205+
request,
206+
}),
207+
)
208+
209+
this.passthrough()
210+
return null
211+
}
212+
194213
this.events.emit(
195214
new RequestEvent('request:unhandled', {
196215
requestId,

‎src/http/http-handler.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @vitest-environment jsdom
2-
import { createRequestId } from '@mswjs/interceptors'
2+
import { createRequestId, FetchRequest } from '@mswjs/interceptors'
33
import type { HttpRequestResolverExtras } from './http-handler'
44
import { HttpHandler } from './http-handler'
55
import { HttpResponse } from './index'
@@ -70,6 +70,24 @@ describe('parse', () => {
7070
})
7171

7272
describe('predicate', () => {
73+
/**
74+
* @todo Decide whether `http.all()` matches CONNECT requests
75+
* once `http.connect()` is supported.
76+
*/
77+
test('returns false given a CONNECT request', async () => {
78+
const handler = new HttpHandler(/.+/, '*', resolver)
79+
const request = new FetchRequest('http://localhost/127.0.0.1:443', {
80+
method: 'CONNECT',
81+
})
82+
83+
await expect(
84+
handler.predicate({
85+
request,
86+
parsedResult: await handler.parse({ request }),
87+
}),
88+
).resolves.toBe(false)
89+
})
90+
7391
test('returns true given a matching request', async () => {
7492
const handler = new HttpHandler('POST', '/login', resolver)
7593
const request = new Request(new URL('/login', location.href), {

‎src/http/http-handler.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ export class HttpHandler extends RequestHandler<
122122
request: Request
123123
resolutionContext?: ResponseResolutionContext
124124
}) {
125+
/**
126+
* @note CONNECT requests target an authority ("host:port"), not a URL.
127+
* HTTP handlers never match them.
128+
* @todo Match the authority against the handler path once
129+
* `http.connect()` is supported instead of bailing out.
130+
*/
131+
if (isStringEqual(args.request.method, 'CONNECT')) {
132+
return {
133+
match: { matches: false, params: {} },
134+
cookies: {},
135+
}
136+
}
137+
125138
const url = new URL(args.request.url)
126139
const cookies = getAllRequestCookies(args.request)
127140

‎src/vite/plugin.test.ts‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,9 @@ document.querySelector('output').textContent = String(network.readyState)
454454
let completedUpdates = 0
455455
456456
if (import.meta.hot) {
457+
import.meta.hot.on('vite:beforeUpdate', () => {
458+
document.body.dataset.disabledBeforeUpdate = String(network.readyState === 0)
459+
})
457460
import.meta.hot.on('vite:afterUpdate', () => {
458461
document.body.dataset.completedUpdates = String(++completedUpdates)
459462
})
@@ -519,18 +522,33 @@ document.querySelector('button').onclick = async () => {
519522
await page.getByRole('button', { name: 'Enable' }).click()
520523

521524
await expect.poll(() => page.locator('output').textContent()).toBe('mocked')
525+
await page.evaluate(() => {
526+
delete document.body.dataset.disabledBeforeUpdate
527+
})
522528
server.environments.client.hot.send({ type: 'update', updates: [] })
523529

524530
await expect
525531
.poll(() => page.locator('body').getAttribute('data-completed-updates'))
526532
.toBe('2')
527533
await expect(
528-
page.evaluate(async () => {
529-
const response = await fetch('/resource')
530-
531-
return response.text()
532-
}),
533-
).resolves.toBe('mocked')
534+
page.locator('body').getAttribute('data-disabled-before-update'),
535+
).resolves.toBe('true')
536+
/**
537+
* @note The runtime re-enables the network after the update asynchronously:
538+
* the worker has to be re-activated before it can respond with mocks.
539+
* Until then, requests reach the dev server. Poll for the mocked response.
540+
*/
541+
await expect
542+
.poll(
543+
() =>
544+
page.evaluate(async () => {
545+
const response = await fetch('/resource')
546+
547+
return response.text()
548+
}),
549+
{ timeout: 10_000 },
550+
)
551+
.toBe('mocked')
534552
await expect(
535553
page.evaluate(async () => {
536554
const registrations = await navigator.serviceWorker.getRegistrations()

0 commit comments

Comments
 (0)