Exemple #1
0
 def test_httprequest_repr_invalid_method_and_path(self):
     request = HttpRequest()
     self.assertEqual(repr(request), "<HttpRequest>")
     request = HttpRequest()
     request.method = "GET"
     self.assertEqual(repr(request), "<HttpRequest>")
     request = HttpRequest()
     request.path = ""
     self.assertEqual(repr(request), "<HttpRequest>")
Exemple #2
0
    def test_get_port_with_x_forwarded_port(self):
        request = HttpRequest()
        request.META = {
            'SERVER_PORT': '8080',
            'HTTP_X_FORWARDED_PORT': '80',
        }
        # Should use the X-Forwarded-Port header
        self.assertEqual(request.get_port(), '80')

        request = HttpRequest()
        request.META = {
            'SERVER_PORT': '8080',
        }
        self.assertEqual(request.get_port(), '8080')
 def test_request_accepts_some(self):
     request = HttpRequest()
     request.META['HTTP_ACCEPT'] = 'text/html,application/xhtml+xml,application/xml;q=0.9'
     self.assertIs(request.accepts('text/html'), True)
     self.assertIs(request.accepts('application/xhtml+xml'), True)
     self.assertIs(request.accepts('application/xml'), True)
     self.assertIs(request.accepts('application/json'), False)
 def test_no_headers(self):
     """Absence of Accept header defaults to '*/*'."""
     request = HttpRequest()
     self.assertEqual(
         [str(accepted_type) for accepted_type in request.accepted_types],
         ['*/*'],
     )
Exemple #5
0
 def test_host_retrieval(self):
     request = HttpRequest()
     request.get_host = lambda: 'www.example.com'
     request.path = ''
     self.assertEqual(
         request.build_absolute_uri(location='/path/with:colons'),
         'http://www.example.com/path/with:colons')
Exemple #6
0
    def test_host_validation_in_debug_mode(self):
        """
        If ALLOWED_HOSTS is empty and DEBUG is True, variants of localhost are
        allowed.
        """
        valid_hosts = ['localhost', '127.0.0.1', '[::1]']
        for host in valid_hosts:
            request = HttpRequest()
            request.META = {'HTTP_HOST': host}
            self.assertEqual(request.get_host(), host)

        # Other hostnames raise a SuspiciousOperation.
        with self.assertRaises(SuspiciousOperation):
            request = HttpRequest()
            request.META = {'HTTP_HOST': 'example.com'}
            request.get_host()
Exemple #7
0
 def test_httprequest_full_path_with_query_string_and_fragment(self):
     request = HttpRequest()
     request.path = '/foo#bar'
     request.path_info = '/prefix' + request.path
     request.META['QUERY_STRING'] = 'baz#quux'
     self.assertEqual(request.get_full_path(), '/foo%23bar?baz#quux')
     self.assertEqual(request.get_full_path_info(),
                      '/prefix/foo%23bar?baz#quux')
Exemple #8
0
 def test_httprequest_full_path(self):
     request = HttpRequest()
     request.path = '/;some/?awful/=path/foo:bar/'
     request.path_info = '/prefix' + request.path
     request.META['QUERY_STRING'] = ';some=query&+query=string'
     expected = '/%3Bsome/%3Fawful/%3Dpath/foo:bar/?;some=query&+query=string'
     self.assertEqual(request.get_full_path(), expected)
     self.assertEqual(request.get_full_path_info(), '/prefix' + expected)
Exemple #9
0
 def test_httprequest_repr(self):
     request = HttpRequest()
     request.path = '/somepath/'
     request.method = 'GET'
     request.GET = {'get-key': 'get-value'}
     request.POST = {'post-key': 'post-value'}
     request.COOKIES = {'post-key': 'post-value'}
     request.META = {'post-key': 'post-value'}
     self.assertEqual(repr(request), "<HttpRequest: GET '/somepath/'>")
