File: test_matching.py

package info (click to toggle)
httpie 3.2.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,904 kB
  • sloc: python: 13,760; xml: 162; makefile: 141; ruby: 79; sh: 32
file content (214 lines) | stat: -rw-r--r-- 5,827 bytes parent folder | download | duplicates (3)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""
Here we test our output parsing and matching implementation, not HTTPie itself.

"""
from httpie.models import ELAPSED_TIME_LABEL
from httpie.output.writer import MESSAGE_SEPARATOR
from ...utils import CRLF
from . import assert_output_does_not_match, assert_output_matches, Expect


def test_assert_output_matches_headers_incomplete():
    assert_output_does_not_match(f'HTTP/1.1{CRLF}', [Expect.RESPONSE_HEADERS])


def test_assert_output_matches_headers_unterminated():
    assert_output_does_not_match(
        (
            f'HTTP/1.1{CRLF}'
            'AAA:BBB'
            f'{CRLF}'
        ),
        [Expect.RESPONSE_HEADERS],
    )


def test_assert_output_matches_response_headers():
    assert_output_matches(
        (
            f'HTTP/1.1 200 OK{CRLF}'
            f'AAA:BBB{CRLF}'
            f'{CRLF}'
        ),
        [Expect.RESPONSE_HEADERS],
    )


def test_assert_output_matches_request_headers():
    assert_output_matches(
        (
            f'GET / HTTP/1.1{CRLF}'
            f'AAA:BBB{CRLF}'
            f'{CRLF}'
        ),
        [Expect.REQUEST_HEADERS],
    )


def test_assert_output_matches_headers_and_separator():
    assert_output_matches(
        (
            f'HTTP/1.1{CRLF}'
            f'AAA:BBB{CRLF}'
            f'{CRLF}'
            f'{MESSAGE_SEPARATOR}'
        ),
        [Expect.RESPONSE_HEADERS, Expect.SEPARATOR],
    )


def test_assert_output_matches_body_unmatched_crlf():
    assert_output_does_not_match(f'AAA{CRLF}', [Expect.BODY])


def test_assert_output_matches_body_unmatched_separator():
    assert_output_does_not_match(f'AAA{MESSAGE_SEPARATOR}', [Expect.BODY])


def test_assert_output_matches_body_and_separator():
    assert_output_matches(f'AAA{MESSAGE_SEPARATOR}', [Expect.BODY, Expect.SEPARATOR])


def test_assert_output_matches_body_r():
    assert_output_matches('AAA\r', [Expect.BODY])


def test_assert_output_matches_body_n():
    assert_output_matches('AAA\n', [Expect.BODY])


def test_assert_output_matches_body_r_body():
    assert_output_matches('AAA\rBBB', [Expect.BODY])


def test_assert_output_matches_body_n_body():
    assert_output_matches('AAA\nBBB', [Expect.BODY])


def test_assert_output_matches_headers_and_body():
    assert_output_matches(
        (
            f'HTTP/1.1{CRLF}'
            f'AAA:BBB{CRLF}'
            f'{CRLF}'
            'CCC'
        ),
        [Expect.RESPONSE_HEADERS, Expect.BODY]
    )


def test_assert_output_matches_headers_with_body_and_separator():
    assert_output_matches(
        (
            f'HTTP/1.1 {CRLF}'
            f'AAA:BBB{CRLF}{CRLF}'
            f'CCC{MESSAGE_SEPARATOR}'
        ),
        [Expect.RESPONSE_HEADERS, Expect.BODY, Expect.SEPARATOR]
    )


def test_assert_output_matches_response_meta():
    assert_output_matches(
        (
            'Key: Value\n'
            f'{ELAPSED_TIME_LABEL}: 3.3s'
        ),
        [Expect.RESPONSE_META]
    )


def test_assert_output_matches_whole_response():
    assert_output_matches(
        (
            f'HTTP/1.1{CRLF}'
            f'AAA:BBB{CRLF}'
            f'{CRLF}'
            f'CCC{MESSAGE_SEPARATOR}'
            f'{ELAPSED_TIME_LABEL}: 3.3s'
        ),
        [Expect.RESPONSE_HEADERS, Expect.BODY, Expect.RESPONSE_META]
    )


def test_assert_output_matches_multiple_messages():
    assert_output_matches(
        (
            f'POST / HTTP/1.1{CRLF}'
            f'AAA:BBB{CRLF}'
            f'{CRLF}'

            f'CCC'
            f'{MESSAGE_SEPARATOR}'

            f'HTTP/1.1 200 OK{CRLF}'
            f'EEE:FFF{CRLF}'
            f'{CRLF}'

            'GGG'
            f'{MESSAGE_SEPARATOR}'
        ), [
            Expect.REQUEST_HEADERS,
            Expect.BODY,
            Expect.SEPARATOR,
            Expect.RESPONSE_HEADERS,
            Expect.BODY,
            Expect.SEPARATOR,
        ]
    )


def test_assert_output_matches_multipart_body():
    output = (
        'POST / HTTP/1.1\r\n'
        'User-Agent: HTTPie/2.4.0-dev\r\n'
        'Accept-Encoding: gzip, deflate\r\n'
        'Accept: */*\r\n'
        'Connection: keep-alive\r\n'
        'Content-Type: multipart/form-data; boundary=1e22169de43e4a2e8d9e41c0a1c93cc5\r\n'
        'Content-Length: 212\r\n'
        'Host: pie.dev\r\n'
        '\r\n'
        '--1e22169de43e4a2e8d9e41c0a1c93cc5\r\n'
        'Content-Disposition: form-data; name="AAA"\r\n'
        '\r\n'
        'BBB\r\n'
        '--1e22169de43e4a2e8d9e41c0a1c93cc5\r\n'
        'Content-Disposition: form-data; name="CCC"\r\n'
        '\r\n'
        'DDD\r\n'
        '--1e22169de43e4a2e8d9e41c0a1c93cc5--\r\n'
    )
    assert_output_matches(output, [Expect.REQUEST_HEADERS, Expect.BODY])


def test_assert_output_matches_multipart_body_with_separator():
    output = (
        'POST / HTTP/1.1\r\n'
        'User-Agent: HTTPie/2.4.0-dev\r\n'
        'Accept-Encoding: gzip, deflate\r\n'
        'Accept: */*\r\n'
        'Connection: keep-alive\r\n'
        'Content-Type: multipart/form-data; boundary=1e22169de43e4a2e8d9e41c0a1c93cc5\r\n'
        'Content-Length: 212\r\n'
        'Host: pie.dev\r\n'
        '\r\n'
        '--1e22169de43e4a2e8d9e41c0a1c93cc5\r\n'
        'Content-Disposition: form-data; name="AAA"\r\n'
        '\r\n'
        'BBB\r\n'
        '--1e22169de43e4a2e8d9e41c0a1c93cc5\r\n'
        'Content-Disposition: form-data; name="CCC"\r\n'
        '\r\n'
        'DDD\r\n'
        '--1e22169de43e4a2e8d9e41c0a1c93cc5--\r\n'
        f'{MESSAGE_SEPARATOR}'
    )
    assert_output_matches(output, [Expect.REQUEST_HEADERS, Expect.BODY, Expect.SEPARATOR])


def test_assert_output_matches_multiple_separators():
    assert_output_matches(
        MESSAGE_SEPARATOR + MESSAGE_SEPARATOR + 'AAA' + MESSAGE_SEPARATOR + MESSAGE_SEPARATOR,
        [Expect.SEPARATOR, Expect.SEPARATOR, Expect.BODY, Expect.SEPARATOR, Expect.SEPARATOR]
    )