-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ClientRequest): support "http.request()" and "http.get()" without…
… any arguments (#468)
- Loading branch information
1 parent
198533a
commit 65b7ac1
Showing
2 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
test/modules/http/compliance/http-request-without-options.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { vi, it, expect, beforeAll, afterAll } from 'vitest' | ||
import http from 'http' | ||
import { ClientRequestInterceptor } from '../../../../src/interceptors/ClientRequest' | ||
import { waitForClientRequest } from '../../../helpers' | ||
|
||
const interceptor = new ClientRequestInterceptor() | ||
|
||
beforeAll(() => { | ||
interceptor.apply() | ||
}) | ||
|
||
afterAll(() => { | ||
interceptor.dispose() | ||
}) | ||
|
||
it('supports "http.request()" without any options', async () => { | ||
const responseListener = vi.fn() | ||
const errorListener = vi.fn() | ||
|
||
const request = http | ||
// @ts-ignore It's possible to make a request without any options. | ||
// This will result in a "GET http://localhost" request in Node.js. | ||
.request() | ||
request.end() | ||
request.on('response', responseListener) | ||
request.on('error', errorListener) | ||
|
||
expect(errorListener).not.toHaveBeenCalled() | ||
expect(responseListener).not.toHaveBeenCalled() | ||
expect(request.path).toBe('/') | ||
expect(request.method).toBe('GET') | ||
expect(request.protocol).toBe('http:') | ||
expect(request.host).toBe('localhost') | ||
}) | ||
|
||
it('responds with a mocked response for "http.request()" without any options', async () => { | ||
interceptor.once('request', ({ request }) => { | ||
if (request.url === 'http://localhost/') { | ||
request.respondWith(new Response('Mocked')) | ||
} | ||
}) | ||
|
||
const request = http | ||
// @ts-ignore It's possible to make a request without any options. | ||
// This will result in a "GET http://localhost" request in Node.js. | ||
.request() | ||
request.end() | ||
|
||
const errorListener = vi.fn() | ||
request.on('error', errorListener) | ||
|
||
const { res, text } = await waitForClientRequest(request) | ||
|
||
expect(errorListener).not.toHaveBeenCalled() | ||
|
||
expect(res.statusCode).toBe(200) | ||
expect(await text()).toBe('Mocked') | ||
}) | ||
|
||
it('supports "http.get()" without any argumenst', async () => { | ||
const responseListener = vi.fn() | ||
const errorListener = vi.fn() | ||
|
||
const request = http | ||
// @ts-ignore It's possible to make a request without any options. | ||
// This will result in a "GET http://localhost" request in Node.js. | ||
.get() | ||
request.end() | ||
request.on('response', responseListener) | ||
request.on('error', errorListener) | ||
|
||
expect(errorListener).not.toHaveBeenCalled() | ||
expect(responseListener).not.toHaveBeenCalled() | ||
expect(request.path).toBe('/') | ||
expect(request.method).toBe('GET') | ||
expect(request.protocol).toBe('http:') | ||
expect(request.host).toBe('localhost') | ||
}) | ||
|
||
it('responds with a mocked response for "http.get()" without any options', async () => { | ||
interceptor.once('request', ({ request }) => { | ||
if (request.url === 'http://localhost/') { | ||
request.respondWith(new Response('Mocked')) | ||
} | ||
}) | ||
|
||
const request = http | ||
// @ts-ignore It's possible to make a request without any options. | ||
// This will result in a "GET http://localhost" request in Node.js. | ||
.get() | ||
request.end() | ||
|
||
const errorListener = vi.fn() | ||
request.on('error', errorListener) | ||
|
||
const { res, text } = await waitForClientRequest(request) | ||
|
||
expect(errorListener).not.toHaveBeenCalled() | ||
|
||
expect(res.statusCode).toBe(200) | ||
expect(await text()).toBe('Mocked') | ||
}) |