Exemple #10
0
    def test_get_host_suggestion_of_allowed_host(self):
        """get_host() makes helpful suggestions if a valid-looking host is not in ALLOWED_HOSTS."""
        msg_invalid_host = "Invalid HTTP_HOST header: %r."
        msg_suggestion = msg_invalid_host + " You may need to add %r to ALLOWED_HOSTS."
        msg_suggestion2 = msg_invalid_host + " The domain name provided is not valid according to RFC 1034/1035"

        for host in [  # Valid-looking hosts
                'example.com',
                '12.34.56.78',
                '[2001:19f0:feee::dead:beef:cafe]',
                'xn--4ca9at.com',  # Punycode for öäü.com
        ]:
            request = HttpRequest()
            request.META = {'HTTP_HOST': host}
            with self.assertRaisesMessage(DisallowedHost,
                                          msg_suggestion % (host, host)):
                request.get_host()

        for domain, port in [  # Valid-looking hosts with a port number
            ('example.com', 80),
            ('12.34.56.78', 443),
            ('[2001:19f0:feee::dead:beef:cafe]', 8080),
        ]:
            host = '%s:%s' % (domain, port)
            request = HttpRequest()
            request.META = {'HTTP_HOST': host}
            with self.assertRaisesMessage(DisallowedHost,
                                          msg_suggestion % (host, domain)):
                request.get_host()

        for host in self.poisoned_hosts:
            request = HttpRequest()
            request.META = {'HTTP_HOST': host}
            with self.assertRaisesMessage(DisallowedHost,
                                          msg_invalid_host % host):
                request.get_host()

        request = HttpRequest()
        request.META = {'HTTP_HOST': "invalid_hostname.com"}
        with self.assertRaisesMessage(DisallowedHost, msg_suggestion2 %
                                      "invalid_hostname.com"):
            request.get_host()
Exemple #11
0
 def test_base_request_headers(self):
     request = HttpRequest()
     request.META = self.ENVIRON
     self.assertEqual(
         dict(request.headers), {
             'Content-Type': 'text/html',
             'Content-Length': '100',
             'Accept': '*',
             'Host': 'example.com',
             'User-Agent': 'python-requests/1.2.0',
         })
Exemple #12
0
    def test_httprequest_location(self):
        request = HttpRequest()
        self.assertEqual(
            request.build_absolute_uri(
                location="https://www.example.com/asdf"),
            'https://www.example.com/asdf')

        request.get_host = lambda: 'www.example.com'
        request.path = ''
        self.assertEqual(
            request.build_absolute_uri(location="/path/with:colons"),
            'http://www.example.com/path/with:colons')
 def test_accept_headers(self):
     request = HttpRequest()
     request.META['HTTP_ACCEPT'] = (
         'text/html, application/xhtml+xml,application/xml ;q=0.9,*/*;q=0.8'
     )
     self.assertEqual(
         [str(accepted_type) for accepted_type in request.accepted_types],
         [
             'text/html',
             'application/xhtml+xml',
             'application/xml; q=0.9',
             '*/*; q=0.8',
         ],
     )
Exemple #14
0
    def test_httprequest(self):
        request = HttpRequest()
        self.assertEqual(list(request.GET), [])
        self.assertEqual(list(request.POST), [])
        self.assertEqual(list(request.COOKIES), [])
        self.assertEqual(list(request.META), [])

        # .GET and .POST should be QueryDicts
        self.assertEqual(request.GET.urlencode(), '')
        self.assertEqual(request.POST.urlencode(), '')

        # and FILES should be MultiValueDict
        self.assertEqual(request.FILES.getlist('foo'), [])

        self.assertIsNone(request.content_type)
        self.assertIsNone(request.content_params)
 def test_request_accepts_none(self):
     request = HttpRequest()
     request.META['HTTP_ACCEPT'] = ''
     self.assertIs(request.accepts('application/json'), False)
     self.assertEqual(request.accepted_types, [])
 def test_request_accepts_any(self):
     request = HttpRequest()
     request.META['HTTP_ACCEPT'] = '*/*'
     self.assertIs(request.accepts('application/json'), True)
Exemple #17
0
 def test_absolute_url(self):
     request = HttpRequest()
     url = 'https://www.example.com/asdf'
     self.assertEqual(request.build_absolute_uri(location=url), url)
