File: example_pytest.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 (31 lines) | stat: -rw-r--r-- 1,331 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
# Run this code as 'pytest example_pytest.py'

import pytest
import requests

from pytest_httpserver import HTTPServer


# specify where the server should bind to
# you can return 0 as the port, in this case it will bind to a free (ephemeral) TCP port
@pytest.fixture(scope="session")
def httpserver_listen_address():
    return ("127.0.0.1", 8000)


# specify httpserver fixture
def test_oneshot_and_permanent_happy_path1(httpserver: HTTPServer):
    # define some request handlers
    # more details in the documentation
    httpserver.expect_request("/permanent").respond_with_data("OK permanent")
    httpserver.expect_oneshot_request("/oneshot1").respond_with_data("OK oneshot1")
    httpserver.expect_oneshot_request("/oneshot2").respond_with_data("OK oneshot2")

    # query those handlers with a real HTTP client (requests in this example but could by anything)
    # the 'url_for' method  formats the final URL, so there's no need to wire-in any ports
    assert requests.get(httpserver.url_for("/permanent")).text == "OK permanent"
    assert requests.get(httpserver.url_for("/oneshot1")).text == "OK oneshot1"
    assert requests.get(httpserver.url_for("/oneshot2")).text == "OK oneshot2"
    assert requests.get(httpserver.url_for("/permanent")).text == "OK permanent"

    assert len(httpserver.oneshot_handlers) == 0