Skip to content
Myr

Implemented dummy socket server interface and added one test

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Braulio Valdivielso Martínez · committed by Braulio Valdivielso Martínez · parent 6506dc8 · Browse files · 3 files+57 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+57 added−0 removed

  • other:+57 added−0 removed

3 files+57 added−0 removed

added:dummyserver/__init__.py
added:dummyserver/server.py+38 added
1#!/usr/bin/python23import threading, socket4567"""8Dummy server using for unit testing9"""1011class Server(threading.Thread):12 def __init__(self, handler, host='localhost', port=8021):13 threading.Thread.__init__(self)14 self.handler = handler15 self.host = host16 self.port = port17 self.ready_event = threading.Event()18 self.stop_event = threading.Event()1920 def run(self):21 sock = socket.socket()22 sock.bind((self.host, self.port))23 sock.listen(0)24 self.ready_event.set()25 self.handler(sock)26 self.stop_event.set()27 sock.close()2829 def __enter__(self):30 self.start()31 self.ready_event.wait()32 return self.host, self.port33 34 def __exit__(self, exc_type, exc_value, traceback):35 if exc_type is None:36 self.stop_event.wait()37 return False # allow exceptions to propagate 38
modified:test_requests.py+19 added
27 unmodified lines
28from requests.structures import CaseInsensitiveDict29from requests.sessions import SessionRedirectMixin30from requests.models import urlencode31from requests.hooks import default_hooks32from dummyserver.server import Server33import socket3435try:36 import StringIO37except ImportError:
1699 unmodified lines
17371738 with pytest.raises(ImportError):1739 from requests.packages import webbrowser17401741class TestDummyServer(unittest.TestCase):1742 def test_basic(self):1743 question = "sucess?"1744 answer = "yeah, success"1745 def handler(server_sock):1746 sock, _ = server_sock.accept()1747 text = sock.recv(1000)1748 assert text == question 1749 sock.send(answer)1750 1751 with Server(handler) as (host, port):1752 sock = socket.socket()1753 sock.connect((host, port))1754 sock.send(question)1755 text = sock.recv(1000)1756 assert text == answer1757 sock.close()17581759if __name__ == '__main__':1760 unittest.main()