Exemple #18
0
    def test_http_get_host_with_x_forwarded_host(self):
        # Check if X_FORWARDED_HOST is provided.
        request = HttpRequest()
        request.META = {
            'HTTP_X_FORWARDED_HOST': 'forward.com',
            'HTTP_HOST': 'example.com',
            'SERVER_NAME': 'internal.com',
            'SERVER_PORT': 80,
        }
        # X_FORWARDED_HOST is obeyed.
        self.assertEqual(request.get_host(), 'forward.com')

        # Check if X_FORWARDED_HOST isn't provided.
        request = HttpRequest()
        request.META = {
            'HTTP_HOST': 'example.com',
            'SERVER_NAME': 'internal.com',
            'SERVER_PORT': 80,
        }
        self.assertEqual(request.get_host(), 'example.com')

        # Check if HTTP_HOST isn't provided.
        request = HttpRequest()
        request.META = {
            'SERVER_NAME': 'internal.com',
            'SERVER_PORT': 80,
        }
        self.assertEqual(request.get_host(), 'internal.com')

        # Check if HTTP_HOST isn't provided, and we're on a nonstandard port
        request = HttpRequest()
        request.META = {
            'SERVER_NAME': 'internal.com',
            'SERVER_PORT': 8042,
        }
        self.assertEqual(request.get_host(), 'internal.com:8042')

        # Poisoned host headers are rejected as suspicious
        legit_hosts = [
            'example.com',
            'example.com:80',
            '12.34.56.78',
            '12.34.56.78:443',
            '[2001:19f0:feee::dead:beef:cafe]',
            '[2001:19f0:feee::dead:beef:cafe]:8080',
            'xn--4ca9at.com',  # Punycode for öäü.com
        ]

        for host in legit_hosts:
            request = HttpRequest()
            request.META = {
                'HTTP_HOST': host,
            }
            request.get_host()

        for host in self.poisoned_hosts:
            with self.assertRaises(DisallowedHost):
                request = HttpRequest()
                request.META = {
                    'HTTP_HOST': host,
                }
                request.get_host()
Exemple #19
0
    def test_http_get_host(self):
        # Check if X_FORWARDED_HOST is provided.
        request = HttpRequest()
        request.META = {
            'HTTP_X_FORWARDED_HOST': 'forward.com',
            'HTTP_HOST': 'example.com',
            'SERVER_NAME': 'internal.com',
            'SERVER_PORT': 80,
        }
        # X_FORWARDED_HOST is ignored.
        self.assertEqual(request.get_host(), 'example.com')

        # Check if X_FORWARDED_HOST isn't provided.
        request = HttpRequest()
        request.META = {
            'HTTP_HOST': 'example.com',
            'SERVER_NAME': 'internal.com',
            'SERVER_PORT': 80,
        }
        self.assertEqual(request.get_host(), 'example.com')

        # Check if HTTP_HOST isn't provided.
        request = HttpRequest()
        request.META = {
            'SERVER_NAME': 'internal.com',
            'SERVER_PORT': 80,
        }
        self.assertEqual(request.get_host(), 'internal.com')

        # Check if HTTP_HOST isn't provided, and we're on a nonstandard port
        request = HttpRequest()
        request.META = {
            'SERVER_NAME': 'internal.com',
            'SERVER_PORT': 8042,
        }
        self.assertEqual(request.get_host(), 'internal.com:8042')

        legit_hosts = [
            'example.com',
            'example.com:80',
            '12.34.56.78',
            '12.34.56.78:443',
            '[2001:19f0:feee::dead:beef:cafe]',
            '[2001:19f0:feee::dead:beef:cafe]:8080',
            'xn--4ca9at.com',  # Punycode for öäü.com
            'anything.multitenant.com',
            'multitenant.com',
            'insensitive.com',
            'example.com.',
            'example.com.:80',
            '[::ffff:169.254.169.254]',
        ]

        for host in legit_hosts:
            request = HttpRequest()
            request.META = {
                'HTTP_HOST': host,
            }
            request.get_host()

        # Poisoned host headers are rejected as suspicious
        for host in chain(self.poisoned_hosts, ['other.com', 'example.com..']):
            with self.assertRaises(DisallowedHost):
                request = HttpRequest()
                request.META = {
                    'HTTP_HOST': host,
                }
                request.get_host()