Skip to content

Commit

Permalink
test: migrate other http tests to socket interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Mar 5, 2024
1 parent 1c74f23 commit a755028
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
12 changes: 11 additions & 1 deletion src/interceptors/Socket/SocketInterceptor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import net from 'node:net'
import { STATUS_CODES } from 'node:http'
import { HTTPParser } from 'node:_http_common'
import { randomUUID } from 'node:crypto'
import { Readable } from 'node:stream'
Expand All @@ -9,7 +10,7 @@ import {
toInteractiveRequest,
} from '../../utils/toInteractiveRequest'
import { emitAsync } from '../../utils/emitAsync'
import { STATUS_CODES } from 'node:http'
import { isPropertyAccessible } from '../../utils/isPropertyAccessible'

type NormalizedSocketConnectArgs = [
options: NormalizedSocketConnectOptions,
Expand Down Expand Up @@ -108,6 +109,15 @@ export class SocketInterceptor extends Interceptor<SocketEventMap> {
const mockedResponse = resolverResult.data

if (mockedResponse) {
// Handle mocked "Response.error()" instances.
if (
isPropertyAccessible(mockedResponse, 'type') &&
mockedResponse.type === 'error'
) {
socketWrap.errorWith(new TypeError('Network error'))
return
}

socketWrap.respondWith(mockedResponse)
} else {
socketWrap.passthrough()
Expand Down
5 changes: 3 additions & 2 deletions test/modules/http/http-performance.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { it, expect, beforeAll, afterAll } from 'vitest'
import { HttpServer } from '@open-draft/test-server/http'
import { ClientRequestInterceptor } from '../../../src/interceptors/ClientRequest'
import { SocketInterceptor } from '../../../src/interceptors/Socket/SocketInterceptor'
import { httpGet, PromisifiedResponse, useCors } from '../../helpers'

function arrayWith<V>(length: number, mapFn: (index: number) => V): V[] {
Expand Down Expand Up @@ -28,7 +28,8 @@ const httpServer = new HttpServer((app) => {
})
})

const interceptor = new ClientRequestInterceptor()
const interceptor = new SocketInterceptor()

interceptor.on('request', ({ request }) => {
const url = new URL(request.url)

Expand Down
6 changes: 3 additions & 3 deletions test/modules/http/response/http-response-delay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { it, expect, beforeAll, afterAll } from 'vitest'
import http from 'http'
import { HttpServer } from '@open-draft/test-server/http'
import { sleep, waitForClientRequest } from '../../../helpers'
import { ClientRequestInterceptor } from '../../../../src/interceptors/ClientRequest'
import { SocketInterceptor } from '../../../../src/interceptors/Socket/SocketInterceptor'

const interceptor = new ClientRequestInterceptor()
const interceptor = new SocketInterceptor()

const httpServer = new HttpServer((app) => {
app.get('/resource', (req, res) => {
Expand All @@ -29,7 +29,7 @@ it('supports custom delay before responding with a mock', async () => {
})

const requestStart = Date.now()
const request = http.get('https://non-existing-host.com')
const request = http.get('http://non-existing-host.com')
const { res, text } = await waitForClientRequest(request)
const requestEnd = Date.now()

Expand Down
4 changes: 2 additions & 2 deletions test/modules/http/response/http-response-error.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { it, expect, beforeAll, afterAll } from 'vitest'
import http from 'http'
import { ClientRequestInterceptor } from '../../../../src/interceptors/ClientRequest'
import { SocketInterceptor } from '../../../../src/interceptors/Socket/SocketInterceptor'
import { DeferredPromise } from '@open-draft/deferred-promise'

const interceptor = new ClientRequestInterceptor()
const interceptor = new SocketInterceptor()

interceptor.on('request', ({ request }) => {
request.respondWith(Response.error())
Expand Down
8 changes: 2 additions & 6 deletions test/modules/http/response/http-response-patching.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { it, expect, beforeAll, afterAll } from 'vitest'
import http from 'http'
import { HttpServer } from '@open-draft/test-server/http'
import { BatchInterceptor } from '../../../../src'
import { ClientRequestInterceptor } from '../../../../src/interceptors/ClientRequest'
import { SocketInterceptor } from '../../../../src/interceptors/Socket/SocketInterceptor'
import { sleep, waitForClientRequest } from '../../../helpers'

const server = new HttpServer((app) => {
Expand All @@ -11,10 +10,7 @@ const server = new HttpServer((app) => {
})
})

const interceptor = new BatchInterceptor({
name: 'response-patching',
interceptors: [new ClientRequestInterceptor()],
})
const interceptor = new SocketInterceptor()

async function getResponse(request: Request): Promise<Response | undefined> {
const url = new URL(request.url)
Expand Down

0 comments on commit a755028

Please sign in to comment.