This commit is contained in:
Dan Schulte 2018-10-01 13:55:56 -07:00
Родитель 8f17a6562a
Коммит d188f4a1fd
3 изменённых файлов: 209 добавлений и 1 удалений

Просмотреть файл

@ -200,7 +200,7 @@ export class ServiceClient {
const requestUrl: URLBuilder = URLBuilder.parse(baseUri); const requestUrl: URLBuilder = URLBuilder.parse(baseUri);
if (operationSpec.path) { if (operationSpec.path) {
requestUrl.setPath(operationSpec.path); requestUrl.appendPath(operationSpec.path);
} }
if (operationSpec.urlParameters && operationSpec.urlParameters.length > 0) { if (operationSpec.urlParameters && operationSpec.urlParameters.length > 0) {
for (const urlParameter of operationSpec.urlParameters) { for (const urlParameter of operationSpec.urlParameters) {

Просмотреть файл

@ -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. * Get the path that has been set in this URL.
*/ */

Просмотреть файл

@ -567,6 +567,192 @@ describe("URLBuilder", () => {
assert.strictEqual(urlBuilder.getPath(), "test/path.html"); assert.strictEqual(urlBuilder.getPath(), "test/path.html");
assert.strictEqual(urlBuilder.toString(), "/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()", () => { describe("setQuery()", () => {