File: test_http.py

package info (click to toggle)
python-w3lib 2.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 396 kB
  • sloc: python: 3,141; makefile: 133
file content (90 lines) | stat: -rw-r--r-- 3,163 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import unittest
from collections import OrderedDict

from w3lib.http import (
    HeadersDictInput,
    basic_auth_header,
    headers_dict_to_raw,
    headers_raw_to_dict,
)

__doctests__ = ["w3lib.http"]  # for trial support


class HttpTests(unittest.TestCase):
    def test_basic_auth_header(self):
        self.assertEqual(
            b"Basic c29tZXVzZXI6c29tZXBhc3M=", basic_auth_header("someuser", "somepass")
        )
        # Check url unsafe encoded header
        self.assertEqual(
            b"Basic c29tZXVzZXI6QDx5dTk+Jm8/UQ==",
            basic_auth_header("someuser", "@<yu9>&o?Q"),
        )

    def test_basic_auth_header_encoding(self):
        self.assertEqual(
            b"Basic c29tw6Z1c8Oocjpzw7htZXDDpHNz",
            basic_auth_header("somæusèr", "sømepäss", encoding="utf8"),
        )
        # default encoding (ISO-8859-1)
        self.assertEqual(
            b"Basic c29t5nVz6HI6c/htZXDkc3M=", basic_auth_header("somæusèr", "sømepäss")
        )

    def test_headers_raw_dict_none(self):
        self.assertIsNone(headers_raw_to_dict(None))
        self.assertIsNone(headers_dict_to_raw(None))

    def test_headers_raw_to_dict(self):
        raw = b"Content-type: text/html\n\rAccept: gzip\n\r\
                Cache-Control: no-cache\n\rCache-Control: no-store\n\n"
        dct = {
            b"Content-type": [b"text/html"],
            b"Accept": [b"gzip"],
            b"Cache-Control": [b"no-cache", b"no-store"],
        }
        self.assertEqual(headers_raw_to_dict(raw), dct)

    def test_headers_dict_to_raw(self):
        dct = OrderedDict([(b"Content-type", b"text/html"), (b"Accept", b"gzip")])
        self.assertEqual(
            headers_dict_to_raw(dct), b"Content-type: text/html\r\nAccept: gzip"
        )

    def test_headers_dict_to_raw_listtuple(self):
        dct: HeadersDictInput = OrderedDict(
            [(b"Content-type", [b"text/html"]), (b"Accept", [b"gzip"])]
        )
        self.assertEqual(
            headers_dict_to_raw(dct), b"Content-type: text/html\r\nAccept: gzip"
        )

        dct = OrderedDict([(b"Content-type", (b"text/html",)), (b"Accept", (b"gzip",))])
        self.assertEqual(
            headers_dict_to_raw(dct), b"Content-type: text/html\r\nAccept: gzip"
        )

        dct = OrderedDict([(b"Cookie", (b"val001", b"val002")), (b"Accept", b"gzip")])
        self.assertEqual(
            headers_dict_to_raw(dct),
            b"Cookie: val001\r\nCookie: val002\r\nAccept: gzip",
        )

        dct = OrderedDict([(b"Cookie", [b"val001", b"val002"]), (b"Accept", b"gzip")])
        self.assertEqual(
            headers_dict_to_raw(dct),
            b"Cookie: val001\r\nCookie: val002\r\nAccept: gzip",
        )

    def test_headers_dict_to_raw_wrong_values(self):
        dct: HeadersDictInput = OrderedDict(
            [
                (b"Content-type", 0),
            ]
        )
        self.assertEqual(headers_dict_to_raw(dct), b"")
        self.assertEqual(headers_dict_to_raw(dct), b"")

        dct = OrderedDict([(b"Content-type", 1), (b"Accept", [b"gzip"])])
        self.assertEqual(headers_dict_to_raw(dct), b"Accept: gzip")