Skip to content

Commit 09a9da3

Browse files
committed
Update resolveUrl to ignore absolute URLs
1 parent ad3372f commit 09a9da3

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

http-api-client/src/request.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const Async = require("crocks/Async");
55
const assign = require("crocks/helpers/assign");
66
const compose = require("crocks/helpers/compose");
77
const curry = require("crocks/core/curry");
8+
const identity = require("crocks/combinators/identity");
9+
const ifElse = require("crocks/logic/ifElse");
810
const flip = require("crocks/combinators/flip");
911
const liftA2 = require("crocks/helpers/liftA2");
1012
const maybePropOr = require("crocks/helpers/getPropOr");
@@ -15,6 +17,7 @@ const substitution = require("crocks/combinators/substitution");
1517
const { getProp } = require("@epistemology-factory/crocks-ext/Async");
1618
const { prepend } = require("@epistemology-factory/crocks-ext/helpers");
1719

20+
const { isRelativeUrl } = require("./predicates");
1821
const { newError } = require("./errors");
1922

2023
// getPropOr :: a -> String -> b -> Async c
@@ -110,11 +113,17 @@ const addHeaders = curry((factory) =>
110113
/**
111114
* Resolves a relative URL in a {@link HttpRequest} to an absolute URL.
112115
*
116+
* Leaves absolute URLs unchanged.
117+
*
113118
* Takes a base URL to resolve to, followed by a request.
114119
*/
115120
// resolveUrl :: String -> HttpRequestPolicy
116121
const resolveUrl = curry((base) =>
117-
modifyRequest("url", map(prepend(base)))
122+
modifyRequest("url", map(ifElse(
123+
isRelativeUrl,
124+
prepend(base),
125+
identity
126+
)))
118127
);
119128

120129
module.exports = {

http-api-client/test/request.test.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,27 @@ describe("Http Request", function() {
7373
});
7474

7575
describe("resolving url", function() {
76-
it("should join path to base", async function() {
77-
const base = "http://localhost:3000";
78-
const path = "/v1/foo/bar";
76+
const base = "http://localhost:3000";
77+
const path = "/v1/foo/bar";
7978

79+
it("should join relative path to base", async function() {
8080
const result = await resolveUrl(base, {
8181
url: path
8282
}).toPromise();
8383

8484
assertThat(result, hasProperty("url", equalTo(`${base}${path}`)));
8585
});
8686

87+
it("should leave absolute url", async function() {
88+
const absoluteUrl = "http://foobar.com";
89+
90+
const result = await resolveUrl(base, {
91+
url: absoluteUrl
92+
}).toPromise();
93+
94+
assertThat(result, hasProperty("url", equalTo(absoluteUrl)));
95+
});
96+
8797
it("should reject if url missing in request", async function() {
8898
await promiseThat(resolveUrl("", {}).toPromise(), isRejectedWith(allOf(
8999
instanceOf(Error),

0 commit comments

Comments
 (0)