2022-05-03 15:10:13 +00:00
|
|
|
/* 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>(
|
2024-08-15 20:16:30 +00:00
|
|
|
'http://postman-echo.com/get',
|
2022-05-03 15:10:13 +00:00
|
|
|
additionalHeaders
|
|
|
|
)
|
2023-05-23 11:23:56 +00:00
|
|
|
expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('foo')
|
|
|
|
expect(jsonObj.headers[httpm.Headers.ContentType]).toContain(
|
2022-05-03 15:10:13 +00:00
|
|
|
httpm.MediaTypes.ApplicationJson
|
|
|
|
)
|
|
|
|
|
|
|
|
const httpWithHeaders = new httpm.HttpClient()
|
|
|
|
httpWithHeaders.requestOptions = {
|
|
|
|
headers: {
|
|
|
|
[httpm.Headers.Accept]: 'baz'
|
|
|
|
}
|
|
|
|
}
|
2024-08-15 20:16:30 +00:00
|
|
|
jsonObj = await httpWithHeaders.getJson<any>('http://postman-echo.com/get')
|
2023-05-23 11:23:56 +00:00
|
|
|
expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('baz')
|
|
|
|
expect(jsonObj.headers[httpm.Headers.ContentType]).toContain(
|
2022-05-03 15:10:13 +00:00
|
|
|
httpm.MediaTypes.ApplicationJson
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('preserves existing headers on postJson', async () => {
|
|
|
|
const additionalHeaders = {[httpm.Headers.Accept]: 'foo'}
|
|
|
|
let jsonObj = await _http.postJson<any>(
|
2024-08-15 20:23:33 +00:00
|
|
|
'http://postman-echo.com/post',
|
2022-05-03 15:10:13 +00:00
|
|
|
{},
|
|
|
|
additionalHeaders
|
|
|
|
)
|
2023-05-23 11:23:56 +00:00
|
|
|
expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('foo')
|
|
|
|
expect(jsonObj.headers[httpm.Headers.ContentType]).toContain(
|
2022-05-03 15:10:13 +00:00
|
|
|
httpm.MediaTypes.ApplicationJson
|
|
|
|
)
|
|
|
|
|
|
|
|
const httpWithHeaders = new httpm.HttpClient()
|
|
|
|
httpWithHeaders.requestOptions = {
|
|
|
|
headers: {
|
|
|
|
[httpm.Headers.Accept]: 'baz'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
jsonObj = await httpWithHeaders.postJson<any>(
|
2024-08-15 20:23:33 +00:00
|
|
|
'http://postman-echo.com/post',
|
2022-05-03 15:10:13 +00:00
|
|
|
{}
|
|
|
|
)
|
2023-05-23 11:23:56 +00:00
|
|
|
expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('baz')
|
|
|
|
expect(jsonObj.headers[httpm.Headers.ContentType]).toContain(
|
2022-05-03 15:10:13 +00:00
|
|
|
httpm.MediaTypes.ApplicationJson
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('preserves existing headers on putJson', async () => {
|
|
|
|
const additionalHeaders = {[httpm.Headers.Accept]: 'foo'}
|
|
|
|
let jsonObj = await _http.putJson<any>(
|
2024-08-15 20:23:33 +00:00
|
|
|
'http://postman-echo.com/put',
|
2022-05-03 15:10:13 +00:00
|
|
|
{},
|
|
|
|
additionalHeaders
|
|
|
|
)
|
2023-05-23 11:23:56 +00:00
|
|
|
expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('foo')
|
|
|
|
expect(jsonObj.headers[httpm.Headers.ContentType]).toContain(
|
2022-05-03 15:10:13 +00:00
|
|
|
httpm.MediaTypes.ApplicationJson
|
|
|
|
)
|
|
|
|
|
|
|
|
const httpWithHeaders = new httpm.HttpClient()
|
|
|
|
httpWithHeaders.requestOptions = {
|
|
|
|
headers: {
|
|
|
|
[httpm.Headers.Accept]: 'baz'
|
|
|
|
}
|
|
|
|
}
|
2023-05-23 11:23:56 +00:00
|
|
|
jsonObj = await httpWithHeaders.putJson<any>(
|
2024-08-15 20:23:33 +00:00
|
|
|
'http://postman-echo.com/put',
|
2023-05-23 11:23:56 +00:00
|
|
|
{}
|
|
|
|
)
|
|
|
|
expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('baz')
|
|
|
|
expect(jsonObj.headers[httpm.Headers.ContentType]).toContain(
|
2022-05-03 15:10:13 +00:00
|
|
|
httpm.MediaTypes.ApplicationJson
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('preserves existing headers on patchJson', async () => {
|
|
|
|
const additionalHeaders = {[httpm.Headers.Accept]: 'foo'}
|
|
|
|
let jsonObj = await _http.patchJson<any>(
|
2024-08-15 20:23:33 +00:00
|
|
|
'http://postman-echo.com/patch',
|
2022-05-03 15:10:13 +00:00
|
|
|
{},
|
|
|
|
additionalHeaders
|
|
|
|
)
|
2023-05-23 11:23:56 +00:00
|
|
|
expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('foo')
|
|
|
|
expect(jsonObj.headers[httpm.Headers.ContentType]).toContain(
|
2022-05-03 15:10:13 +00:00
|
|
|
httpm.MediaTypes.ApplicationJson
|
|
|
|
)
|
|
|
|
|
|
|
|
const httpWithHeaders = new httpm.HttpClient()
|
|
|
|
httpWithHeaders.requestOptions = {
|
|
|
|
headers: {
|
|
|
|
[httpm.Headers.Accept]: 'baz'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
jsonObj = await httpWithHeaders.patchJson<any>(
|
2024-08-15 20:23:33 +00:00
|
|
|
'http://postman-echo.com/patch',
|
2022-05-03 15:10:13 +00:00
|
|
|
{}
|
|
|
|
)
|
2023-05-23 11:23:56 +00:00
|
|
|
expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('baz')
|
|
|
|
expect(jsonObj.headers[httpm.Headers.ContentType]).toContain(
|
2022-05-03 15:10:13 +00:00
|
|
|
httpm.MediaTypes.ApplicationJson
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|