Skip to content

Commit cb22e3e

Browse files
authored
Merge pull request #228 from PJGitLan/fixContentType
fix: set correct Content-Type
2 parents f82d4fb + 8787b81 commit cb22e3e

File tree

4 files changed

+64
-12
lines changed

4 files changed

+64
-12
lines changed

javascript/lib/api/src/client/constants/content-type.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
*/
1313

1414
export enum ContentType {
15-
MERGE_PATCH_JSON = 'application/merge-patch+json',
16-
JSON = 'application/json',
17-
TEXT = 'text/plain'
15+
MERGE_PATCH_JSON = 'application/merge-patch+json',
16+
JSON = 'application/json',
17+
FORM_URLENCODED = 'application/x-www-form-urlencoded',
18+
TEXT = 'text/plain'
1819
}

javascript/lib/api/src/client/handles/search.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
*/
1313

1414
import { SearchThingsResponse } from '../../model/response';
15-
import { CountOptions, SearchOptions } from '../../options/request.options';
15+
import { CountOptions, DefaultSearchOptions, RequestOptions, SearchOptions } from '../../options/request.options';
16+
import { ContentType } from '../constants/content-type';
17+
import { Header } from '../constants/header';
1618
import { HttpVerb } from '../constants/http-verb';
1719
import { RequestSender, RequestSenderFactory } from '../request-factory/request-sender';
1820

@@ -54,9 +56,7 @@ export interface SearchHandle {
5456
* Handle to send Search requests.
5557
*/
5658
export class DefaultSearchHandle implements SearchHandle {
57-
58-
private constructor(readonly requestFactory: RequestSender) {
59-
}
59+
private constructor(readonly requestFactory: RequestSender) {}
6060

6161
/**
6262
* returns an instance of SearchHandle using the provided RequestSender.
@@ -92,7 +92,8 @@ export class DefaultSearchHandle implements SearchHandle {
9292
return this.requestFactory.fetchFormRequest({
9393
verb: HttpVerb.POST,
9494
parser: SearchThingsResponse.fromObject,
95-
payload: options?.getOptions()
95+
payload: options?.getOptions(),
96+
requestOptions: this.getFormRequestOptions(options)
9697
});
9798
}
9899

@@ -122,7 +123,19 @@ export class DefaultSearchHandle implements SearchHandle {
122123
verb: HttpVerb.POST,
123124
parser: Number,
124125
path: 'count',
125-
payload: options?.getOptions()
126+
payload: options?.getOptions(),
127+
requestOptions: this.getFormRequestOptions(options)
128+
});
129+
}
130+
131+
private getFormRequestOptions(options?: SearchOptions | CountOptions): RequestOptions | undefined {
132+
const requestOptions = DefaultSearchOptions.getInstance();
133+
options?.getHeaders().forEach((value, key) => {
134+
requestOptions.addHeader(key, value);
126135
});
136+
if (!requestOptions?.getHeaders().has(Header.CONTENT_TYPE)) {
137+
requestOptions?.addHeader(Header.CONTENT_TYPE, ContentType.FORM_URLENCODED);
138+
}
139+
return requestOptions;
127140
}
128141
}

javascript/lib/api/tests/client/http/http.helper.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,13 @@ export class HttpHelper extends Helper {
4949
headers: options.responseHeaders,
5050
body: options.testBody
5151
};
52-
this.requester.addResponse(options.method,
53-
`${HttpHelper.matcher}${api}/${options.request}`, options.requestHeaders, options.payload, requestResponse);
52+
this.requester.addResponse(
53+
options.method,
54+
`${HttpHelper.matcher}${api}/${options.request}`,
55+
options.requestHeaders,
56+
options.payload,
57+
requestResponse
58+
);
5459
if (options.expected !== undefined) {
5560
return options.toTest()
5661
.then(response => {

javascript/lib/api/tests/client/http/search.http.spec.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* SPDX-License-Identifier: EPL-2.0
1212
*/
1313

14+
import { ContentType } from '../../../src/client/constants/content-type';
15+
import { Header } from '../../../src/client/constants/header';
1416
import { HttpVerb } from '../../../src/client/constants/http-verb';
1517
import { SearchHandle } from '../../../src/client/handles/search';
1618
import { SearchThingsResponse } from '../../../src/model/response';
@@ -39,10 +41,13 @@ describe('Http Search Handle', () => {
3941
toTest: () => handle.postSearch(searchOptions),
4042
testBody: response.toObject(),
4143
expected: response,
42-
payload:expectedPayload,
44+
payload: expectedPayload,
4345
request: baseRequest,
4446
method: HttpVerb.POST,
4547
status: 200,
48+
requestHeaders: new Map<string, string>([
49+
[Header.CONTENT_TYPE, ContentType.FORM_URLENCODED],
50+
]),
4651
});
4752
});
4853

@@ -55,6 +60,9 @@ describe('Http Search Handle', () => {
5560
request: baseRequest,
5661
method: HttpVerb.POST,
5762
status: 200,
63+
requestHeaders: new Map<string, string>([
64+
[Header.CONTENT_TYPE, ContentType.FORM_URLENCODED],
65+
]),
5866
});
5967
});
6068

@@ -90,6 +98,9 @@ describe('Http Search Handle', () => {
9098
method: HttpVerb.POST,
9199
payload: expectedPayload,
92100
status: 200,
101+
requestHeaders: new Map<string, string>([
102+
[Header.CONTENT_TYPE, ContentType.FORM_URLENCODED],
103+
]),
93104
});
94105
});
95106

@@ -101,6 +112,28 @@ describe('Http Search Handle', () => {
101112
request: `${baseRequest}/count`,
102113
method: HttpVerb.POST,
103114
status: 200,
115+
requestHeaders: new Map<string, string>([
116+
[Header.CONTENT_TYPE, ContentType.FORM_URLENCODED],
117+
]),
118+
});
119+
});
120+
121+
it("counts Things by post with text/plain Content-Type", () => {
122+
const contentTypeHeader = DefaultSearchOptions.getInstance().addHeader(
123+
Header.CONTENT_TYPE,
124+
ContentType.TEXT
125+
);
126+
return H.test({
127+
toTest: () => handle.postCount(contentTypeHeader),
128+
testBody: 4,
129+
expected: 4,
130+
request: `${baseRequest}/count`,
131+
method: HttpVerb.POST,
132+
status: 200,
133+
payload: '',
134+
requestHeaders: new Map<string, string>([
135+
[Header.CONTENT_TYPE, ContentType.TEXT],
136+
]),
104137
});
105138
});
106139

0 commit comments

Comments
 (0)