зеркало из https://github.com/Azure/ms-rest-js.git
Merge pull request #238 from Azure/daschult/appendPath
Add URLBuilder.appendPath()
This commit is contained in:
Коммит
940900d30c
|
@ -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) {
|
||||
|
|
22
lib/url.ts
22
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.
|
||||
*/
|
||||
|
|
|
@ -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()", () => {
|
||||
|
|
Загрузка…
Ссылка в новой задаче