Skip to content

Commit a71f0e9

Browse files
committed
test(http-client): add tests for isResponseOk and createDeleteRequest
Improve http-client.ts coverage by adding comprehensive tests for isResponseOk and createDeleteRequest functions. - Test isResponseOk with 2xx, non-2xx, and undefined status codes - Test createDeleteRequest success and error paths - Branch coverage improved from 60.2% to 61.22% - Total tests: 456 (added 5 new tests)
1 parent 187feb2 commit a71f0e9

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

test/unit/http-client.test.mts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest'
1212
import { MAX_RESPONSE_SIZE } from '../../src/constants.js'
1313
import {
1414
ResponseError,
15+
createDeleteRequest,
1516
createGetRequest,
1617
createRequestWithJson,
1718
getErrorResponseBody,
1819
getHttpModule,
1920
getResponseJson,
21+
isResponseOk,
2022
} from '../../src/http-client.js'
23+
import {
24+
createRouteHandler,
25+
setupLocalHttpServer,
26+
} from '../utils/local-server-helpers.mts'
2127

2228
import type { IncomingMessage, Server } from 'node:http'
2329

@@ -458,3 +464,65 @@ describe('HTTP Client - ResponseError Edge Cases', () => {
458464
})
459465
})
460466
})
467+
468+
describe('HTTP Client - isResponseOk', () => {
469+
it('should return true for 2xx status codes', () => {
470+
const response200 = { statusCode: 200 } as IncomingMessage
471+
const response201 = { statusCode: 201 } as IncomingMessage
472+
const response204 = { statusCode: 204 } as IncomingMessage
473+
const response299 = { statusCode: 299 } as IncomingMessage
474+
475+
expect(isResponseOk(response200)).toBe(true)
476+
expect(isResponseOk(response201)).toBe(true)
477+
expect(isResponseOk(response204)).toBe(true)
478+
expect(isResponseOk(response299)).toBe(true)
479+
})
480+
481+
it('should return false for non-2xx status codes', () => {
482+
const response199 = { statusCode: 199 } as IncomingMessage
483+
const response300 = { statusCode: 300 } as IncomingMessage
484+
const response400 = { statusCode: 400 } as IncomingMessage
485+
const response404 = { statusCode: 404 } as IncomingMessage
486+
const response500 = { statusCode: 500 } as IncomingMessage
487+
488+
expect(isResponseOk(response199)).toBe(false)
489+
expect(isResponseOk(response300)).toBe(false)
490+
expect(isResponseOk(response400)).toBe(false)
491+
expect(isResponseOk(response404)).toBe(false)
492+
expect(isResponseOk(response500)).toBe(false)
493+
})
494+
495+
it('should return false for undefined statusCode', () => {
496+
const responseNoStatus = {} as IncomingMessage
497+
expect(isResponseOk(responseNoStatus)).toBe(false)
498+
})
499+
})
500+
501+
describe('HTTP Client - createDeleteRequest', () => {
502+
const getBaseUrl = setupLocalHttpServer(
503+
createRouteHandler({
504+
'/test-delete': (_req: IncomingMessage, res) => {
505+
res.writeHead(204, { 'Content-Type': 'application/json' })
506+
res.end()
507+
},
508+
}),
509+
)
510+
511+
it('should make successful DELETE request', async () => {
512+
const response = await createDeleteRequest(
513+
getBaseUrl(),
514+
'/test-delete',
515+
{},
516+
)
517+
518+
expect(response.statusCode).toBe(204)
519+
})
520+
521+
it('should handle DELETE request errors', async () => {
522+
await expect(
523+
createDeleteRequest('http://localhost:1', '/test-delete', {
524+
timeout: 100,
525+
}),
526+
).rejects.toThrow()
527+
})
528+
})

0 commit comments

Comments
 (0)