kopia lustrzana https://github.com/c9/core
add unit tests for domain mappings
rodzic
7d8062ddbf
commit
b340c081af
|
@ -9,16 +9,35 @@
|
|||
var assert = require("assert");
|
||||
|
||||
function getHost(req) {
|
||||
if (req.headers)
|
||||
return req.headers.host;
|
||||
var host =
|
||||
req.headers && req.headers.host ||
|
||||
req.host ||
|
||||
req.url ||
|
||||
req;
|
||||
|
||||
return host.replace(/^https?:\/\/([^/]*).*/, "$1");
|
||||
}
|
||||
|
||||
if (req.host)
|
||||
return req.host;
|
||||
function splitDomain(req, domains) {
|
||||
var host = getHost(req);
|
||||
|
||||
var allDomains = domains.join("|");
|
||||
var domainRe = new RegExp("^(.*?)\\.?(" + allDomains.replace(".", "\\.") + ")(?::(\\d+))?$");
|
||||
|
||||
if (req.url)
|
||||
return req.url.replace(/^https?:\/\/([^/]*).*/, "$1");
|
||||
|
||||
return req;
|
||||
var m = host.match(domainRe);
|
||||
if (m) {
|
||||
return {
|
||||
subDomains: m[1],
|
||||
domainName: m[2],
|
||||
port: m[3] || ""
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
subDomains: "",
|
||||
domainName: host.split(":")[0],
|
||||
port: host.split(":")[1] || ""
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -115,4 +134,6 @@ function replaceDomain(url, domain) {
|
|||
module.exports = {
|
||||
replaceDomains: replaceDomains,
|
||||
getBaseUrl: getBaseUrl,
|
||||
splitDomain: splitDomain,
|
||||
getHost: getHost
|
||||
};
|
|
@ -9,7 +9,7 @@ if (typeof define === "undefined") {
|
|||
require("../../test/setup_paths");
|
||||
}
|
||||
|
||||
var assert = require("assert");
|
||||
var assert = require("assert-diff");
|
||||
var urls = require("./urls");
|
||||
var url = require("url");
|
||||
var sinon = require("sinon");
|
||||
|
@ -126,4 +126,28 @@ describe("urls", function() {
|
|||
var baseUrl = urls.getBaseUrl(input, "https://preview.$DOMAIN");
|
||||
assert.equal(baseUrl, "https://preview.newclient-lennartcl.c9.io");
|
||||
});
|
||||
|
||||
it("should split domains", function() {
|
||||
var domains = ["c9.io", "c9users.io", "cs50.io"];
|
||||
assert.deepEqual(urls.splitDomain("fjakobs-ace.c9.io", domains), {
|
||||
domainName: "c9.io",
|
||||
subDomains: "fjakobs-ace",
|
||||
port: ""
|
||||
});
|
||||
assert.deepEqual(urls.splitDomain("preview.c9users.io", domains), {
|
||||
domainName: "c9users.io",
|
||||
subDomains: "preview",
|
||||
port: ""
|
||||
});
|
||||
assert.deepEqual(urls.splitDomain("fjakobs.ace.cs50.io", domains), {
|
||||
domainName: "cs50.io",
|
||||
subDomains: "fjakobs.ace",
|
||||
port: ""
|
||||
});
|
||||
assert.deepEqual(urls.splitDomain("fjakobs.ace.cs50.io:8081", domains), {
|
||||
domainName: "cs50.io",
|
||||
subDomains: "fjakobs.ace",
|
||||
port: "8081"
|
||||
});
|
||||
});
|
||||
});
|
Ładowanie…
Reference in New Issue