Return URL instance from getApiUrl

This commit is contained in:
Linus Groh
2019-09-27 22:12:49 +01:00
parent 600934183a
commit d01de41e1f
2 changed files with 9 additions and 7 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ export const getApiUrl = path => {
? config.api.baseUrl.slice(1)
: config.api.baseUrl;
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
return `${normalizedBaseUrl}${normalizedPath}`;
return new URL(`${normalizedBaseUrl}${normalizedPath}`);
};
export const isIsoDate = s => ISO_DATE_REGEXP.test(s);
+8 -6
View File
@@ -9,16 +9,18 @@ import {
describe("getApiUrl", () => {
test("without base URL", () => {
// See testURL in jest.config.js
expect(getApiUrl("foo")).toBe("http://localhost/foo");
expect(getApiUrl("/foo")).toBe("http://localhost/foo");
expect(getApiUrl("/foo/bar")).toBe("http://localhost/foo/bar");
expect(getApiUrl("foo").href).toBe("http://localhost/foo");
expect(getApiUrl("/foo").href).toBe("http://localhost/foo");
expect(getApiUrl("/foo/bar").href).toBe("http://localhost/foo/bar");
});
test("with base URL", () => {
config.api.baseUrl = "http://example.com/owntracks";
expect(getApiUrl("foo")).toBe("http://example.com/owntracks/foo");
expect(getApiUrl("/foo")).toBe("http://example.com/owntracks/foo");
expect(getApiUrl("/foo/bar")).toBe("http://example.com/owntracks/foo/bar");
expect(getApiUrl("foo").href).toBe("http://example.com/owntracks/foo");
expect(getApiUrl("/foo").href).toBe("http://example.com/owntracks/foo");
expect(getApiUrl("/foo/bar").href).toBe(
"http://example.com/owntracks/foo/bar"
);
});
});