Split out of actions/toolkit (packages/http-client, history kept by filter-repo). Brings the 2024 [LevK] packaging edits forward onto current upstream: - tsconfig.json: the monorepo root's compiler options inlined, no extends - jest.config.cjs: the root's ts-jest transform, rooted at tests/ (isolatedModules: ESM lib/ and sources run through CommonJS) - __tests__/ -> tests/ - package.json: test = jest, build = tsc; jest/ts-jest/typescript as devDependencies; repository, homepage, bugs on git.levk.dev - .gitignore: node_modules/, lib/, audit.json - HttpCodes.PayloadTooLarge = 413 (the 2024 edit; still missing upstream) Not carried over from 2024: the _getExistingOrDefaultHeader type narrowing (upstream has since fixed it properly), version reset to 1.0.0 (dependents' ranges need upstream's numbering) and the Unlicense field (the code is MIT). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UHXzGrsxfkGk8LmwrBbHNZ
120 lines
3.6 KiB
TypeScript
120 lines
3.6 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
import * as httpm from '..'
|
|
|
|
describe('headers', () => {
|
|
let _http: httpm.HttpClient
|
|
|
|
beforeEach(() => {
|
|
_http = new httpm.HttpClient('http-client-tests')
|
|
})
|
|
|
|
it('preserves existing headers on getJson', async () => {
|
|
const additionalHeaders = {[httpm.Headers.Accept]: 'foo'}
|
|
let jsonObj = await _http.getJson<any>(
|
|
'https://postman-echo.com/get',
|
|
additionalHeaders
|
|
)
|
|
expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('foo')
|
|
expect(jsonObj.headers[httpm.Headers.ContentType]).toContain(
|
|
httpm.MediaTypes.ApplicationJson
|
|
)
|
|
|
|
const httpWithHeaders = new httpm.HttpClient()
|
|
httpWithHeaders.requestOptions = {
|
|
headers: {
|
|
[httpm.Headers.Accept]: 'baz'
|
|
}
|
|
}
|
|
jsonObj = await httpWithHeaders.getJson<any>('https://postman-echo.com/get')
|
|
expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('baz')
|
|
expect(jsonObj.headers[httpm.Headers.ContentType]).toContain(
|
|
httpm.MediaTypes.ApplicationJson
|
|
)
|
|
})
|
|
|
|
it('preserves existing headers on postJson', async () => {
|
|
const additionalHeaders = {[httpm.Headers.Accept]: 'foo'}
|
|
let jsonObj = await _http.postJson<any>(
|
|
'https://postman-echo.com/post',
|
|
{},
|
|
additionalHeaders
|
|
)
|
|
expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('foo')
|
|
expect(jsonObj.headers[httpm.Headers.ContentType]).toContain(
|
|
httpm.MediaTypes.ApplicationJson
|
|
)
|
|
|
|
const httpWithHeaders = new httpm.HttpClient()
|
|
httpWithHeaders.requestOptions = {
|
|
headers: {
|
|
[httpm.Headers.Accept]: 'baz'
|
|
}
|
|
}
|
|
jsonObj = await httpWithHeaders.postJson<any>(
|
|
'https://postman-echo.com/post',
|
|
{}
|
|
)
|
|
expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('baz')
|
|
expect(jsonObj.headers[httpm.Headers.ContentType]).toContain(
|
|
httpm.MediaTypes.ApplicationJson
|
|
)
|
|
})
|
|
|
|
it('preserves existing headers on putJson', async () => {
|
|
const additionalHeaders = {[httpm.Headers.Accept]: 'foo'}
|
|
let jsonObj = await _http.putJson<any>(
|
|
'https://postman-echo.com/put',
|
|
{},
|
|
additionalHeaders
|
|
)
|
|
expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('foo')
|
|
expect(jsonObj.headers[httpm.Headers.ContentType]).toContain(
|
|
httpm.MediaTypes.ApplicationJson
|
|
)
|
|
|
|
const httpWithHeaders = new httpm.HttpClient()
|
|
httpWithHeaders.requestOptions = {
|
|
headers: {
|
|
[httpm.Headers.Accept]: 'baz'
|
|
}
|
|
}
|
|
jsonObj = await httpWithHeaders.putJson<any>(
|
|
'https://postman-echo.com/put',
|
|
{}
|
|
)
|
|
expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('baz')
|
|
expect(jsonObj.headers[httpm.Headers.ContentType]).toContain(
|
|
httpm.MediaTypes.ApplicationJson
|
|
)
|
|
})
|
|
|
|
it('preserves existing headers on patchJson', async () => {
|
|
const additionalHeaders = {[httpm.Headers.Accept]: 'foo'}
|
|
let jsonObj = await _http.patchJson<any>(
|
|
'https://postman-echo.com/patch',
|
|
{},
|
|
additionalHeaders
|
|
)
|
|
expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('foo')
|
|
expect(jsonObj.headers[httpm.Headers.ContentType]).toContain(
|
|
httpm.MediaTypes.ApplicationJson
|
|
)
|
|
|
|
const httpWithHeaders = new httpm.HttpClient()
|
|
httpWithHeaders.requestOptions = {
|
|
headers: {
|
|
[httpm.Headers.Accept]: 'baz'
|
|
}
|
|
}
|
|
jsonObj = await httpWithHeaders.patchJson<any>(
|
|
'https://postman-echo.com/patch',
|
|
{}
|
|
)
|
|
expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('baz')
|
|
expect(jsonObj.headers[httpm.Headers.ContentType]).toContain(
|
|
httpm.MediaTypes.ApplicationJson
|
|
)
|
|
})
|
|
})
|