Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
style: rename UrlUtility.ts to UrlUtils.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
pamapa committed Oct 25, 2021
1 parent 9fb4aa0 commit add0bbe
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 40 deletions.
20 changes: 10 additions & 10 deletions src/SigninRequest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Log, UrlUtility } from "./utils";
import { Log, UrlUtils } from "./utils";
import { SigninState } from "./SigninState";

export interface SigninRequestArgs {
Expand Down Expand Up @@ -82,26 +82,26 @@ export class SigninRequest {
skipUserInfo
});

url = UrlUtility.addQueryParam(url, "client_id", client_id);
url = UrlUtility.addQueryParam(url, "redirect_uri", redirect_uri);
url = UrlUtility.addQueryParam(url, "response_type", response_type);
url = UrlUtility.addQueryParam(url, "scope", scope);
url = UrlUtils.addQueryParam(url, "client_id", client_id);
url = UrlUtils.addQueryParam(url, "redirect_uri", redirect_uri);
url = UrlUtils.addQueryParam(url, "response_type", response_type);
url = UrlUtils.addQueryParam(url, "scope", scope);

url = UrlUtility.addQueryParam(url, "state", this.state.id);
url = UrlUtils.addQueryParam(url, "state", this.state.id);
if (this.state.code_challenge) {
url = UrlUtility.addQueryParam(url, "code_challenge", this.state.code_challenge);
url = UrlUtility.addQueryParam(url, "code_challenge_method", "S256");
url = UrlUtils.addQueryParam(url, "code_challenge", this.state.code_challenge);
url = UrlUtils.addQueryParam(url, "code_challenge_method", "S256");
}

const optional: Record<string, any> = { prompt, display, max_age, ui_locales, login_hint, acr_values, resource, request, request_uri, response_mode };
for (const key in optional) {
if (optional[key]) {
url = UrlUtility.addQueryParam(url, key, optional[key]);
url = UrlUtils.addQueryParam(url, key, optional[key]);
}
}

for (const key in extraQueryParams) {
url = UrlUtility.addQueryParam(url, key, extraQueryParams[key]);
url = UrlUtils.addQueryParam(url, key, extraQueryParams[key]);
}

this.url = url;
Expand Down
4 changes: 2 additions & 2 deletions src/SigninResponse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Timer, UrlUtility } from "./utils";
import { Timer, UrlUtils } from "./utils";
import type { UserProfile } from "./User";

