1
# Copyright (c) 2009 Twisted Matrix Laboratories.
2
# See LICENSE for details.
5
Tests for L{twisted.web.vhost}.
8
from twisted.internet.defer import gatherResults
9
from twisted.trial.unittest import TestCase
10
from twisted.web.http import NOT_FOUND
11
from twisted.web.static import Data
12
from twisted.web.vhost import NameVirtualHost
13
from twisted.web.test.test_web import DummyRequest
14
from twisted.web.test._util import _render
16
class NameVirtualHostTests(TestCase):
18
Tests for L{NameVirtualHost}.
20
def test_renderWithoutHost(self):
22
L{NameVirtualHost.render} returns the result of rendering the
23
instance's C{default} if it is not C{None} and there is no I{Host}
24
header in the request.
26
virtualHostResource = NameVirtualHost()
27
virtualHostResource.default = Data("correct result", "")
28
request = DummyRequest([''])
30
virtualHostResource.render(request), "correct result")
33
def test_renderWithoutHostNoDefault(self):
35
L{NameVirtualHost.render} returns a response with a status of I{NOT
36
FOUND} if the instance's C{default} is C{None} and there is no I{Host}
37
header in the request.
39
virtualHostResource = NameVirtualHost()
40
request = DummyRequest([''])
41
d = _render(virtualHostResource, request)
42
def cbRendered(ignored):
43
self.assertEqual(request.responseCode, NOT_FOUND)
44
d.addCallback(cbRendered)
48
def test_renderWithHost(self):
50
L{NameVirtualHost.render} returns the result of rendering the resource
51
which is the value in the instance's C{host} dictionary corresponding
52
to the key indicated by the value of the I{Host} header in the request.
54
virtualHostResource = NameVirtualHost()
55
virtualHostResource.addHost('example.org', Data("winner", ""))
57
request = DummyRequest([''])
58
request.headers['host'] = 'example.org'
59
d = _render(virtualHostResource, request)
60
def cbRendered(ignored, request):
61
self.assertEqual(''.join(request.written), "winner")
62
d.addCallback(cbRendered, request)
64
# The port portion of the Host header should not be considered.
65
requestWithPort = DummyRequest([''])
66
requestWithPort.headers['host'] = 'example.org:8000'
67
dWithPort = _render(virtualHostResource, requestWithPort)
68
def cbRendered(ignored, requestWithPort):
69
self.assertEqual(''.join(requestWithPort.written), "winner")
70
dWithPort.addCallback(cbRendered, requestWithPort)
72
return gatherResults([d, dWithPort])
75
def test_renderWithUnknownHost(self):
77
L{NameVirtualHost.render} returns the result of rendering the
78
instance's C{default} if it is not C{None} and there is no host
79
matching the value of the I{Host} header in the request.
81
virtualHostResource = NameVirtualHost()
82
virtualHostResource.default = Data("correct data", "")
83
request = DummyRequest([''])
84
request.headers['host'] = 'example.com'
85
d = _render(virtualHostResource, request)
86
def cbRendered(ignored):
87
self.assertEqual(''.join(request.written), "correct data")
88
d.addCallback(cbRendered)
92
def test_renderWithUnknownHostNoDefault(self):
94
L{NameVirtualHost.render} returns a response with a status of I{NOT
95
FOUND} if the instance's C{default} is C{None} and there is no host
96
matching the value of the I{Host} header in the request.
98
virtualHostResource = NameVirtualHost()
99
request = DummyRequest([''])
100
request.headers['host'] = 'example.com'
101
d = _render(virtualHostResource, request)
102
def cbRendered(ignored):
103
self.assertEqual(request.responseCode, NOT_FOUND)
104
d.addCallback(cbRendered)