|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +test_http11.py |
| 4 | +~~~~~~~~~~~~~~ |
| 5 | +
|
| 6 | +Unit tests for hyper's HTTP/1.1 implementation. |
| 7 | +""" |
| 8 | +from io import BytesIO |
| 9 | + |
| 10 | +from hyper.http11.connection import HTTP11Connection |
| 11 | +from hyper.http11.response import HTTP11Response |
| 12 | + |
| 13 | + |
| 14 | +class TestHTTP11Connection(object): |
| 15 | + def test_initialization_no_port(self): |
| 16 | + c = HTTP11Connection('http2bin.org') |
| 17 | + |
| 18 | + assert c.host == 'http2bin.org' |
| 19 | + assert c.port == 80 |
| 20 | + assert not c.secure |
| 21 | + |
| 22 | + def test_initialization_inline_port(self): |
| 23 | + c = HTTP11Connection('http2bin.org:443') |
| 24 | + |
| 25 | + assert c.host == 'http2bin.org' |
| 26 | + assert c.port == 443 |
| 27 | + assert c.secure |
| 28 | + |
| 29 | + def test_initialization_separate_port(self): |
| 30 | + c = HTTP11Connection('localhost', 8080) |
| 31 | + |
| 32 | + assert c.host == 'localhost' |
| 33 | + assert c.port == 8080 |
| 34 | + assert not c.secure |
| 35 | + |
| 36 | + def test_basic_request(self): |
| 37 | + c = HTTP11Connection('http2bin.org') |
| 38 | + c._sock = sock = DummySocket() |
| 39 | + |
| 40 | + c.request('GET', '/get', headers={'User-Agent': 'hyper'}) |
| 41 | + |
| 42 | + expected = ( |
| 43 | + b"GET /get HTTP/1.1\r\n" |
| 44 | + b"User-Agent: hyper\r\n" |
| 45 | + b"\r\n" |
| 46 | + ) |
| 47 | + received = b''.join(sock.queue) |
| 48 | + |
| 49 | + assert received == expected |
| 50 | + |
| 51 | + def test_request_with_bytestring_body(self): |
| 52 | + c = HTTP11Connection('http2bin.org') |
| 53 | + c._sock = sock = DummySocket() |
| 54 | + |
| 55 | + c.request('POST', '/post', headers={'User-Agent': 'hyper'}, body=b'hi') |
| 56 | + |
| 57 | + expected = ( |
| 58 | + b"POST /post HTTP/1.1\r\n" |
| 59 | + b"User-Agent: hyper\r\n" |
| 60 | + b"\r\n" |
| 61 | + b"hi" |
| 62 | + ) |
| 63 | + received = b''.join(sock.queue) |
| 64 | + |
| 65 | + assert received == expected |
| 66 | + |
| 67 | + def test_get_response(self): |
| 68 | + c = HTTP11Connection('http2bin.org') |
| 69 | + c._sock = sock = DummySocket() |
| 70 | + |
| 71 | + sock.buffer= BytesIO( |
| 72 | + b"HTTP/1.1 201 No Content\r\n" |
| 73 | + b"Connection: close\r\n" |
| 74 | + b"Server: Socket\r\n" |
| 75 | + b"Content-Length: 0\r\n" |
| 76 | + b"\r\n" |
| 77 | + ) |
| 78 | + |
| 79 | + r = c.get_response() |
| 80 | + |
| 81 | + assert r.status == 201 |
| 82 | + assert r.reason == b'No Content' |
| 83 | + assert list(r.headers.iter_raw()) == [ |
| 84 | + (b'Connection', b'close'), |
| 85 | + (b'Server', b'Socket'), |
| 86 | + (b'Content-Length', b'0') |
| 87 | + ] |
| 88 | + assert r.read() == b'' |
| 89 | + |
| 90 | + def test_response_short_reads(self): |
| 91 | + c = HTTP11Connection('http2bin.org') |
| 92 | + c._sock = sock = DummySocket() |
| 93 | + |
| 94 | + sock.buffer= BytesIO( |
| 95 | + b"HTTP/1.1 200 OK\r\n" |
| 96 | + b"Content-Length: 15\r\n" |
| 97 | + b"\r\n" |
| 98 | + b"hellotherechamp" |
| 99 | + ) |
| 100 | + |
| 101 | + r = c.get_response() |
| 102 | + |
| 103 | + assert r.status == 200 |
| 104 | + assert r.reason == b'OK' |
| 105 | + assert r.read(5) == b'hello' |
| 106 | + assert r.read(5) == b'there' |
| 107 | + assert r.read(5) == b'champ' |
| 108 | + assert r.read(5) == b'' |
| 109 | + |
| 110 | + |
| 111 | +class TestHTTP11Response(object): |
| 112 | + def test_short_circuit_read(self): |
| 113 | + r = HTTP11Response(200, 'OK', {}, None) |
| 114 | + |
| 115 | + assert r.read() == b'' |
| 116 | + |
| 117 | + |
| 118 | +class DummySocket(object): |
| 119 | + def __init__(self): |
| 120 | + self.queue = [] |
| 121 | + self.buffer = BytesIO() |
| 122 | + self.can_read = False |
| 123 | + |
| 124 | + def send(self, data): |
| 125 | + self.queue.append(data) |
| 126 | + |
| 127 | + def recv(self, l): |
| 128 | + return memoryview(self.buffer.read(l)) |
| 129 | + |
| 130 | + def close(self): |
| 131 | + pass |
| 132 | + |
| 133 | + def readline(self): |
| 134 | + return memoryview(self.buffer.readline()) |
0 commit comments