fix bug in createUrl of apiclient and added tests

This commit is contained in:
Sebastian Sdorra
2018-07-12 08:22:27 +02:00
parent 08a8d36c5e
commit ed077f704f
2 changed files with 17 additions and 4 deletions

View File

@@ -26,15 +26,15 @@ function handleStatusCode(response: Response) {
return response;
}
function createUrl(url: string) {
export function createUrl(url: string) {
if (url.indexOf("://") > 0) {
return url;
}
let urlWithEndingSlash = url;
let urlWithStartingSlash = url;
if (url.indexOf("/") !== 0) {
urlWithEndingSlash += "/";
urlWithStartingSlash = "/" + urlWithStartingSlash;
}
return `${apiUrl}/api/rest/v2${urlWithEndingSlash}`;
return `${apiUrl}/api/rest/v2${urlWithStartingSlash}`;
}
class ApiClient {

View File

@@ -0,0 +1,13 @@
// @flow
import { createUrl } from "./apiclient";
test("create url, should not change absolute urls", () => {
expect(createUrl("https://www.scm-manager.org")).toBe(
"https://www.scm-manager.org"
);
});
test("create url, should add prefix for api", () => {
expect(createUrl("/users")).toBe("/scm/api/rest/v2/users");
expect(createUrl("users")).toBe("/scm/api/rest/v2/users");
});