Skip to content

Commit

Permalink
Support URLs using hashes. Fix #255
Browse files Browse the repository at this point in the history
  • Loading branch information
aarranz committed Nov 17, 2016
1 parent a2407e7 commit 067f1aa
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
66 changes: 66 additions & 0 deletions src/js_tests/wirecloud/ioSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,72 @@
})).toBe(original.toString());
});

it("should maintain the hash part for proxied URLs", function () {
var original = "http://server:1234/path?q=1#id";
var expected = "https://wirecloud.example.com/cdp/http/server:1234/path?q=1#id";

expect(Wirecloud.io.buildProxyURL(original)).toBe(expected);
});

it("should maintain the hash part for proxied URLs when providing parameters", function () {
var original = "http://server:1234/path#id";
var expected = "https://wirecloud.example.com/cdp/http/server:1234/path?q=1#id";

expect(Wirecloud.io.buildProxyURL(original, {
method: 'GET',
parameters: {
q: 1
}
})).toBe(expected);
});

it("should maintain the \"hash\" part when using blob URLs", function () {
// In fact, blob urls don't support hashes. This test checks
// buildProxyURL don't process data as a hash component
var original = "blob:d3958f5c-0777-0845-9dcf-2cb28783acaf#id";

expect(Wirecloud.io.buildProxyURL(original, {
method: 'GET'
})).toBe(original);
});

it("should maintain the \"hash\" part when using blob URLs and providing parameters", function () {
// In fact, blob urls don't support hashes. This test checks
// buildProxyURL don't process data as a hash component
var original = "blob:d3958f5c-0777-0845-9dcf-2cb28783acaf#id";

expect(Wirecloud.io.buildProxyURL(original, {
method: 'GET',
parameters: {
e: 1,
b: "c"
}
})).toBe(original);
});

it("should maintain the \"hash\" part when using data URLs", function () {
// In fact, data urls don't support hashes. This test checks
// buildProxyURL don't process data as a hash component
var original = "data:text/html,lots%20of%20text...<p><a%20name%3D\"bottom\">bottom</a>?arg=val#id";

expect(Wirecloud.io.buildProxyURL(original, {
method: 'GET'
})).toBe(original);
});

it("should maintain the \"hash\" part when using data URLs and providing parameters", function () {
// In fact, data urls don't support hashes. This test checks
// buildProxyURL don't process data as a hash component
var original = "data:text/html,lots%20of%20text...<p><a%20name%3D\"bottom\">bottom</a>?arg=val#id";

expect(Wirecloud.io.buildProxyURL(original, {
method: 'GET',
parameters: {
e: 1,
b: "c"
}
})).toBe(original);
});
});

describe("makeRequest(url, options)", function () {
Expand Down
14 changes: 9 additions & 5 deletions src/wirecloud/platform/static/js/wirecloud/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@
var io = {};

io.buildProxyURL = function buildProxyURL(url, options) {
var forceProxy, inmemoryurl;
var forceProxy, hash;

options = utils.merge({
method: 'POST',
Expand All @@ -248,17 +248,21 @@
}

forceProxy = !!options.forceProxy;
inmemoryurl = ["blob:", "data:"].indexOf(url.protocol) !== -1;
if (["blob:", "data:"].indexOf(url.protocol) !== -1) {
return url.toString();
}

if (!inmemoryurl && (forceProxy || (options.supportsAccessControl !== true && url.origin !== Wirecloud.location.domain))) {
hash = url.hash;
url.hash = '';
if (forceProxy || (options.supportsAccessControl !== true && url.origin !== Wirecloud.location.domain)) {
url = Wirecloud.location.domain +
Wirecloud.URLs.PROXY.evaluate({protocol: url.protocol.slice(0, -1), domain: url.host, path: url.pathname}) + url.search;
} else {
url = url.toString();
}

// Add parameters
if (!inmemoryurl && options.parameters != null && (typeof options.parameters === 'string' || typeof options.parameters === 'object')) {
if (options.parameters != null && (typeof options.parameters === 'string' || typeof options.parameters === 'object')) {
if (['PUT', 'POST'].indexOf(options.method) === -1 || options.postBody != null) {
if (url.indexOf('?') !== -1) {
url += '&' + toQueryString(options.parameters);
Expand All @@ -268,7 +272,7 @@
}
}

return url;
return url + hash;
};

io.makeRequest = function makeRequest(url, options) {
Expand Down

0 comments on commit 067f1aa

Please sign in to comment.