Skip to content

Commit a059576

Browse files
committed
Makes contenttype resolvement more error relaxed.
1 parent 4693876 commit a059576

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/http/client/rest-template.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class RestTemplate implements RestOperations {
102102

103103
private async handleData<D>(response: Response): Promise<[D, Response]> {
104104
const contentTypeHeader = response.headers.get('content-type');
105-
const contentType = contentTypeHeader?.split(';')[0] ?? undefined;
105+
const contentType = this.resolvePrimaryContentType(contentTypeHeader);
106106

107107
if(!contentType) {
108108
return Promise.resolve([null, response]);
@@ -128,4 +128,13 @@ export class RestTemplate implements RestOperations {
128128
}
129129
}
130130

131+
private resolvePrimaryContentType(value: string) {
132+
let result: string | undefined = undefined;
133+
const firstPart = value?.split(';')[0] ?? undefined;
134+
if(firstPart) {
135+
result = firstPart.split(',')[0];
136+
}
137+
return result;
138+
}
139+
131140
}

test/http/client/rest-template.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,28 @@ describe("passport control", () => {
4141
expect(result).toEqual('error');
4242
});
4343

44+
it("should contenttype header issues correctly", async () => {
45+
// Arrange
46+
template.errorHandler = {
47+
hasError: () => false,
48+
handleError: () => {throw Error('')}
49+
}
50+
fetchMock.mockIf('http://localhost/test', req => {
51+
return Promise.resolve({
52+
status: 200,
53+
body: JSON.stringify({id: '123'}),
54+
headers: {
55+
'content-type': 'application/json; application/xml',
56+
'Content-Type': 'application/json'
57+
}
58+
});
59+
});
60+
61+
// Act
62+
const result = await template.getForObject('http://localhost/test') as any;
63+
64+
// Assert
65+
expect(result.id).toEqual('123');
66+
});
67+
4468
});

0 commit comments

Comments
 (0)