From d188f4a1fd52e37c287275b4f016f6751ab0598f Mon Sep 17 00:00:00 2001 From: Dan Schulte Date: Mon, 1 Oct 2018 13:55:56 -0700 Subject: [PATCH] Add URLBuilder.appendPath() --- lib/serviceClient.ts | 2 +- lib/url.ts | 22 +++++ test/shared/urlTests.ts | 186 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 209 insertions(+), 1 deletion(-) diff --git a/lib/serviceClient.ts b/lib/serviceClient.ts index aa72084..63fe02e 100644 --- a/lib/serviceClient.ts +++ b/lib/serviceClient.ts @@ -200,7 +200,7 @@ export class ServiceClient { const requestUrl: URLBuilder = URLBuilder.parse(baseUri); if (operationSpec.path) { - requestUrl.setPath(operationSpec.path); + requestUrl.appendPath(operationSpec.path); } if (operationSpec.urlParameters && operationSpec.urlParameters.length > 0) { for (const urlParameter of operationSpec.urlParameters) { diff --git a/lib/url.ts b/lib/url.ts index 2a47b0f..e7984da 100644 --- a/lib/url.ts +++ b/lib/url.ts @@ -227,6 +227,28 @@ export class URLBuilder { } } + /** + * Append the provided path to this URL's existing path. If the provided path contains a query, + * then it will be added to this URL as well. + */ + public appendPath(path: string | undefined): void { + if (path) { + let currentPath: string | undefined = this.getPath(); + if (currentPath) { + if (!currentPath.endsWith("/")) { + currentPath += "/"; + } + + if (path.startsWith("/")) { + path = path.substring(1); + } + + path = currentPath + path; + } + this.set(path, URLTokenizerState.PATH); + } + } + /** * Get the path that has been set in this URL. */ diff --git a/test/shared/urlTests.ts b/test/shared/urlTests.ts index 9cde29d..cbd8424 100644 --- a/test/shared/urlTests.ts +++ b/test/shared/urlTests.ts @@ -567,6 +567,192 @@ describe("URLBuilder", () => { assert.strictEqual(urlBuilder.getPath(), "test/path.html"); assert.strictEqual(urlBuilder.toString(), "/test/path.html"); }); + + it(`from "/test" to "/more/path.html"`, () => { + const urlBuilder = new URLBuilder(); + urlBuilder.setHost("https://www.example.com/test"); + assert.strictEqual(urlBuilder.getPath(), "/test"); + urlBuilder.setPath("/more/path.html"); + assert.strictEqual(urlBuilder.getPath(), "/more/path.html"); + assert.strictEqual(urlBuilder.toString(), "https://www.example.com/more/path.html"); + }); + }); + + describe("appendPath()", () => { + it(`with undefined and undefined`, () => { + const urlBuilder = new URLBuilder(); + urlBuilder.appendPath(undefined); + assert.strictEqual(urlBuilder.getPath(), undefined); + assert.strictEqual(urlBuilder.toString(), ""); + }); + + it(`with undefined and ""`, () => { + const urlBuilder = new URLBuilder(); + urlBuilder.appendPath(""); + assert.strictEqual(urlBuilder.getPath(), undefined); + assert.strictEqual(urlBuilder.toString(), ""); + }); + + it(`with undefined and "/"`, () => { + const urlBuilder = new URLBuilder(); + urlBuilder.appendPath("/"); + assert.strictEqual(urlBuilder.getPath(), "/"); + assert.strictEqual(urlBuilder.toString(), "/"); + }); + + it(`with undefined and "cats"`, () => { + const urlBuilder = new URLBuilder(); + urlBuilder.appendPath("cats"); + assert.strictEqual(urlBuilder.getPath(), "cats"); + assert.strictEqual(urlBuilder.toString(), "/cats"); + }); + + it(`with undefined and "/cats"`, () => { + const urlBuilder = new URLBuilder(); + urlBuilder.appendPath("/cats"); + assert.strictEqual(urlBuilder.getPath(), "/cats"); + assert.strictEqual(urlBuilder.toString(), "/cats"); + }); + + it(`with "" and undefined`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse(""); + urlBuilder.appendPath(undefined); + assert.strictEqual(urlBuilder.getPath(), undefined); + assert.strictEqual(urlBuilder.toString(), ""); + }); + + it(`with "" and ""`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse(""); + urlBuilder.appendPath(""); + assert.strictEqual(urlBuilder.getPath(), undefined); + assert.strictEqual(urlBuilder.toString(), ""); + }); + + it(`with "" and "/"`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse(""); + urlBuilder.appendPath("/"); + assert.strictEqual(urlBuilder.getPath(), "/"); + assert.strictEqual(urlBuilder.toString(), "/"); + }); + + it(`with "" and "cats"`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse(""); + urlBuilder.appendPath("cats"); + assert.strictEqual(urlBuilder.getPath(), "cats"); + assert.strictEqual(urlBuilder.toString(), "/cats"); + }); + + it(`with "" and "/cats"`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse(""); + urlBuilder.appendPath("/cats"); + assert.strictEqual(urlBuilder.getPath(), "/cats"); + assert.strictEqual(urlBuilder.toString(), "/cats"); + }); + + it(`with "/" and undefined`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse("/"); + urlBuilder.appendPath(undefined); + assert.strictEqual(urlBuilder.getPath(), "/"); + assert.strictEqual(urlBuilder.toString(), "/"); + }); + + it(`with "/" and ""`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse("/"); + urlBuilder.appendPath(""); + assert.strictEqual(urlBuilder.getPath(), "/"); + assert.strictEqual(urlBuilder.toString(), "/"); + }); + + it(`with "/" and "/"`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse("/"); + urlBuilder.appendPath("/"); + assert.strictEqual(urlBuilder.getPath(), "/"); + assert.strictEqual(urlBuilder.toString(), "/"); + }); + + it(`with "/" and "cats"`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse("/"); + urlBuilder.appendPath("cats"); + assert.strictEqual(urlBuilder.getPath(), "/cats"); + assert.strictEqual(urlBuilder.toString(), "/cats"); + }); + + it(`with "/" and "/cats"`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse("/"); + urlBuilder.appendPath("/cats"); + assert.strictEqual(urlBuilder.getPath(), "/cats"); + assert.strictEqual(urlBuilder.toString(), "/cats"); + }); + + it(`with "/dogs" and undefined`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse("/dogs"); + urlBuilder.appendPath(undefined); + assert.strictEqual(urlBuilder.getPath(), "/dogs"); + assert.strictEqual(urlBuilder.toString(), "/dogs"); + }); + + it(`with "/dogs" and ""`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse("/dogs"); + urlBuilder.appendPath(""); + assert.strictEqual(urlBuilder.getPath(), "/dogs"); + assert.strictEqual(urlBuilder.toString(), "/dogs"); + }); + + it(`with "/dogs" and "/"`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse("/dogs"); + urlBuilder.appendPath("/"); + assert.strictEqual(urlBuilder.getPath(), "/dogs/"); + assert.strictEqual(urlBuilder.toString(), "/dogs/"); + }); + + it(`with "/dogs" and "cats"`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse("/dogs"); + urlBuilder.appendPath("cats"); + assert.strictEqual(urlBuilder.getPath(), "/dogs/cats"); + assert.strictEqual(urlBuilder.toString(), "/dogs/cats"); + }); + + it(`with "/dogs" and "/cats"`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse("/dogs"); + urlBuilder.appendPath("/cats"); + assert.strictEqual(urlBuilder.getPath(), "/dogs/cats"); + assert.strictEqual(urlBuilder.toString(), "/dogs/cats"); + }); + + it(`with "/dogs/" and undefined`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse("/dogs/"); + urlBuilder.appendPath(undefined); + assert.strictEqual(urlBuilder.getPath(), "/dogs/"); + assert.strictEqual(urlBuilder.toString(), "/dogs/"); + }); + + it(`with "/dogs/" and ""`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse("/dogs/"); + urlBuilder.appendPath(""); + assert.strictEqual(urlBuilder.getPath(), "/dogs/"); + assert.strictEqual(urlBuilder.toString(), "/dogs/"); + }); + + it(`with "/dogs/" and "/"`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse("/dogs/"); + urlBuilder.appendPath("/"); + assert.strictEqual(urlBuilder.getPath(), "/dogs/"); + assert.strictEqual(urlBuilder.toString(), "/dogs/"); + }); + + it(`with "/dogs/" and "cats"`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse("/dogs/"); + urlBuilder.appendPath("cats"); + assert.strictEqual(urlBuilder.getPath(), "/dogs/cats"); + assert.strictEqual(urlBuilder.toString(), "/dogs/cats"); + }); + + it(`with "/dogs/" and "/cats"`, () => { + const urlBuilder: URLBuilder = URLBuilder.parse("/dogs/"); + urlBuilder.appendPath("/cats"); + assert.strictEqual(urlBuilder.getPath(), "/dogs/cats"); + assert.strictEqual(urlBuilder.toString(), "/dogs/cats"); + }); }); describe("setQuery()", () => {