forked from csernazs/pytest-httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_urimatch.py
More file actions
54 lines (33 loc) · 1.79 KB
/
test_urimatch.py
File metadata and controls
54 lines (33 loc) · 1.79 KB
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
import re
from pytest_httpserver import HTTPServer, URIPattern
import requests
class PrefixMatch(URIPattern):
def __init__(self, prefix: str):
self.prefix = prefix
def match(self, uri):
return uri.startswith(self.prefix)
class PrefixMatchEq:
def __init__(self, prefix: str):
self.prefix = prefix
def __eq__(self, uri):
return uri.startswith(self.prefix)
def test_uripattern_object(httpserver: HTTPServer):
httpserver.expect_request(PrefixMatch("/foo")).respond_with_json({"foo": "bar"})
assert requests.get(httpserver.url_for("/foo")).json() == {"foo": "bar"}
assert requests.get(httpserver.url_for("/foobar")).json() == {"foo": "bar"}
assert requests.get(httpserver.url_for("/foobaz")).json() == {"foo": "bar"}
assert requests.get(httpserver.url_for("/barfoo")).status_code == 500
assert len(httpserver.assertions) == 1
def test_regexp(httpserver: HTTPServer):
httpserver.expect_request(re.compile(r"/foo/\d+/bar/")).respond_with_json({"foo": "bar"})
assert requests.get(httpserver.url_for("/foo/123/bar/")).json() == {"foo": "bar"}
assert requests.get(httpserver.url_for("/foo/9999/bar/")).json() == {"foo": "bar"}
assert requests.get(httpserver.url_for("/foo/bar/")).status_code == 500
assert len(httpserver.assertions) == 1
def test_object_with_eq(httpserver: HTTPServer):
httpserver.expect_request(PrefixMatchEq("/foo")).respond_with_json({"foo": "bar"})
assert requests.get(httpserver.url_for("/foo")).json() == {"foo": "bar"}
assert requests.get(httpserver.url_for("/foobar")).json() == {"foo": "bar"}
assert requests.get(httpserver.url_for("/foobaz")).json() == {"foo": "bar"}
assert requests.get(httpserver.url_for("/barfoo")).status_code == 500
assert len(httpserver.assertions) == 1