~launchpad-pqm/launchpad/devel

1977 by Canonical.com Patch Queue Manager
[r=Bjorn] some clean up of helpers.py
1
Adapting Requests to Countries
2
------------------------------
3
4
Adapting a request to a country allows you to see where the request came from.
5
6
Here's a dummy request. Zope adds the REMOTE_ADDR CGI environment variable
1681.1.62 by Stuart Bishop
Explanatory text
7
for us. Upstream proxy servers (and tinkering users!) may also add
8
X-Forwarded-For: headers. The X-Forwarded-For: header takes precidence
9
if it is set.
10
1977 by Canonical.com Patch Queue Manager
[r=Bjorn] some clean up of helpers.py
11
    >>> from canonical.launchpad.webapp.servers import LaunchpadTestRequest
9678.4.41 by Guilherme Salgado
[r=barry] Change our tests that were using TestRequest to use LaunchpadTestRequest.
12
    >>> request = LaunchpadTestRequest(environ={
13
    ...     'HTTP_X_FORWARDED_FOR': '127.0.0.1,82.211.81.179',
1681.1.61 by Stuart Bishop
Fix request_country.txt test
14
    ...     'REMOTE_ADDR': '127.0.0.1',
15
    ...     })
16
1977 by Canonical.com Patch Queue Manager
[r=Bjorn] some clean up of helpers.py
17
Here's us converting it to a country.
18
19
    >>> from lp.services.worlddata.interfaces.country import ICountry
10409.5.2 by Curtis Hovey
Removed glob imports from translations.
20
    >>> country = ICountry(request)
1977 by Canonical.com Patch Queue Manager
[r=Bjorn] some clean up of helpers.py
21
    >>> country.name
22
    u'United Kingdom'
1681.1.61 by Stuart Bishop
Fix request_country.txt test
23
1977 by Canonical.com Patch Queue Manager
[r=Bjorn] some clean up of helpers.py
24
Sometimes we don't know where the request came from.
25
26
    >>> request = LaunchpadTestRequest(
9678.4.41 by Guilherme Salgado
[r=barry] Change our tests that were using TestRequest to use LaunchpadTestRequest.
27
    ...     environ={'REMOTE_ADDR': '255.255.255.255'})
28
    >>> ICountry(request)
1977 by Canonical.com Patch Queue Manager
[r=Bjorn] some clean up of helpers.py
29
    Traceback (most recent call last):
30
    ...
31
    TypeError: ('Could not adapt', ...
32