File: test_request.py

package info (click to toggle)
python-overpy 0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 556 kB
  • sloc: python: 2,235; xml: 1,055; makefile: 150
file content (246 lines) | stat: -rw-r--r-- 7,314 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
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
237
238
239
240
241
242
243
244
245
246
from http.server import BaseHTTPRequestHandler

import pytest

import overpy

from tests import read_file, new_server_thread, stop_server_thread


def handle_bad_request(request):
    request.send_response(400, "Bad Request")
    request.send_header("Content-Type", "text/html; charset=utf-8")
    request.end_headers()
    request.wfile.write(read_file("response/bad-request.html", "rb"))


def handle_bad_request_encoding(request):
    request.send_response(400, "Bad Request")
    request.send_header("Content-Type", "text/html; charset=utf-8")
    request.end_headers()
    request.wfile.write(read_file("response/bad-request-encoding.html", "rb"))


def handle_too_many_requests(request):
    request.send_response(429, "Too Many Requests")
    request.send_header("Content-Type", "text/html; charset=utf-8")
    request.end_headers()
    request.wfile.write(b"Too Many Requests")


def handle_gateway_timeout(request):
    request.send_response(504, "Gateway Timeout")
    request.send_header("Content-Type", "text/html; charset=utf-8")
    request.end_headers()
    request.wfile.write(b"Gateway Timeout")


def handle_unknown_content_type(request):
    request.send_response(200, "OK")
    request.send_header("Content-Type", "application/foobar")
    request.end_headers()
    request.wfile.write(read_file("xml/way-02.xml", "rb"))


def handle_unknown_http_status_code(request):
    request.send_response(123, "Unknown")
    request.send_header("Content-Type", "text/html; charset=utf-8")
    request.end_headers()
    request.wfile.write(b"Unknown status code")


class HandleOverpassBadRequest(BaseHTTPRequestHandler):
    """
    Simulate the response if the query string has syntax errors
    """
    def do_POST(self):
        handle_bad_request(self)


class HandleOverpassBadRequestEncoding(BaseHTTPRequestHandler):
    """
    """
    def do_POST(self):
        handle_bad_request_encoding(self)


class HandleOverpassTooManyRequests(BaseHTTPRequestHandler):
    """
    """
    def do_POST(self):
        handle_too_many_requests(self)


class HandleOverpassGatewayTimeout(BaseHTTPRequestHandler):
    """
    """
    def do_POST(self):
        handle_gateway_timeout(self)


class HandleOverpassUnknownHTTPStatusCode(BaseHTTPRequestHandler):
    """
    """
    def do_POST(self):
        handle_unknown_http_status_code(self)


class HandleResponseJSON(BaseHTTPRequestHandler):
    """
    """
    def do_POST(self):
        self.send_response(200, "OK")
        self.send_header("Content-Type", "application/json")
        self.end_headers()
        self.wfile.write(read_file("json/way-02.json", "rb"))


class HandleResponseXML(BaseHTTPRequestHandler):
    """
    """
    def do_POST(self):
        self.send_response(200, "OK")
        self.send_header("Content-Type", "application/osm3s+xml")
        self.end_headers()
        self.wfile.write(read_file("xml/way-02.xml", "rb"))


class HandleResponseUnknown(BaseHTTPRequestHandler):
    """
    """
    def do_POST(self):
        handle_unknown_content_type(self)


class HandleRetry(BaseHTTPRequestHandler):
    default_handler_func = [
        handle_bad_request,
        handle_bad_request_encoding,
        handle_too_many_requests,
        handle_gateway_timeout,
        handle_unknown_content_type,
        handle_unknown_http_status_code
    ]

    def do_POST(self):
        f = self.default_handler_func.pop(0)
        f(self)


class TestQuery:
    def test_chunk_size(self):
        url, server = new_server_thread(HandleResponseJSON)

        api = overpy.Overpass(read_chunk_size=128)
        api.url = url
        result = api.query("[out:json];node(50.745,7.17,50.75,7.18);out;")
        stop_server_thread(server)
        assert len(result.nodes) > 0

    def test_overpass_syntax_error(self):
        url, server = new_server_thread(HandleOverpassBadRequest)

        api = overpy.Overpass()
        api.url = url
        with pytest.raises(overpy.exception.OverpassBadRequest):
            # Missing ; after way(1)
            api.query(
                "way(1)"
                "out body;"
            )
        stop_server_thread(server)

    def test_overpass_syntax_error_encoding_error(self):
        with pytest.raises(UnicodeDecodeError):
            # File should be encoded with iso8859-15 and will raise an exception
            tmp = read_file("response/bad-request-encoding.html", "rb")
            tmp.decode("utf-8")

        url, server = new_server_thread(HandleOverpassBadRequestEncoding)

        api = overpy.Overpass()
        api.url = url
        with pytest.raises(overpy.exception.OverpassBadRequest):
            # Missing ; after way(1)
            api.query(
                "way(1)"
                "out body;"
            )
        stop_server_thread(server)

    def test_overpass_too_many_requests(self):
        url, server = new_server_thread(HandleOverpassTooManyRequests)

        api = overpy.Overpass()
        api.url = url
        with pytest.raises(overpy.exception.OverpassTooManyRequests):
            api.query(
                "way(1);"
                "out body;"
            )
        stop_server_thread(server)

    def test_overpass_gateway_timeout(self):
        url, server = new_server_thread(HandleOverpassGatewayTimeout)

        api = overpy.Overpass()
        api.url = url
        with pytest.raises(overpy.exception.OverpassGatewayTimeout):
            api.query(
                "way(1);"
                "out body;"
            )
        stop_server_thread(server)

    def test_overpass_unknown_status_code(self):
        url, server = new_server_thread(HandleOverpassUnknownHTTPStatusCode)

        api = overpy.Overpass()
        api.url = url
        with pytest.raises(overpy.exception.OverpassUnknownHTTPStatusCode):
            api.query(
                "way(1);"
                "out body;"
            )
        stop_server_thread(server)

    def test_response_json(self):
        url, server = new_server_thread(HandleResponseJSON)

        api = overpy.Overpass()
        api.url = url
        result = api.query("[out:json];node(50.745,7.17,50.75,7.18);out;")
        stop_server_thread(server)
        assert len(result.nodes) > 0

    def test_response_unknown(self):
        url, server = new_server_thread(HandleResponseUnknown)

        api = overpy.Overpass()
        api.url = url
        with pytest.raises(overpy.exception.OverpassUnknownContentType):
            api.query("[out:xml];node(50.745,7.17,50.75,7.18);out;")
        stop_server_thread(server)

    def test_response_xml(self):
        url, server = new_server_thread(HandleResponseXML)

        api = overpy.Overpass()
        api.url = url
        result = api.query("[out:xml];node(50.745,7.17,50.75,7.18);out;")
        stop_server_thread(server)
        assert len(result.nodes) > 0

    def test_retry(self):
        url, server = new_server_thread(HandleRetry)

        api = overpy.Overpass()
        # HandleRetry.default_handler_cls should contain at least 2 classes to process
        api.max_retry_count = len(HandleRetry.default_handler_func) - 1
        api.url = url
        with pytest.raises(overpy.exception.MaxRetriesReached):
            api.query(
                "way(1);"
                "out body;"
            )
        stop_server_thread(server)