Skip to content

Commit

Permalink
fix: set correct content type
Browse files Browse the repository at this point in the history
  • Loading branch information
PJGitLan committed Mar 4, 2024
1 parent f82d4fb commit 8787b81
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
7 changes: 4 additions & 3 deletions javascript/lib/api/src/client/constants/content-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
*/

export enum ContentType {
MERGE_PATCH_JSON = 'application/merge-patch+json',
JSON = 'application/json',
TEXT = 'text/plain'
MERGE_PATCH_JSON = 'application/merge-patch+json',
JSON = 'application/json',
FORM_URLENCODED = 'application/x-www-form-urlencoded',
TEXT = 'text/plain'
}
25 changes: 19 additions & 6 deletions javascript/lib/api/src/client/handles/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
*/

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

Expand Down Expand Up @@ -54,9 +56,7 @@ export interface SearchHandle {
* Handle to send Search requests.
*/
export class DefaultSearchHandle implements SearchHandle {

private constructor(readonly requestFactory: RequestSender) {
}
private constructor(readonly requestFactory: RequestSender) {}

/**
* returns an instance of SearchHandle using the provided RequestSender.
Expand Down Expand Up @@ -92,7 +92,8 @@ export class DefaultSearchHandle implements SearchHandle {
return this.requestFactory.fetchFormRequest({
verb: HttpVerb.POST,
parser: SearchThingsResponse.fromObject,
payload: options?.getOptions()
payload: options?.getOptions(),
requestOptions: this.getFormRequestOptions(options)
});
}

Expand Down Expand Up @@ -122,7 +123,19 @@ export class DefaultSearchHandle implements SearchHandle {
verb: HttpVerb.POST,
parser: Number,
path: 'count',
payload: options?.getOptions()
payload: options?.getOptions(),
requestOptions: this.getFormRequestOptions(options)
});
}

private getFormRequestOptions(options?: SearchOptions | CountOptions): RequestOptions | undefined {
const requestOptions = DefaultSearchOptions.getInstance();
options?.getHeaders().forEach((value, key) => {
requestOptions.addHeader(key, value);
});
if (!requestOptions?.getHeaders().has(Header.CONTENT_TYPE)) {
requestOptions?.addHeader(Header.CONTENT_TYPE, ContentType.FORM_URLENCODED);
}
return requestOptions;
}
}
9 changes: 7 additions & 2 deletions javascript/lib/api/tests/client/http/http.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,13 @@ export class HttpHelper extends Helper {
headers: options.responseHeaders,
body: options.testBody
};
this.requester.addResponse(options.method,
`${HttpHelper.matcher}${api}/${options.request}`, options.requestHeaders, options.payload, requestResponse);
this.requester.addResponse(
options.method,
`${HttpHelper.matcher}${api}/${options.request}`,
options.requestHeaders,
options.payload,
requestResponse
);
if (options.expected !== undefined) {
return options.toTest()
.then(response => {
Expand Down
35 changes: 34 additions & 1 deletion javascript/lib/api/tests/client/http/search.http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* SPDX-License-Identifier: EPL-2.0
*/

import { ContentType } from '../../../src/client/constants/content-type';
import { Header } from '../../../src/client/constants/header';
import { HttpVerb } from '../../../src/client/constants/http-verb';
import { SearchHandle } from '../../../src/client/handles/search';
import { SearchThingsResponse } from '../../../src/model/response';
Expand Down Expand Up @@ -39,10 +41,13 @@ describe('Http Search Handle', () => {
toTest: () => handle.postSearch(searchOptions),
testBody: response.toObject(),
expected: response,
payload:expectedPayload,
payload: expectedPayload,
request: baseRequest,
method: HttpVerb.POST,
status: 200,
requestHeaders: new Map<string, string>([
[Header.CONTENT_TYPE, ContentType.FORM_URLENCODED],
]),
});
});

Expand All @@ -55,6 +60,9 @@ describe('Http Search Handle', () => {
request: baseRequest,
method: HttpVerb.POST,
status: 200,
requestHeaders: new Map<string, string>([
[Header.CONTENT_TYPE, ContentType.FORM_URLENCODED],
]),
});
});

Expand Down Expand Up @@ -90,6 +98,9 @@ describe('Http Search Handle', () => {
method: HttpVerb.POST,
payload: expectedPayload,
status: 200,
requestHeaders: new Map<string, string>([
[Header.CONTENT_TYPE, ContentType.FORM_URLENCODED],
]),
});
});

Expand All @@ -101,6 +112,28 @@ describe('Http Search Handle', () => {
request: `${baseRequest}/count`,
method: HttpVerb.POST,
status: 200,
requestHeaders: new Map<string, string>([
[Header.CONTENT_TYPE, ContentType.FORM_URLENCODED],
]),
});
});

it("counts Things by post with text/plain Content-Type", () => {
const contentTypeHeader = DefaultSearchOptions.getInstance().addHeader(
Header.CONTENT_TYPE,
ContentType.TEXT
);
return H.test({
toTest: () => handle.postCount(contentTypeHeader),
testBody: 4,
expected: 4,
request: `${baseRequest}/count`,
method: HttpVerb.POST,
status: 200,
payload: '',
requestHeaders: new Map<string, string>([
[Header.CONTENT_TYPE, ContentType.TEXT],
]),
});
});

Expand Down

0 comments on commit 8787b81

Please sign in to comment.