tests/test_http.py
author Tero Marttila <terom@fixme.fi>
Mon, 16 Feb 2009 19:08:17 +0200
changeset 78 a46d2fc07951
parent 75 f94c06cfcc0e
permissions -rw-r--r--
add test for tree_parse filesystem stuff
74
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
     1
# :set encoding=utf8
73
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
75
f94c06cfcc0e separate tests/ package, although the import paths are still wonky
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
     4
    Unit tests for qmsk.web.http
73
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
"""
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
import unittest
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
from cStringIO import StringIO
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
import http
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
class TestHttpUtil (unittest.TestCase) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
    def test_request_url (self) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
        for env, url in (
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
            ({
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
                'wsgi.url_scheme':  "https",
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
                'HTTP_HOST':        "testhost",
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
                'REQUEST_URI':      "/test"
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
            }, "https://testhost/test"),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
            ({
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
                'HTTP_HOST':        "-"
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
            }, "[???]://-[???]")
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
        ) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
            self.assertEqual(http.request_url(env), url)
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
class TestHttpRequest (unittest.TestCase) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
    def build_request (self, env_dict={}, **env_kw) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
        env = {
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
            'wsgi.url_scheme':      "http",
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
            'HTTP_HOST':            "testhost",
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
            'REQUEST_METHOD':       "GET",
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
        }
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
        env.update(env_dict)
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
        env.update(env_kw)
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
        return http.Request(env)
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
    
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
    def test_site_host (self) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
        self.assertEqual(self.build_request(HTTP_HOST='testhost').site_host, "testhost")
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
        self.assertEqual(self.build_request(HTTP_HOST='testhost:13').site_host, "testhost:13")
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
    
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
    def test_site_root (self) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
        for script_name, site_root in (
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
            ('',            ""         ),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
            ('/foo',        ""         ),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
            ('/foo/bar/',   "/foo/bar"  ),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
        ) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
            self.assertEqual(self.build_request(SCRIPT_NAME=script_name).site_root, site_root)
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
    
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
    def test_get_page_name (self) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
        for path_info, page_name in (
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
            ('',                ""),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
            ('/',               ""),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
            ('/foo1/bar',       "foo1/bar"),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
            ('/foo2/bar/',      "foo2/bar/"),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
            ('/foo3/bar/../',   "foo3/"),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
            ('/foo4/bar/..',    "foo4"),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
            ('//',              ""),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
        ) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
            self.assertEqual(self.build_request(PATH_INFO=path_info).get_page_name(), page_name)
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
    
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
    def _test_page_prefix (self, request_uri, path_info, page_prefix) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
        self.assertEqual(self.build_request(REQUEST_URI=request_uri, PATH_INFO=path_info).page_prefix, page_prefix)
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
    def test_page_prefix_empty (self) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
        self._test_page_prefix('', '', "")
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
    def test_page_prefix_root_dir (self) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
        self._test_page_prefix('/foo/', '/foo/', "")
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
    
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
    def test_page_prefix_cgi (self) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
        self._test_page_prefix('/foo.cgi/quux', '/quux', "/foo.cgi")
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
    def test_page_prefix_qargs (self) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
        self._test_page_prefix('/foo.cgi/?foo', '/', "/foo.cgi")
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
    
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
    def test_get_arg (self) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
        for query_string, key, value in (
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
            ('',                'foo',      None),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
            ('foo',             'foo',      ''),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
            ('foo=',            'foo',      ''),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
            ('foo=bar',         'foo',      'bar'),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
            ('foo=&bar=.',      'bar',      '.'),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
        ) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
            self.assertEqual(self.build_request(QUERY_STRING=query_string).get_arg(key, None), value)
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
    
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
    def test_get_args (self) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
        for query_string, keyvals in (
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
            ('',                []),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
            ('foo1=&bar',       [('foo1', ''), ('bar', '')]),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
            ('foo2=bar',        [('foo2', 'bar')]),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
            ('foo3=bar&foo3',   [('foo3', 'bar'), ('foo3', '')]),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
        ) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
            self.assertEqual(self.build_request(QUERY_STRING=query_string).get_args(), keyvals)
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
    
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
    def _build_post (self, method='POST', **vals) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
        data = '&'.join('%s=%s' % kv for kv in vals.iteritems())
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
        return self.build_request({'wsgi.input': StringIO(data)}, REQUEST_METHOD=method, CONTENT_TYPE='application/x-www-form-urlencoded')
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
    def test_is_post (self) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
        for request_method, bool in (
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   103
            ('GET',             False),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
            ('POST',            True),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
            ('post',            True),
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
        ) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
            self.assertEqual(self._build_post(method=request_method).is_post(), bool)
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
        
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
    def _test_post (self, **vals) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
        req = self._build_post(**vals)
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
    
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
    def test_post_empty (self) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
        req = self._build_post()
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   115
        self.assertTrue(req.is_post())
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
        self.assertEqual(req.get_post('foo', None), None)
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
    
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   118
    def test_post_simple (self) :
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   119
        req = self._build_post(foo='bar')
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   120
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   121
        self.assertTrue(req.is_post())
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   122
        self.assertEqual(req.get_post('foo', None), 'bar')
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   123
74
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   124
class TestHttpResponse (unittest.TestCase) :
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   125
    def test_status (self) :
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   126
        response = http.Response(None, status='100 Continue')
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   127
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   128
        self.assertEqual(response.get_status(), '100 Continue')
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   129
    
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   130
    def test_data_empty (self) :
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   131
        self.assertEqual(http.Response(None).get_data(), '')
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   132
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   133
    def test_data_str (self) :
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   134
        self.assertEqual(http.Response('foo').get_data(), 'foo')
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   135
    
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   136
    def test_data_utf8 (self) :
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   137
        data = u'föö'
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   138
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   139
        self.assertEqual(http.Response(data).get_data(), data.encode('utf8'))
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   140
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   141
    def test_data_utf16 (self) :
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   142
        data = u'fåå'
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   143
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   144
        self.assertEqual(http.Response(data, charset='utf16').get_data(), data.encode('utf16'))
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   145
    
75
f94c06cfcc0e separate tests/ package, although the import paths are still wonky
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   146
    def test_data_invalid (self) :
f94c06cfcc0e separate tests/ package, although the import paths are still wonky
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   147
        data = 'fää'
f94c06cfcc0e separate tests/ package, although the import paths are still wonky
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   148
f94c06cfcc0e separate tests/ package, although the import paths are still wonky
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   149
        self.assertRaises(UnicodeDecodeError, http.Response(data).get_data)
f94c06cfcc0e separate tests/ package, although the import paths are still wonky
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   150
74
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   151
    def test_data_binary (self) :
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   152
        data = '\x01\x00'
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   153
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   154
        self.assertEqual(http.Response(data, charset=None).get_data(), data)
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   155
    
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   156
    def test_content_type_none (self) :
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   157
        response = http.Response(None, content_type=None)
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   158
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   159
        self.assertEqual(response.get_headers(), [])
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   160
    
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   161
    def test_content_type_default (self) :
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   162
        self.assertEqual(http.Response(None).get_headers(), [('Content-type', "text/html; charset=\"UTF-8\"")])
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   163
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   164
    def test_content_type_weird (self) :
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   165
        self.assertEqual(http.Response(None, content_type="foo/x-bar").get_headers(), [('Content-type', "foo/x-bar; charset=\"UTF-8\"")])
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   166
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   167
    def test_content_type_charset (self) :
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   168
        self.assertEqual(http.Response(None, content_type="foo/x-bar", charset="---").get_headers(), [('Content-type', "foo/x-bar; charset=\"---\"")])
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   169
    
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   170
    def _build_headers (self, tlist=None, **map) :
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   171
        response = http.Response(None)
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   172
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   173
        if tlist :
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   174
            for name, value, params in tlist :
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   175
                response.add_header(name, value, **params)
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   176
        
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   177
        for name, value in map.iteritems() :
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   178
            response.add_header(name, value)
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   179
       
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   180
        return response
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   181
        
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   182
    def test_headers_simple (self) :
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   183
        self.assertEqual(self._build_headers(**{'X-Foo': 'bar, never'}).get_headers(), [
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   184
            ('Content-type', "text/html; charset=\"UTF-8\""),
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   185
            ('X-Foo', "bar, never")
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   186
        ])
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   187
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   188
    def test_headers_params (self) :
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   189
        self.assertEqual(self._build_headers([('X-Bar', 'bar', dict(never='yes'))]).get_headers(), [
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   190
            ('Content-type', "text/html; charset=\"UTF-8\""),
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   191
            ('X-Bar', "bar; never=\"yes\"")
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   192
        ])
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   193
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   194
    def test_headers_multi (self) :
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   195
        self.assertEqual(self._build_headers([('X-Bar', 'bar', {}), ('X-bar', 'foo', {})]).get_headers(), [
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   196
            ('Content-type', "text/html; charset=\"UTF-8\""),
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   197
            ('X-Bar', "bar"),
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   198
            ('X-bar', "foo"),
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   199
        ])
406cf77d23f9 http.Response tests
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   200
75
f94c06cfcc0e separate tests/ package, although the import paths are still wonky
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   201
class TestHttpRedirect (unittest.TestCase) :
f94c06cfcc0e separate tests/ package, although the import paths are still wonky
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   202
    def test_redirect (self) :
f94c06cfcc0e separate tests/ package, although the import paths are still wonky
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   203
        response = http.Redirect('http://test/bar')
73
1554d3d083b8 add a http.request_url utility function, start writing some unit tests (now for http.Request), fix some issues
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   204
75
f94c06cfcc0e separate tests/ package, although the import paths are still wonky
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   205
        self.assertEquals(response.get_status(), '302 Found')
f94c06cfcc0e separate tests/ package, although the import paths are still wonky
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   206
        self.assertEquals(response.get_headers(), [('Location', 'http://test/bar')])
f94c06cfcc0e separate tests/ package, although the import paths are still wonky
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   207
        self.assertEquals(response.get_data(), '')
f94c06cfcc0e separate tests/ package, although the import paths are still wonky
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   208
f94c06cfcc0e separate tests/ package, although the import paths are still wonky
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   209