Browse Source

pass host from header to target_from_uri

main
Marc Brakken 5 years ago
parent
commit
9479b6ad22
  1. 4
      lib/utils.js
  2. 2
      server.js
  3. 23
      test/utils.js

4
lib/utils.js

@ -217,12 +217,12 @@ module.exports.request_parameters = function request_parameters(accepted_params,
}, {});
};
module.exports.target_from_url = function target_from_url(uri) {
module.exports.target_from_url = function target_from_url(hostname, uri) {
var parsed = url.parse(uri);
var pathname = parsed.pathname;
if (pathname.substring(0,2) !== "/.") {
return "/" + parsed.hostname.split(".").reverse().join(".") + pathname;
return "/" + hostname.split(".").reverse().join(".") + pathname;
} else {
return "/" + pathname.substring(2);
}

2
server.js

@ -48,7 +48,7 @@ http.createServer(function(req, res){
res.setHeader("Access-Control-Expose-Headers", EXPOSED_HEADERS);
// all requests are interrorgated for these values
var target_url = utils.target_from_url(req.url);
var target_url = utils.target_from_url(req.headers["host"], req.url);
// check for request parameters, first in the header and then in the querystring
// Moved these to an object to avoid possible issues from "private" being a reserved word

23
test/utils.js

@ -166,32 +166,27 @@ describe("utils.js", function() {
describe("#target_from_url(uri)", function() {
it("should set target from url", function() {
var test_uri = "http://test.jsfs.com/path/to/file.json";
var result = utils.target_from_url(test_uri);
var host = "test.jsfs.com";
var uri = "/path/to/file.json";
var result = utils.target_from_url(host, uri);
expect(result).to.be.a("string");
expect(result).to.equal(TEST_PATH);
});
it("should return fully specificed target path", function() {
var test_uri = "http://test2.jsfs.com/.com.jsfs.test/path/to/file.json";
var result = utils.target_from_url(test_uri);
expect(result).to.be.a("string");
expect(result).to.equal(TEST_PATH);
});
it("should ignore the port", function() {
var test_uri = "http://test.jsfs.com:1234/path/to/file.json";
var result = utils.target_from_url(test_uri);
var host = "test2.jsfs.com";
var uri = "/.com.jsfs.test/path/to/file.json";
var result = utils.target_from_url(host, uri);
expect(result).to.be.a("string");
expect(result).to.equal(TEST_PATH);
});
it("should ignore query params", function() {
var test_uri = "http://test.jsfs.com/path/to/file.json?test=query&more=fun";
var result = utils.target_from_url(test_uri);
var host = "test.jsfs.com";
var uri = "/path/to/file.json?test=query&more=fun";
var result = utils.target_from_url(host, uri);
expect(result).to.be.a("string");
expect(result).to.equal(TEST_PATH);

Loading…
Cancel
Save