Skip to content

Commit 9a33321

Browse files
committed
use local server on more tests
1 parent f3610ea commit 9a33321

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

tests/files_test.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,36 @@
55
"""Post Files Tests"""
66
# pylint: disable=line-too-long
77

8+
import functools
89
import re
10+
import socketserver
11+
import threading
12+
import time
913
from unittest import mock
1014

1115
import mocket
1216
import pytest
1317
import requests as python_requests
18+
from local_test_server import LocalTestServerHandler
19+
20+
21+
def uses_local_server(func):
22+
@functools.wraps(func)
23+
def wrapper(*args, **kwargs):
24+
with socketserver.TCPServer(("127.0.0.1", 5000), LocalTestServerHandler) as server:
25+
server_thread = threading.Thread(target=server.serve_forever)
26+
server_thread.daemon = True
27+
server_thread.start()
28+
time.sleep(2) # Give the server some time to start
29+
30+
result = func(*args, **kwargs)
31+
32+
server.shutdown()
33+
server.server_close()
34+
time.sleep(2)
35+
return result
36+
37+
return wrapper
1438

1539

1640
@pytest.fixture
@@ -20,7 +44,8 @@ def log_stream():
2044

2145
@pytest.fixture
2246
def post_url():
23-
return "https://httpbin.org/post"
47+
# return "https://httpbin.org/post"
48+
return "http://127.0.0.1:5000/post"
2449

2550

2651
@pytest.fixture
@@ -63,6 +88,7 @@ def get_actual_request_data(log_stream):
6388
return boundary, content_length, actual_request_post
6489

6590

91+
@uses_local_server
6692
def test_post_file_as_data( # pylint: disable=unused-argument
6793
requests, sock, log_stream, post_url, request_logging
6894
):
@@ -85,6 +111,7 @@ def test_post_file_as_data( # pylint: disable=unused-argument
85111
assert sent.endswith(actual_request_post)
86112

87113

114+
@uses_local_server
88115
def test_post_files_text( # pylint: disable=unused-argument
89116
sock, requests, log_stream, post_url, request_logging
90117
):
@@ -120,6 +147,7 @@ def test_post_files_text( # pylint: disable=unused-argument
120147
assert sent.endswith(actual_request_post)
121148

122149

150+
@uses_local_server
123151
def test_post_files_file( # pylint: disable=unused-argument
124152
sock, requests, log_stream, post_url, request_logging
125153
):

tests/local_test_server.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66

77

88
class LocalTestServerHandler(SimpleHTTPRequestHandler):
9+
def do_POST(self):
10+
if self.path == "/post":
11+
resp_body = json.dumps({"url": "http://localhost:5000/post"}).encode("utf-8")
12+
self.send_response(200)
13+
self.send_header("Content-type", "application/json")
14+
self.send_header("Content-Length", str(len(resp_body)))
15+
self.end_headers()
16+
self.wfile.write(resp_body)
17+
918
def do_GET(self):
1019
if self.path == "/get":
1120
resp_body = json.dumps({"url": "http://localhost:5000/get"}).encode("utf-8")

0 commit comments

Comments
 (0)