Skip to content
This repository was archived by the owner on Oct 30, 2022. It is now read-only.

Commit 9cf87ad

Browse files
author
Kimmo Sääskilahti
authored
Fix bug in handling empty query string. (#1)
* Fix bug in query string handling. * Simplify test. * Simplify more. * Ignore .vscode.
1 parent b743899 commit 9cf87ad

4 files changed

Lines changed: 36 additions & 19 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ dist
33
*.tgz
44
.DS_Store
55
.*.swp
6+
.vscode/

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -193,27 +193,29 @@ function validateRequest(request: HttpRequest): void {
193193
}
194194
}
195195

196+
const parseQuery = (searchParams: URLSearchParams): HttpQueryParameters => {
197+
const queryMap = new Object();
198+
for (const [key, value] of searchParams) {
199+
const parameterName = decodeURIComponent(key);
200+
const parameterValue = decodeURIComponent(value);
201+
202+
let existingEntry = queryMap[parameterName] as string[];
203+
if (!existingEntry) {
204+
existingEntry = new Array<string>();
205+
queryMap[parameterName] = existingEntry;
206+
}
207+
existingEntry.push(parameterValue);
208+
}
209+
const query = new HttpQueryParameters(
210+
queryMap as { string: string | string[] }
211+
);
212+
return query;
213+
};
214+
196215
export class HttpRequestBuilder {
197216
static fromPath(requestData: HttpRequestFromPath): HttpRequest {
198217
const url = new URL("file://" + requestData.path);
199-
200-
const queryMap = new Object();
201-
const queryString = url.search.substring(1);
202-
for (const entry of queryString.split("&")) {
203-
const pair = entry.split("=");
204-
const parameterName = decodeURIComponent(pair[0]);
205-
const parameterValue = decodeURIComponent(pair[1]);
206-
207-
let existingEntry = queryMap[parameterName] as string[];
208-
if (!existingEntry) {
209-
existingEntry = new Array<string>();
210-
queryMap[parameterName] = existingEntry;
211-
}
212-
existingEntry.push(parameterValue);
213-
}
214-
const query = new HttpQueryParameters(
215-
queryMap as { string: string | string[] }
216-
);
218+
const query = parseQuery(url.searchParams);
217219

218220
const request = {
219221
timestamp: requestData.timestamp ? requestData.timestamp : undefined,

test/index.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ test("Building exchange from path", () => {
8181
expect(count).toBe(2);
8282
});
8383

84+
test("Building exchange from path with no query parameters", () => {
85+
const timestamp = new Date();
86+
const request = HttpRequestBuilder.fromPath({
87+
timestamp: timestamp,
88+
method: HttpMethod.GET,
89+
protocol: HttpProtocol.HTTPS,
90+
host: "example.com",
91+
headers: {},
92+
path: "/my/path",
93+
body: "request string body"
94+
});
95+
expect(request.query.toJSON()).toEqual({});
96+
});
97+
8498
test("Http exchanges from JSON with path", () => {
8599
const json = `{
86100
"request": {

0 commit comments

Comments
 (0)