Skip to content

Commit 4a63220

Browse files
committed
Adding tests for url parsing and comments to the private url parsing functions
1 parent 6c68a75 commit 4a63220

File tree

3 files changed

+70
-17
lines changed

3 files changed

+70
-17
lines changed

spec/core/urlGeneration.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,37 @@ cases.push({
113113
.query({ $select: "id" }),
114114
});
115115

116+
// handling an invalid input
117+
cases.push({
118+
url: "https://graph.microsoft.com/v1.0/me/people?$select=displayName,title&select=value&test",
119+
request: client
120+
.api("/me/people")
121+
.version("v1.0")
122+
.select(["displayName", "title"])
123+
.query({ select: "value" })
124+
.query("test"),
125+
});
126+
127+
// handling an invalid input
128+
cases.push({
129+
url: "https://graph.microsoft.com/v1.0/me/people?$expand=address($select=home,$expand=city)&$select=home,displayName,title&select=value&test",
130+
request: client
131+
.api("/me/people?$expand=address($select=home,$expand=city)&$select=home")
132+
.version("v1.0")
133+
.select(["displayName", "title"])
134+
.query({ select: "value" })
135+
.query("test"),
136+
});
137+
138+
cases.push({
139+
url: "https://graph.microsoft.com/v1.0/me/people?$expand=home($select=home)&name=test",
140+
request: client.api("/me/people").query("?name=test&$expand=home($select=home)"),
141+
});
142+
cases.push({
143+
url: "https://graph.microsoft.com/v1.0/me/people?$expand=home($select=home)&name=test",
144+
request: client.api("/me/people?name=test&$expand=home($select=home)"),
145+
});
146+
116147
cases.push({
117148
url: "https://graph.microsoft.com/v1.0/me/drive/root?$expand=children($select=name),permissions",
118149
request: client

spec/core/urlParsing.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ const testCases = {
3434
"me?$select=displayName&$select=id": "https://graph.microsoft.com/v1.0/me?$select=displayName,id",
3535
"/me?$filter=b&$filter=a": "https://graph.microsoft.com/v1.0/me?$filter=a",
3636
"https://graph.microsoft.com/v1.0/me?$top=4&$expand=4&$iscount=true&$top=2": "https://graph.microsoft.com/v1.0/me?$top=2&$expand=4&$iscount=true",
37+
"/items?$expand=fields($select=Title)&$expand=name($select=firstName)": "https://graph.microsoft.com/v1.0/items?$expand=fields($select=Title),name($select=firstName)",
38+
39+
// Passing invalid parameters
40+
"/me?&test&123": "https://graph.microsoft.com/v1.0/me?&test&123",
41+
"/me?$select($select=name)": "https://graph.microsoft.com/v1.0/me?$select($select=name)",
3742
};
3843

3944
describe("urlParsing.ts", () => {

src/GraphRequest.ts

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export interface URLComponents {
4949
path?: string;
5050
oDataQueryParams: KeyValuePairObjectStringNumber;
5151
otherURLQueryParams: KeyValuePairObjectStringNumber;
52-
otherURLQueryStrings: string[];
52+
otherURLQueryOptions: any[];
5353
}
5454

5555
/**
@@ -119,7 +119,7 @@ export class GraphRequest {
119119
version: this.config.defaultVersion,
120120
oDataQueryParams: {},
121121
otherURLQueryParams: {},
122-
otherURLQueryStrings: [],
122+
otherURLQueryOptions: [],
123123
};
124124
this._headers = {};
125125
this._options = {};
@@ -240,8 +240,8 @@ export class GraphRequest {
240240
}
241241
}
242242

243-
if (urlComponents.otherURLQueryStrings.length !== 0) {
244-
for (const str of urlComponents.otherURLQueryStrings) {
243+
if (urlComponents.otherURLQueryOptions.length !== 0) {
244+
for (const str of urlComponents.otherURLQueryOptions) {
245245
query.push(str);
246246
}
247247
}
@@ -250,7 +250,9 @@ export class GraphRequest {
250250

251251
/**
252252
* @private
253-
* @param queryDictionaryOrString
253+
* Parses the query parameters to set the urlComponents property of the GraphRequest object
254+
* @param {string|KeyValuePairObjectStringNumber} queryDictionaryOrString - The query parameter
255+
* @returns The same GraphRequest instance that is being called with
254256
*/
255257
private parseQueryParameter(queryDictionaryOrString: string | KeyValuePairObjectStringNumber): GraphRequest {
256258
if (typeof queryDictionaryOrString === "string") {
@@ -266,20 +268,25 @@ export class GraphRequest {
266268
} else {
267269
this.parseQueryParamenterString(queryDictionaryOrString);
268270
}
269-
} else {
271+
} else if (queryDictionaryOrString.constructor === Object) {
270272
for (const key in queryDictionaryOrString) {
271273
if (queryDictionaryOrString.hasOwnProperty(key)) {
272274
this.setURLComponentsQueryParamater(key, queryDictionaryOrString[key]);
273275
}
274276
}
277+
} else {
278+
/*Push values which are not of key-value structure.
279+
Example-> Handle an invalid input->.query(123) and let the Graph API respond with the error in the URL*/ this.urlComponents.otherURLQueryOptions.push(queryDictionaryOrString);
275280
}
276281

277282
return this;
278283
}
279284

280285
/**
281286
* @private
282-
* @param query
287+
* Parses the query parameter of string type to set the urlComponents property of the GraphRequest object
288+
* @param {string} queryParameter - the query parameters
289+
* returns nothing
283290
*/
284291
private parseQueryParamenterString(queryParameter: string): void {
285292
/* The query key-value pair must be split on the first equals sign to avoid errors in parsing nested query parameters.
@@ -290,13 +297,17 @@ export class GraphRequest {
290297
const paramValue = queryParameter.substring(indexOfFirstEquals + 1, queryParameter.length);
291298
this.setURLComponentsQueryParamater(paramKey, paramValue);
292299
} else {
293-
this.urlComponents.otherURLQueryStrings.push(queryParameter);
300+
/* Push values which are not of key-value structure.
301+
Example-> Handle an invalid input->.query(test), .query($select($select=name)) and let the Graph API respond with the error in the URL*/
302+
this.urlComponents.otherURLQueryOptions.push(queryParameter);
294303
}
295304
}
296305

297306
/**
298307
* @private
299-
* @param query
308+
* Sets values into the urlComponents property of GraphRequest object.
309+
* @param {string} paramKey - the query parameter key
310+
* @param {string} paramValue - the query paramter value
300311
*/
301312
private setURLComponentsQueryParamater(paramKey: string, paramValue: string | number): void {
302313
if (oDataQueryNames.indexOf(paramKey) !== -1) {
@@ -307,19 +318,25 @@ export class GraphRequest {
307318
this.urlComponents.otherURLQueryParams[paramKey] = paramValue;
308319
}
309320
}
310-
311-
private isValidQueryKeyValuePair(queryDictionaryOrString: string): boolean {
312-
const indexofFirstEquals = queryDictionaryOrString.indexOf("=");
321+
/**
322+
* @private
323+
* Check if the query parameter string has a valid key-value structure
324+
* @param {string} queryString - the query parameter string. Example -> "name=value"
325+
* #returns true if the query string has a valid key-value structure else false
326+
*/
327+
private isValidQueryKeyValuePair(queryString: string): boolean {
328+
const indexofFirstEquals = queryString.indexOf("=");
313329
if (indexofFirstEquals === -1) {
314330
return false;
315-
} else {
316-
const indexofOpeningParanthesis = queryDictionaryOrString.indexOf("(");
317-
if (indexofOpeningParanthesis !== -1 && queryDictionaryOrString.indexOf("(") < indexofFirstEquals) {
318-
return false;
319-
}
331+
}
332+
const indexofOpeningParanthesis = queryString.indexOf("(");
333+
if (indexofOpeningParanthesis !== -1 && queryString.indexOf("(") < indexofFirstEquals) {
334+
// Example -> .query($select($expand=true));
335+
return false;
320336
}
321337
return true;
322338
}
339+
323340
/**
324341
* @private
325342
* Updates the custom headers and options for a request

0 commit comments

Comments
 (0)