File: test_dispatch.py

package info (click to toggle)
python-cheroot 11.1.2%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,236 kB
  • sloc: python: 6,969; makefile: 10
file content (58 lines) | stat: -rw-r--r-- 1,273 bytes parent folder | download
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
"""Tests for the HTTP server."""

from cheroot.wsgi import PathInfoDispatcher


def wsgi_invoke(app, environ):
    """Serve 1 request from a WSGI application."""
    response = {}

    def start_response(status, headers):
        response.update(
            {
                'status': status,
                'headers': headers,
            },
        )

    response['body'] = b''.join(
        app(environ, start_response),
    )

    return response


def test_dispatch_no_script_name():
    """Dispatch despite lack of ``SCRIPT_NAME`` in environ."""

    # Bare bones WSGI hello world app (from PEP 333).
    def app(environ, start_response):
        start_response(
            '200 OK',
            [
                ('Content-Type', 'text/plain; charset=utf-8'),
            ],
        )
        return [b'Hello, world!']

    # Build a dispatch table.
    d = PathInfoDispatcher(
        [
            ('/', app),
        ],
    )

    # Dispatch a request without `SCRIPT_NAME`.
    response = wsgi_invoke(
        d,
        {
            'PATH_INFO': '/foo',
        },
    )
    assert response == {
        'status': '200 OK',
        'headers': [
            ('Content-Type', 'text/plain; charset=utf-8'),
        ],
        'body': b'Hello, world!',
    }