aboutsummaryrefslogtreecommitdiffstats
path: root/test/pathod/test_language_http2.py
blob: 4f89adb803222e94b50cae19a76b98a215821b15 (plain)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import io
import pytest

from mitmproxy.net import tcp
from mitmproxy.net.http import user_agents

from pathod import language
from pathod.language import http2
from pathod.protocols.http2 import HTTP2StateProtocol


def parse_request(s):
    return next(language.parse_pathoc(s, True))


def parse_response(s):
    return next(language.parse_pathod(s, True))


def default_settings():
    return language.Settings(
        request_host="foo.com",
        protocol=HTTP2StateProtocol(tcp.TCPClient(('localhost', 1234)))
    )


def test_make_error_response():
    d = io.BytesIO()
    s = http2.make_error_response("foo", "bar")
    language.serve(s, d, default_settings())


class TestRequest:

    def test_cached_values(self):
        req = parse_request("get:/")
        req_id = id(req)
        assert req_id == id(req.resolve(default_settings()))
        assert req.values(default_settings()) == req.values(default_settings())

    def test_nonascii(self):
        with pytest.raises(Exception, match="ASCII"):
            parse_request("get:\xf0")

    def test_err(self):
        with pytest.raises(language.ParseException):
            parse_request('GET')

    def test_simple(self):
        r = parse_request('GET:"/foo"')
        assert r.method.string() == b"GET"
        assert r.path.string() == b"/foo"
        r = parse_request('GET:/foo')
        assert r.path.string() == b"/foo"

    def test_multiple(self):
        r = list(language.parse_pathoc("GET:/ PUT:/"))
        assert r[0].method.string() == b"GET"
        assert r[1].method.string() == b"PUT"
        assert len(r) == 2

        l = """
            GET
            "/foo"

            PUT

            "/foo



            bar"
        """
        r = list(language.parse_pathoc(l, True))
        assert len(r) == 2
        assert r[0].method.string() == b"GET"
        assert r[1].method.string() == b"PUT"

        l = """
            get:"http://localhost:9999/p/200"
            get:"http://localhost:9999/p/200"
        """
        r = list(language.parse_pathoc(l, True))
        assert len(r) == 2
        assert r[0].method.string() == b"GET"
        assert r[1].method.string() == b"GET"

    def test_render_simple(self):
        s = io.BytesIO()
        r = parse_request("GET:'/foo'")
        assert language.serve(
            r,
            s,
            default_settings(),
        )

    def test_raw_content_length(self):
        r = parse_request('GET:/:r')
        assert len(r.headers) == 0

        r = parse_request('GET:/:r:b"foobar"')
        assert len(r.headers) == 0

        r = parse_request('GET:/')
        assert len(r.headers) == 1
        assert r.headers[0].values(default_settings()) == (b"content-length", b"0")

        r = parse_request('GET:/:b"foobar"')
        assert len(r.headers) == 1
        assert r.headers[0].values(default_settings()) == (b"content-length", b"6")

        r = parse_request('GET:/:b"foobar":h"content-length"="42"')
        assert len(r.headers) == 1
        assert r.headers[0].values(default_settings()) == (b"content-length", b"42")

        r = parse_request('GET:/:r:b"foobar":h"content-length"="42"')
        assert len(r.headers) == 1
        assert r.headers[0].values(default_settings()) == (b"content-length", b"42")

    def test_content_type(self):
        r = parse_request('GET:/:r:c"foobar"')
        assert len(r.headers) == 1
        assert r.headers[0].values(default_settings()) == (b"content-type", b"foobar")

    def test_user_agent(self):
        r = parse_request('GET:/:r:ua')
        assert len(r.headers) == 1
        assert r.headers[0].values(default_settings()) == (b"user-agent", user_agents.get_by_shortcut('a')[2].encode())

    def test_render_with_headers(self):
        s = io.BytesIO()
        r = parse_request('GET:/foo:h"foo"="bar"')
        assert language.serve(
            r,
            s,
            default_settings(),
        )

    def test_nested_response(self):
        l = "get:/p/:s'200'"
        r = parse_request(l)
        assert len(r.tokens) == 3
        assert isinstance(r.tokens[2], http2.NestedResponse)
        assert r.values(default_settings())

    def test_render_with_body(self):
        s = io.BytesIO()
        r = parse_request("GET:'/foo':bfoobar")
        assert language.serve(
            r,
            s,
            default_settings(),
        )

    def test_spec(self):
        def rt(s):
            s = parse_request(s).spec()
            assert parse_request(s).spec() == s
        rt("get:/foo")


class TestResponse:

    def test_cached_values(self):
        res = parse_response("200")
        res_id = id(res)
        assert res_id == id(res.resolve(default_settings()))
        assert res.values(default_settings()) == res.values(default_settings())

    def test_nonascii(self):
        with pytest.raises(Exception, match="ASCII"):
            parse_response("200:\xf0")

    def test_err(self):
        with pytest.raises(language.ParseException):
            parse_response('GET:/')

    def test_raw_content_length(self):
        r = parse_response('200:r')
        assert len(r.headers) == 0

        r = parse_response('200')
        assert len(r.headers) == 1
        assert r.headers[0].values(default_settings()) == (b"content-length", b"0")

    def test_content_type(self):
        r = parse_response('200:r:c"foobar"')
        assert len(r.headers) == 1
        assert r.headers[0].values(default_settings()) == (b"content-type", b"foobar")

    def test_simple(self):
        r = parse_response('200:r:h"foo"="bar"')
        assert r.status_code.string() == b"200"
        assert len(r.headers) == 1
        assert r.headers[0].values(default_settings()) == (b"foo", b"bar")
        assert r.body is None

        r = parse_response('200:r:h"foo"="bar":bfoobar:h"bla"="fasel"')
        assert r.status_code.string() == b"200"
        assert len(r.headers) == 2
        assert r.headers[0].values(default_settings()) == (b"foo", b"bar")
        assert r.headers[1].values(default_settings()) == (b"bla", b"fasel")
        assert r.body.string() == b"foobar"

    def test_render_simple(self):
        s = io.BytesIO()
        r = parse_response('200')
        assert language.serve(
            r,
            s,
            default_settings(),
        )

    def test_render_with_headers(self):
        s = io.BytesIO()
        r = parse_response('200:h"foo"="bar"')
        assert language.serve(
            r,
            s,
            default_settings(),
        )

    def test_render_with_body(self):
        s = io.BytesIO()
        r = parse_response('200:bfoobar')
        assert language.serve(
            r,
            s,
            default_settings(),
        )

    def test_spec(self):
        def rt(s):
            s = parse_response(s).spec()
            assert parse_response(s).spec() == s
        rt("200:bfoobar")