const OidcScope = "openid";
Expand All @@ -28,7 +28,7 @@ export class SigninResponse {
public profile: UserProfile;

public constructor(url?: string, delimiter = "#") {
const values = UrlUtility.parseUrlFragment(url, delimiter);
const values = UrlUtils.parseUrlFragment(url, delimiter);

this.error = values.error;
this.error_description = values.error_description;
Expand Down
8 changes: 4 additions & 4 deletions src/SignoutRequest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Log, UrlUtility } from "./utils";
import { Log, UrlUtils } from "./utils";
import { State } from "./State";

export interface SignoutRequestArgs {
Expand Down Expand Up @@ -29,17 +29,17 @@ export class SignoutRequest {
}

if (post_logout_redirect_uri) {
url = UrlUtility.addQueryParam(url, "post_logout_redirect_uri", post_logout_redirect_uri);
url = UrlUtils.addQueryParam(url, "post_logout_redirect_uri", post_logout_redirect_uri);

if (state_data) {
this.state = new State({ data: state_data, request_type });

url = UrlUtility.addQueryParam(url, "state", this.state.id);
url = UrlUtils.addQueryParam(url, "state", this.state.id);
}
}

for (const key in extraQueryParams) {
url = UrlUtility.addQueryParam(url, key, extraQueryParams[key]);
url = UrlUtils.addQueryParam(url, key, extraQueryParams[key]);
}

this.url = url;
Expand Down
4 changes: 2 additions & 2 deletions src/SignoutResponse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { UrlUtility } from "./utils";
import { UrlUtils } from "./utils";

export class SignoutResponse {
public error: string | undefined;
Expand All @@ -11,7 +11,7 @@ export class SignoutResponse {
public state: any | undefined;

public constructor(url?: string) {
const values = UrlUtility.parseUrlFragment(url, "?");
const values = UrlUtils.parseUrlFragment(url, "?");

this.error = values.error;
this.error_description = values.error_description;
Expand Down
4 changes: 2 additions & 2 deletions src/navigators/PopupWindow.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Log, UrlUtility } from "../utils";
import { Log, UrlUtils } from "../utils";
import type { IWindow, NavigateParams, NavigateResponse } from "./IWindow";

const checkForPopupClosedInterval = 500;
Expand Down Expand Up @@ -148,7 +148,7 @@ export class PopupWindow implements IWindow {
url = url || window.location.href;

if (url) {
const data = UrlUtility.parseUrlFragment(url, delimiter);
const data = UrlUtils.parseUrlFragment(url, delimiter);
window.opener?.postMessage(JSON.stringify({
data,
url,
Expand Down
4 changes: 2 additions & 2 deletions src/utils/UrlUtility.ts → src/utils/UrlUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { Log } from "./Log";

export class UrlUtility {
export class UrlUtils {
public static addQueryParam(url: string, name: string, value: string | number | boolean): string {
if (url.indexOf("?") < 0) {
url += "?";
Expand Down Expand Up @@ -46,7 +46,7 @@ export class UrlUtility {
while ((m = regex.exec(value)) !== null) {
params[decodeURIComponent(m[1])] = decodeURIComponent(m[2].replace(/\+/g, " "));
if (counter++ > 50) {
Log.error("UrlUtility.parseUrlFragment: response exceeded expected number of parameters", value);
Log.error("UrlUtils.parseUrlFragment: response exceeded expected number of parameters", value);
return {
error: "Response exceeded expected number of parameters"
};
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from "./CryptoUtils";
export * from "./Event";
export * from "./Log";
export * from "./Timer";
export * from "./UrlUtility";
export * from "./UrlUtils";
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { UrlUtility } from "../../../src/utils";
import { UrlUtils } from "../../../src/utils";

describe("UrlUtility", () => {
describe("UrlUtils", () => {

describe("addQueryParam", () => {

it("should add ? if not present", () => {
// act
const result = UrlUtility.addQueryParam("url", "foo", "test");
const result = UrlUtils.addQueryParam("url", "foo", "test");

// assert
expect(result).toEqual("url?foo=test");
});

it("should not add ? if already present", () => {
// act
const result = UrlUtility.addQueryParam("url?", "foo", "test");
const result = UrlUtils.addQueryParam("url?", "foo", "test");

// assert
expect(result).toEqual("url?foo=test");
});

it("should add & if needed", () => {
// act
const result = UrlUtility.addQueryParam("url?x=1", "foo", "test");
const result = UrlUtils.addQueryParam("url?x=1", "foo", "test");

// assert
expect(result).toEqual("url?x=1&foo=test");
});

it("should stringify boolean values", () => {
// act
let result = UrlUtility.addQueryParam("url?x=1", "foo", true);
result = UrlUtility.addQueryParam(result, "bar", false);
let result = UrlUtils.addQueryParam("url?x=1", "foo", true);
result = UrlUtils.addQueryParam(result, "bar", false);

// assert
expect(result).toEqual("url?x=1&foo=true&bar=false");
});

it("should stringify numeric values", () => {
// act
const result = UrlUtility.addQueryParam("url?x=1", "foo", 1.2);
const result = UrlUtils.addQueryParam("url?x=1", "foo", 1.2);

// assert
expect(result).toEqual("url?x=1&foo=1.2");
});

it("should urlencode key and value", () => {
it("should url encode key and value", () => {
// act
const result = UrlUtility.addQueryParam("url", "#", "#");
const result = UrlUtils.addQueryParam("url", "#", "#");

// assert
expect(result).toEqual("url?%23=%23");
Expand All @@ -61,55 +61,55 @@ describe("UrlUtility", () => {

it("should parse key/value pairs", () => {
// act
const result = UrlUtility.parseUrlFragment("a=apple&b=banana&c=carrot");
const result = UrlUtils.parseUrlFragment("a=apple&b=banana&c=carrot");

// assert
expect(result).toEqual({ a: "apple", b: "banana", c: "carrot" });
});

it("should parse any order", () => {
// act
const result = UrlUtility.parseUrlFragment("b=banana&c=carrot&a=apple");
const result = UrlUtils.parseUrlFragment("b=banana&c=carrot&a=apple");

// assert
expect(result).toEqual({ a: "apple", b: "banana", c: "carrot" });
});

it("should parse past host name and hash fragment", () => {
// act
const result = UrlUtility.parseUrlFragment("http://server?test1=xoxo&test2=xoxo/#a=apple&b=banana&c=carrot");
const result = UrlUtils.parseUrlFragment("http://server?test1=xoxo&test2=xoxo/#a=apple&b=banana&c=carrot");

// assert
expect(result).toEqual({ a: "apple", b: "banana", c: "carrot" });
});

it("should parse query string", () => {
// act
const result = UrlUtility.parseUrlFragment("http://server?test1=xoxo&test2=yoyo", "?");
const result = UrlUtils.parseUrlFragment("http://server?test1=xoxo&test2=yoyo", "?");

// assert
expect(result).toEqual({ test1: "xoxo", test2: "yoyo" });
});

it("should parse query string up to hash", () => {
// act
const result = UrlUtility.parseUrlFragment("http://server?test1=xoxo&test2=yoyo#a=apple&b=banana&c=carrot", "?");
const result = UrlUtils.parseUrlFragment("http://server?test1=xoxo&test2=yoyo#a=apple&b=banana&c=carrot", "?");

// assert
expect(result).toEqual({ test1: "xoxo", test2: "yoyo" });
});

it("should return error for long values", () => {
// act
const result = UrlUtility.parseUrlFragment("a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple");
const result = UrlUtils.parseUrlFragment("a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple");

// assert
expect(result).toHaveProperty("error");
});

it("should return empty object for empty string", () => {
// act
const result = UrlUtility.parseUrlFragment("");
const result = UrlUtils.parseUrlFragment("");

// assert
expect(result).toEqual({});
Expand Down

0 comments on commit add0bbe

Please sign in to comment.