Default to sending Accept and Content-Type headers for json
This commit is contained in:
@@ -9,6 +9,7 @@ interface HttpBinData {
|
||||
url: string;
|
||||
data: any;
|
||||
json: any;
|
||||
headers: any;
|
||||
args?: any
|
||||
}
|
||||
|
||||
@@ -206,6 +207,8 @@ describe('basics', () => {
|
||||
expect(jsonObj.statusCode).toBe(200);
|
||||
expect(jsonObj.result).toBeDefined();
|
||||
expect(jsonObj.result.url).toBe('https://httpbin.org/get');
|
||||
expect(jsonObj.result.headers["Accept"]).toBe(httpm.MediaTypes.ApplicationJson);
|
||||
expect(jsonObj.headers[httpm.Headers.ContentType]).toBe(httpm.MediaTypes.ApplicationJson);
|
||||
});
|
||||
|
||||
it('getting a non existent json object returns null', async() => {
|
||||
@@ -221,6 +224,9 @@ describe('basics', () => {
|
||||
expect(restRes.result).toBeDefined();
|
||||
expect(restRes.result.url).toBe('https://httpbin.org/post');
|
||||
expect(restRes.result.json.name).toBe('foo');
|
||||
expect(restRes.result.headers["Accept"]).toBe(httpm.MediaTypes.ApplicationJson);
|
||||
expect(restRes.result.headers["Content-Type"]).toBe(httpm.MediaTypes.ApplicationJson);
|
||||
expect(restRes.headers[httpm.Headers.ContentType]).toBe(httpm.MediaTypes.ApplicationJson);
|
||||
});
|
||||
|
||||
it('puts a json object', async() => {
|
||||
@@ -230,6 +236,10 @@ describe('basics', () => {
|
||||
expect(restRes.result).toBeDefined();
|
||||
expect(restRes.result.url).toBe('https://httpbin.org/put');
|
||||
expect(restRes.result.json.name).toBe('foo');
|
||||
|
||||
expect(restRes.result.headers["Accept"]).toBe(httpm.MediaTypes.ApplicationJson);
|
||||
expect(restRes.result.headers["Content-Type"]).toBe(httpm.MediaTypes.ApplicationJson);
|
||||
expect(restRes.headers[httpm.Headers.ContentType]).toBe(httpm.MediaTypes.ApplicationJson);
|
||||
});
|
||||
|
||||
it('patch a json object', async() => {
|
||||
@@ -239,5 +249,8 @@ describe('basics', () => {
|
||||
expect(restRes.result).toBeDefined();
|
||||
expect(restRes.result.url).toBe('https://httpbin.org/patch');
|
||||
expect(restRes.result.json.name).toBe('foo');
|
||||
});
|
||||
})
|
||||
expect(restRes.result.headers["Accept"]).toBe(httpm.MediaTypes.ApplicationJson);
|
||||
expect(restRes.result.headers["Content-Type"]).toBe(httpm.MediaTypes.ApplicationJson);
|
||||
expect(restRes.headers[httpm.Headers.ContentType]).toBe(httpm.MediaTypes.ApplicationJson);
|
||||
});
|
||||
});
|
||||
|
||||
79
__tests__/headers.test.ts
Normal file
79
__tests__/headers.test.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import * as httpm from '../_out';
|
||||
import * as ifm from '../_out/interfaces'
|
||||
|
||||
describe('headers', () => {
|
||||
let _http: httpm.HttpClient;
|
||||
|
||||
beforeEach(() => {
|
||||
_http = new httpm.HttpClient('http-client-tests');
|
||||
});
|
||||
|
||||
it('preserves existing headers on getJson', async() => {
|
||||
let additionalHeaders = { [httpm.Headers.Accept]: "foo" };
|
||||
let jsonObj: ifm.ITypedResponse<any> = await _http.getJson<any>('https://httpbin.org/get', additionalHeaders);
|
||||
expect(jsonObj.result.headers["Accept"]).toBe("foo");
|
||||
expect(jsonObj.headers[httpm.Headers.ContentType]).toBe(httpm.MediaTypes.ApplicationJson);
|
||||
|
||||
let httpWithHeaders = new httpm.HttpClient();
|
||||
httpWithHeaders.requestOptions = {
|
||||
headers: {
|
||||
[httpm.Headers.Accept]: "baz"
|
||||
}
|
||||
};
|
||||
jsonObj = await httpWithHeaders.getJson<any>('https://httpbin.org/get');
|
||||
expect(jsonObj.result.headers["Accept"]).toBe("baz");
|
||||
expect(jsonObj.headers[httpm.Headers.ContentType]).toBe(httpm.MediaTypes.ApplicationJson);
|
||||
});
|
||||
|
||||
it('preserves existing headers on postJson', async() => {
|
||||
let additionalHeaders = { [httpm.Headers.Accept]: "foo" };
|
||||
let jsonObj: ifm.ITypedResponse<any> = await _http.postJson<any>('https://httpbin.org/post', {}, additionalHeaders);
|
||||
expect(jsonObj.result.headers["Accept"]).toBe("foo");
|
||||
expect(jsonObj.headers[httpm.Headers.ContentType]).toBe(httpm.MediaTypes.ApplicationJson);
|
||||
|
||||
let httpWithHeaders = new httpm.HttpClient();
|
||||
httpWithHeaders.requestOptions = {
|
||||
headers: {
|
||||
[httpm.Headers.Accept]: "baz"
|
||||
}
|
||||
};
|
||||
jsonObj = await httpWithHeaders.postJson<any>('https://httpbin.org/post', {});
|
||||
expect(jsonObj.result.headers["Accept"]).toBe("baz");
|
||||
expect(jsonObj.headers[httpm.Headers.ContentType]).toBe(httpm.MediaTypes.ApplicationJson);
|
||||
});
|
||||
|
||||
it('preserves existing headers on putJson', async() => {
|
||||
let additionalHeaders = { [httpm.Headers.Accept]: "foo" };
|
||||
let jsonObj: ifm.ITypedResponse<any> = await _http.putJson<any>('https://httpbin.org/put', {}, additionalHeaders);
|
||||
expect(jsonObj.result.headers["Accept"]).toBe("foo");
|
||||
expect(jsonObj.headers[httpm.Headers.ContentType]).toBe(httpm.MediaTypes.ApplicationJson);
|
||||
|
||||
let httpWithHeaders = new httpm.HttpClient();
|
||||
httpWithHeaders.requestOptions = {
|
||||
headers: {
|
||||
[httpm.Headers.Accept]: "baz"
|
||||
}
|
||||
};
|
||||
jsonObj = await httpWithHeaders.putJson<any>('https://httpbin.org/put', {});
|
||||
expect(jsonObj.result.headers["Accept"]).toBe("baz");
|
||||
expect(jsonObj.headers[httpm.Headers.ContentType]).toBe(httpm.MediaTypes.ApplicationJson);
|
||||
});
|
||||
|
||||
it('preserves existing headers on patchJson', async() => {
|
||||
let additionalHeaders = { [httpm.Headers.Accept]: "foo" };
|
||||
let jsonObj: ifm.ITypedResponse<any> = await _http.patchJson<any>('https://httpbin.org/patch', {}, additionalHeaders);
|
||||
expect(jsonObj.result.headers["Accept"]).toBe("foo");
|
||||
expect(jsonObj.headers[httpm.Headers.ContentType]).toBe(httpm.MediaTypes.ApplicationJson);
|
||||
|
||||
let httpWithHeaders = new httpm.HttpClient();
|
||||
httpWithHeaders.requestOptions = {
|
||||
headers: {
|
||||
[httpm.Headers.Accept]: "baz"
|
||||
}
|
||||
};
|
||||
jsonObj = await httpWithHeaders.patchJson<any>('https://httpbin.org/patch', {});
|
||||
expect(jsonObj.result.headers["Accept"]).toBe("baz");
|
||||
expect(jsonObj.headers[httpm.Headers.ContentType]).toBe(httpm.MediaTypes.ApplicationJson);
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user