File: test_urimatch.py

package info (click to toggle)
pytest-httpserver 1.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 908 kB
  • sloc: python: 2,382; makefile: 77; sh: 21
file content (54 lines) | stat: -rw-r--r-- 1,875 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
import re

import requests

from pytest_httpserver import HTTPServer
from pytest_httpserver import URIPattern


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"})  # type: ignore
    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