Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: recipe_engine/third_party/requests/tests/test_testserver.py

Issue 2164713003: Vendor requests. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/recipes-py@master
Patch Set: Fix deps.pyl Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 import threading
2 import socket
3 import time
4
5 import pytest
6 import requests
7 from tests.testserver.server import Server
8
9 class TestTestServer:
10 def test_basic(self):
11 """messages are sent and received properly"""
12 question = b"sucess?"
13 answer = b"yeah, success"
14 def handler(sock):
15 text = sock.recv(1000)
16 assert text == question
17 sock.sendall(answer)
18
19 with Server(handler) as (host, port):
20 sock = socket.socket()
21 sock.connect((host, port))
22 sock.sendall(question)
23 text = sock.recv(1000)
24 assert text == answer
25 sock.close()
26
27 def test_server_closes(self):
28 """the server closes when leaving the context manager"""
29 with Server.basic_response_server() as (host, port):
30 sock = socket.socket()
31 sock.connect((host, port))
32
33 sock.close()
34
35 with pytest.raises(socket.error):
36 new_sock = socket.socket()
37 new_sock.connect((host, port))
38
39 def test_text_response(self):
40 """the text_response_server sends the given text"""
41 server = Server.text_response_server(
42 "HTTP/1.1 200 OK\r\n" +
43 "Content-Length: 6\r\n" +
44 "\r\nroflol"
45 )
46
47 with server as (host, port):
48 r = requests.get('http://{0}:{1}'.format(host, port))
49
50 assert r.status_code == 200
51 assert r.text == u'roflol'
52 assert r.headers['Content-Length'] == '6'
53
54 def test_basic_response(self):
55 """the basic response server returns an empty http response"""
56 with Server.basic_response_server() as (host, port):
57 r = requests.get('http://{0}:{1}'.format(host, port))
58 assert r.status_code == 200
59 assert r.text == u''
60 assert r.headers['Content-Length'] == '0'
61
62 def test_basic_waiting_server(self):
63 """the server waits for the block_server event to be set before closing" ""
64 block_server = threading.Event()
65
66 with Server.basic_response_server(wait_to_close_event=block_server) as ( host, port):
67 sock = socket.socket()
68 sock.connect((host, port))
69 sock.sendall(b'send something')
70 time.sleep(2.5)
71 sock.sendall(b'still alive')
72 block_server.set() # release server block
73
74 def test_multiple_requests(self):
75 """multiple requests can be served"""
76 requests_to_handle = 5
77
78 server = Server.basic_response_server(requests_to_handle=requests_to_han dle)
79
80 with server as (host, port):
81 server_url = 'http://{0}:{1}'.format(host, port)
82 for _ in range(requests_to_handle):
83 r = requests.get(server_url)
84 assert r.status_code == 200
85
86 # the (n+1)th request fails
87 with pytest.raises(requests.exceptions.ConnectionError):
88 r = requests.get(server_url)
89
90 def test_request_recovery(self):
91 """can check the requests content"""
92 server = Server.basic_response_server(requests_to_handle=2)
93 first_request = b'put your hands up in the air'
94 second_request = b'put your hand down in the floor'
95
96 with server as address:
97 sock1 = socket.socket()
98 sock2 = socket.socket()
99
100 sock1.connect(address)
101 sock1.sendall(first_request)
102 sock1.close()
103
104 sock2.connect(address)
105 sock2.sendall(second_request)
106 sock2.close()
107
108 assert server.handler_results[0] == first_request
109 assert server.handler_results[1] == second_request
110
111 def test_requests_after_timeout_are_not_received(self):
112 """the basic response handler times out when receiving requests"""
113 server = Server.basic_response_server(request_timeout=1)
114
115 with server as address:
116 sock = socket.socket()
117 sock.connect(address)
118 time.sleep(1.5)
119 sock.sendall(b'hehehe, not received')
120 sock.close()
121
122 assert server.handler_results[0] == b''
123
124
125 def test_request_recovery_with_bigger_timeout(self):
126 """a biggest timeout can be specified"""
127 server = Server.basic_response_server(request_timeout=3)
128 data = b'bananadine'
129
130 with server as address:
131 sock = socket.socket()
132 sock.connect(address)
133 time.sleep(1.5)
134 sock.sendall(data)
135 sock.close()
136
137 assert server.handler_results[0] == data
OLDNEW
« no previous file with comments | « recipe_engine/third_party/requests/tests/test_structures.py ('k') | recipe_engine/third_party/requests/tests/test_utils.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698