Skip to content

Commit

Permalink
[CommonRecorder] Custom query-string
Browse files Browse the repository at this point in the history
  • Loading branch information
sadasant committed Aug 15, 2019
1 parent 55c2c53 commit cd02687
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 0 additions & 2 deletions sdk/test-utils/recorder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@
"@types/mocha": "^5.2.5",
"@types/nise": "^1.4.0",
"@types/nock": "^10.0.1",
"@types/query-string": "6.2.0",
"fs-extra": "^8.1.0",
"nise": "^1.4.10",
"nock": "^10.0.6",
"query-string": "^5.0.0",
"rimraf": "^2.6.2",
"rollup": "^1.16.3",
"rollup-plugin-commonjs": "^10.0.0",
Expand Down
8 changes: 4 additions & 4 deletions sdk/test-utils/recorder/src/baseRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

import fs from "fs-extra";
import nise from "nise";
import queryString from "query-string";
import {
isBrowser,
blobToString,
escapeRegExp,
env,
TestInfo,
isPlaybackMode,
isRecordMode
isRecordMode,
parseUrl
} from "./utils";
import { customConsoleLog } from "./customConsoleLog";

Expand Down Expand Up @@ -244,7 +244,7 @@ export class NiseRecorder extends BaseRecorder {
// We're not storing SAS Query Parameters because they may contain sensitive information
// We're ignoring the "_" parameter as well because it's not being added by our code
// More info on "_": https://stackoverflow.com/questions/3687729/who-add-single-underscore-query-parameter
const parsedUrl = queryString.parseUrl(request.url);
const parsedUrl = parseUrl(request.url);
const query: any = {};
for (const param in parsedUrl.query) {
if (!this.sasQueryParameters.includes(param) && param !== "_") {
Expand Down Expand Up @@ -356,7 +356,7 @@ export class NiseRecorder extends BaseRecorder {
reqSend.call(req, data);

// formattedRequest contains all the necessary information to look for a match in our recordings
const parsedUrl = queryString.parseUrl(req.url);
const parsedUrl = parseUrl(req.url);
const formattedRequest = {
method: req.method,
url: parsedUrl.url,
Expand Down
19 changes: 19 additions & 0 deletions sdk/test-utils/recorder/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,22 @@ export function isBrowser(): boolean {
export function delay(milliseconds: number): Promise<void> | null {
return isPlaybackMode() ? null : new Promise((resolve) => setTimeout(resolve, milliseconds));
}

/**
* Usage - `parseUrl(<url>)`
*
* @param {string} url The URL you want to parse
* @returns {any} An object with the url without parameters, and a query object with all the query properties.
*/
export function parseUrl(url: string): any {
const [cleanUrl, ...queryParts] = url.split(/[?&]/);
const query = queryParts.reduce((query: { [key:string]: any }, part) => {
const [name, value] = part.split(/=/);
query[name] = decodeURIComponent(value.replace(/\+/g, ' '));
return query;
}, {});
return {
url: cleanUrl,
query
}
}

0 comments on commit cd02687

Please sign in to comment.