File: http_server.py

package info (click to toggle)
httpie 3.2.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,904 kB
  • sloc: python: 13,760; xml: 162; makefile: 141; ruby: 79; sh: 32
file content (158 lines) | stat: -rw-r--r-- 4,482 bytes parent folder | download | duplicates (3)
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
import threading
import json

from collections import defaultdict
from contextlib import contextmanager
from http import HTTPStatus
from http.cookies import SimpleCookie
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs

import pytest


class TestHandler(BaseHTTPRequestHandler):
    handlers = defaultdict(dict)

    @classmethod
    def handler(cls, method, path):
        def inner(func):
            cls.handlers[method][path] = func
            return func
        return inner

    def do_generic(self):
        parse_result = urlparse(self.path)
        func = self.handlers[self.command].get(parse_result.path)
        if func is None:
            return self.send_error(HTTPStatus.NOT_FOUND)

        return func(self)

    do_GET = do_generic
    do_POST = do_generic


@TestHandler.handler('GET', '/headers')
def get_headers(handler):
    handler.send_response(200)
    for key, value in handler.headers.items():
        handler.send_header(key, value)
    handler.send_header('Content-Length', 0)
    handler.end_headers()


@TestHandler.handler('GET', '/drip')
def chunked_drip(handler):
    handler.send_response(200)
    accept = handler.headers.get('Accept')
    if accept is not None:
        handler.send_header('Content-Type', accept)
    handler.send_header('Transfer-Encoding', 'chunked')
    handler.end_headers()

    for _ in range(3):
        body = 'test\n'
        handler.wfile.write(f'{len(body):X}\r\n{body}\r\n'.encode('utf-8'))

    handler.wfile.write('0\r\n\r\n'.encode('utf-8'))


@TestHandler.handler('GET', '/stream/encoding/random')
def random_encoding(handler):
    from tests.fixtures import ASCII_FILE_CONTENT, FILE_CONTENT as UNICODE_FILE_CONTENT

    handler.send_response(200)
    handler.send_header('Transfer-Encoding', 'chunked')
    handler.end_headers()

    for body in [
        ASCII_FILE_CONTENT,
        ASCII_FILE_CONTENT,
        UNICODE_FILE_CONTENT,
        UNICODE_FILE_CONTENT,
        UNICODE_FILE_CONTENT,
    ]:
        body += "\n"
        handler.wfile.write(f'{len(body.encode()):X}\r\n{body}\r\n'.encode())

    handler.wfile.write('0\r\n\r\n'.encode('utf-8'))


@TestHandler.handler('POST', '/status/msg')
def status_custom_msg(handler):
    content_len = int(handler.headers.get('content-length', 0))
    post_body = handler.rfile.read(content_len).decode()

    handler.send_response(200, post_body)
    handler.end_headers()


@TestHandler.handler('GET', '/cookies')
def get_cookies(handler):
    cookies = {
        'cookies': {
            key: cookie.value
            for key, cookie in SimpleCookie(handler.headers.get('Cookie')).items()
        }
    }
    payload = json.dumps(cookies)

    handler.send_response(200)
    handler.send_header('Content-Length', len(payload))
    handler.send_header('Content-Type', 'application/json')
    handler.end_headers()
    handler.wfile.write(payload.encode('utf-8'))


@TestHandler.handler('GET', '/cookies/set')
def set_cookies(handler):
    options = parse_qs(urlparse(handler.path).query)

    handler.send_response(200)
    for cookie, [value] in options.items():
        handler.send_header('Set-Cookie', f'{cookie}={value}')

    handler.end_headers()


@TestHandler.handler('GET', '/cookies/set-and-redirect')
def set_cookie_and_redirect(handler):
    handler.send_response(302)

    redirect_to = handler.headers.get('X-Redirect-To', '/headers')
    handler.send_header('Location', redirect_to)

    raw_cookies = handler.headers.get('X-Cookies', 'a=b')
    for cookie in raw_cookies.split(', '):
        handler.send_header('Set-Cookie', cookie)
    handler.end_headers()


@contextmanager
def _http_server():
    server = HTTPServer(('localhost', 0), TestHandler)
    thread = threading.Thread(target=server.serve_forever)
    thread.start()
    yield server
    server.shutdown()
    thread.join()


@pytest.fixture(scope="function")
def http_server():
    """A custom HTTP server implementation for our tests, that is
    built on top of the http.server module. Handy when we need to
    deal with details which httpbin can not capture."""

    with _http_server() as server:
        yield '{0}:{1}'.format(*server.socket.getsockname())


@pytest.fixture(scope="function")
def localhost_http_server():
    """Just like the http_server, but uses the static
    `localhost` name for the host."""

    with _http_server() as server:
        yield 'localhost:{1}'.format(*server.socket.getsockname())