File: test_network_failures.py

package info (click to toggle)
geventhttpclient 2.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,456 kB
  • sloc: ansic: 16,557; python: 3,823; makefile: 24
file content (154 lines) | stat: -rw-r--r-- 4,071 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
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
from http.client import HTTPException

import gevent.server
import gevent.socket
import pytest

from geventhttpclient.client import CRLF, HTTPClient
from tests.common import LISTENER, server


def wrong_response_status_line(sock, addr):
    sock.recv(4096)
    sock.sendall(b"HTTP/1.1 apfais df0 asdf\r\n\r\n")


def test_exception():
    with server(wrong_response_status_line):
        connection = HTTPClient(*LISTENER)
        with pytest.raises(HTTPException):
            connection.get("/")


def close(sock, addr):
    sock.close()


def test_close():
    with server(close):
        client = HTTPClient(*LISTENER)
        with pytest.raises(HTTPException):
            client.get("/")


def close_after_recv(sock, addr):
    sock.recv(4096)
    sock.close()


def test_close_after_recv():
    with server(close_after_recv):
        client = HTTPClient(*LISTENER)
        with pytest.raises(HTTPException):
            client.get("/")


def timeout_recv(sock, addr):
    sock.recv(4096)
    gevent.sleep(1)


def test_timeout_recv():
    with server(timeout_recv):
        connection = HTTPClient(*LISTENER, network_timeout=0.1)
        with pytest.raises(gevent.socket.timeout):
            connection.request("GET", "/")


def timeout_send(sock, addr):
    gevent.sleep(1)


def test_timeout_send():
    with server(timeout_send):
        connection = HTTPClient(*LISTENER, network_timeout=0.1)
        with pytest.raises(gevent.socket.timeout):
            connection.request("GET", "/")


def close_during_content(sock, addr):
    sock.recv(4096)
    sock.sendall(b"""HTTP/1.1 200 Ok\r\nContent-Length: 100\r\n\r\n""")
    sock.close()


def test_close_during_content():
    with server(close_during_content):
        client = HTTPClient(*LISTENER, block_size=1)
        response = client.get("/")
        with pytest.raises(HTTPException):
            response.read()


def content_too_small(sock, addr):
    sock.recv(4096)
    sock.sendall(b"""HTTP/1.1 200 Ok\r\nContent-Length: 100\r\n\r\ncontent""")
    gevent.sleep(10)


def test_content_too_small():
    with server(content_too_small):
        client = HTTPClient(*LISTENER, network_timeout=0.2)
        with pytest.raises(gevent.socket.timeout):
            response = client.get("/")
            response.read()


def close_during_chuncked_readline(sock, addr):
    sock.recv(4096)
    sock.sendall(b"HTTP/1.1 200 Ok\r\nTransfer-Encoding: chunked\r\n\r\n")

    chunks = [
        "This is the data in the first chunk\r\n",
        "and this is the second one\r\n",
        "con\r\n",
    ]

    for chunk in chunks:
        gevent.sleep(0.1)
        sock.sendall((hex(len(chunk))[2:] + CRLF + chunk + CRLF).encode())
    sock.close()


def test_close_during_chuncked_readline():
    with server(close_during_chuncked_readline):
        client = HTTPClient(*LISTENER)
        response = client.get("/")
        assert response["transfer-encoding"] == "chunked"
        chunks = []
        with pytest.raises(HTTPException):
            data = "enter_loop"
            while data:
                data = response.readline()
                chunks.append(data)
        assert len(chunks) == 3


def timeout_during_chuncked_readline(sock, addr):
    sock.recv(4096)
    sock.sendall(b"HTTP/1.1 200 Ok\r\nTransfer-Encoding: chunked\r\n\r\n")

    chunks = [
        "This is the data in the first chunk\r\n",
        "and this is the second one\r\n",
        "con\r\n",
    ]

    for chunk in chunks:
        sock.sendall((hex(len(chunk))[2:] + CRLF + chunk + CRLF).encode())
    gevent.sleep(2)
    sock.close()


def test_timeout_during_chuncked_readline():
    with server(timeout_during_chuncked_readline):
        client = HTTPClient(*LISTENER, network_timeout=0.1)
        response = client.get("/")
        assert response["transfer-encoding"] == "chunked"
        chunks = []
        with pytest.raises(gevent.socket.timeout):
            data = "enter_loop"
            while data:
                data = response.readline()
                chunks.append(data)
        assert len(chunks) == 3