Skip to content

Commit

Permalink
fixup! Fix web-platform-tests#2669: Add alternate_hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
gsnedders committed Apr 30, 2018
1 parent 2c46f38 commit cd5bb89
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 46 deletions.
10 changes: 6 additions & 4 deletions tools/serve/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,11 +437,13 @@ def check_subdomains(domains, paths, bind_address, ssl_config, aliases):
def make_hosts_file(config, host):
rv = []

for domain in config["domains"].values():
rv.append("%s\t%s\n" % (host, domain))
for per_host_domains in config["domains"].values():
for domain in per_host_domains.values():
rv.append("%s\t%s\n" % (host, domain))

for not_domain in config.get("not_domains", {}).values():
rv.append("0.0.0.0\t%s\n" % not_domain)
for per_host_domains in config["not_domains"].values():
for not_domain in per_host_domains.values():
rv.append("0.0.0.0\t%s\n" % not_domain)

return "".join(rv)

Expand Down
60 changes: 38 additions & 22 deletions tools/wptserve/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,16 @@ def test_domains():
not_subdomains={"x", "y"})
domains = c.domains
assert domains == {
"main_": "foo.bar",
"main_a": "a.foo.bar",
"main_b": "b.foo.bar",
"alt_": "foo2.bar",
"alt_a": "a.foo2.bar",
"alt_b": "b.foo2.bar",
"": {
"": "foo.bar",
"a": "a.foo.bar",
"b": "b.foo.bar",
},
"alt": {
"": "foo2.bar",
"a": "a.foo2.bar",
"b": "b.foo2.bar",
},
}


Expand All @@ -293,10 +297,14 @@ def test_not_domains():
not_subdomains={"x", "y"})
not_domains = c.not_domains
assert not_domains == {
"main_x": "x.foo.bar",
"main_y": "y.foo.bar",
"alt_x": "x.foo2.bar",
"alt_y": "y.foo2.bar",
"": {
"x": "x.foo.bar",
"y": "y.foo.bar",
},
"alt": {
"x": "x.foo2.bar",
"y": "y.foo2.bar",
},
}


Expand All @@ -307,8 +315,12 @@ def test_domains_not_domains_intersection():
not_subdomains={"x", "y"})
domains = c.domains
not_domains = c.not_domains
assert len(set(domains.iterkeys()) & set(not_domains.iterkeys())) == 0
assert len(set(domains.itervalues()) & set(not_domains.itervalues())) == 0
assert len(set(domains.iterkeys()) ^ set(not_domains.iterkeys())) == 0
for host in domains.iterkeys():
host_domains = domains[host]
host_not_domains = not_domains[host]
assert len(set(host_domains.iterkeys()) & set(host_not_domains.iterkeys())) == 0
assert len(set(host_domains.itervalues()) & set(host_not_domains.itervalues())) == 0


def test_all_domains():
Expand All @@ -318,16 +330,20 @@ def test_all_domains():
not_subdomains={"x", "y"})
all_domains = c.all_domains
assert all_domains == {
"main_": "foo.bar",
"main_a": "a.foo.bar",
"main_b": "b.foo.bar",
"main_x": "x.foo.bar",
"main_y": "y.foo.bar",
"alt_": "foo2.bar",
"alt_a": "a.foo2.bar",
"alt_b": "b.foo2.bar",
"alt_x": "x.foo2.bar",
"alt_y": "y.foo2.bar",
"": {
"": "foo.bar",
"a": "a.foo.bar",
"b": "b.foo.bar",
"x": "x.foo.bar",
"y": "y.foo.bar",
},
"alt": {
"": "foo2.bar",
"a": "a.foo2.bar",
"b": "b.foo2.bar",
"x": "x.foo2.bar",
"y": "y.foo2.bar",
},
}


Expand Down
22 changes: 12 additions & 10 deletions tools/wptserve/wptserve/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,32 +174,34 @@ def server_host(self, v):
@property
def domains(self):
hosts = self.alternate_hosts.copy()
assert "main" not in hosts
hosts["main"] = self.browser_host
assert "" not in hosts
hosts[""] = self.browser_host

rv = {}
for name, host in hosts.iteritems():
rv.update({(name + "_" + subdomain): (subdomain.encode("idna") + u"." + host)
for subdomain in self.subdomains})
rv[name + "_"] = host
rv[name] = {subdomain: (subdomain.encode("idna") + u"." + host)
for subdomain in self.subdomains}
rv[name][""] = host
return rv

@property
def not_domains(self):
hosts = self.alternate_hosts.copy()
assert "main" not in hosts
hosts["main"] = self.browser_host
assert "" not in hosts
hosts[""] = self.browser_host

rv = {}
for name, host in hosts.iteritems():
rv.update({(name + "_" + subdomain): (subdomain.encode("idna") + u"." + host)
for subdomain in self.not_subdomains})
rv[name] = {subdomain: (subdomain.encode("idna") + u"." + host)
for subdomain in self.not_subdomains}
return rv

@property
def all_domains(self):
rv = self.domains.copy()
rv.update(self.not_domains)
nd = self.not_domains
for host in rv:
rv[host].update(nd[host])
return rv

@property
Expand Down
13 changes: 3 additions & 10 deletions tools/wptserve/wptserve/pipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,17 +442,10 @@ def config_replacement(match):
value = request.headers
elif field == "GET":
value = FirstWrapper(request.GET)
elif field == "domains":
elif field == "hosts":
value = request.server.config.all_domains
ttype, field = tokens.popleft()
if ttype != "index":
raise Exception(
"unexpected token type %s (token '%r'), expected ident" % (ttype, field)
)
if field in value:
value = value[field]
else:
value = value["main_" + field]
elif field == "domains":
value = request.server.config.all_domains[""]
elif field == "host":
value = request.server.config["browser_host"]
elif field in request.server.config:
Expand Down

0 comments on commit cd5bb89

Please sign in to comment.