/
test_unittests_server.py
88 lines (68 loc) · 2.12 KB
/
test_unittests_server.py
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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import server
import pytest
################
# UNIT TESTS
################
def test_response_ok():
response = server.response_ok(b"123456789", "text/plain")
assert "text/plain" in response
assert '9' in response
def test_response_error():
response = server.response_error(
b"405",
b"Method Not Allowed",
b"GET method required."
)
assert b"GET method required" in response
assert b"text/html" in response
assert b"</body>" in response
def test_parse_request_rejects_non_get():
with pytest.raises(NotImplementedError):
server.parse_request(
b"POST / HTTP/1.1\r\n"
b"Host: http://example.com\r\n"
b"\r\n"
)
def test_parse_request_rejects_non_http_1_1():
with pytest.raises(Exception):
server.parse_request(
b"GET / HTTP/1.0\r\n"
b"Host: http://example.com\r\n"
b"\r\n"
)
def test_parse_request_rejects_absent_host_header():
with pytest.raises(ValueError):
server.parse_request(
b"GET / HTTP/1.1\r\n"
b"Some bullshit header\r\n"
b"Another shitty header\r\n"
b"\r\n"
)
def test_parse_request_returns_validated_request():
valid_request = (
b"GET /heres/the/URI HTTP/1.1\r\n"
b"Host: www.example.com\r\n"
b"\r\n"
b"blah blah some kind of body\r\n"
b"\r\n"
)
assert b"/heres/the/URI" == server.parse_request(valid_request)
def test_resolve_uri_success():
body, content_type = server.resolve_uri(b'/sample.txt')
assert b"simple text file" in body
assert b"text" in content_type
def test_resolve_uri_failure():
with pytest.raises(IOError):
server.resolve_uri(b"laskjdhfku")
def test_resolve_uri_fails_given_dots():
with pytest.raises(IOError):
req = (
b"GET /../heres/the/URI HTTP/1.1\r\n"
b"Host: www.example.com\r\n"
b"\r\n"
b"blah blah some kind of body\r\n"
b"\r\n"
)
server.resolve_uri(req)