def test_simple_proxying(self): # We start up a backend backend = create_and_start_backend(0) # ...and an rsp option that proxies everything to it. _, config_file = mkstemp(suffix=".lua") with open(config_file, "w") as f: f.writelines([ 'listenPort = "8000"' 'backendAddress = "127.0.0.1"' 'backendPort = "8888"' ]) server = pexpect.spawn(RSP_BINARY, [config_file]) server.expect("Started. Listening on port 8000.") try: # Make a request and check it works. response = requests.get("http://127.0.0.1:8000") self.assertEqual(response.status_code, 200) self.assertEqual(response.text, "Hello from the mock server\n") # Make a second request and check it works too. response = requests.get("http://127.0.0.1:8000") self.assertEqual(response.status_code, 200) self.assertEqual(response.text, "Hello from the mock server\n") finally: server.kill(9) os.remove(config_file) backend.shutdown()
def test_large_response(self): # We start up a backend expected_response = "0123456789\n" * 1024 * 1024 backend = create_and_start_backend(0, expected_response) # ...and an rsp option that proxies everything to it. _, config_file = mkstemp(suffix=".lua") with open(config_file, "w") as f: f.writelines([ 'listenPort = "8000"' 'backendAddress = "127.0.0.1"' 'backendPort = "8888"' ]) server = pexpect.spawn(RSP_BINARY, [config_file]) server.expect("Started. Listening on port 8000.") try: # Make a request and check it works. response = requests.get("http://127.0.0.1:8000") self.assertEqual(response.status_code, 200) self.assertEqual(len(response.text), len(expected_response)) self.assertEqual(response.text, expected_response, "Response incorrect") finally: server.kill(9) os.remove(config_file) backend.shutdown()
def test_can_handle_client_disconnect(self): backend = create_and_start_backend(0, "0123456789\n" * 1024 * 1024) _, config_file = mkstemp(suffix=".lua") with open(config_file, "w") as f: f.writelines([ 'listenPort = "8000"' 'backendAddress = "127.0.0.1"' 'backendPort = "8888"' ]) server = pexpect.spawn(RSP_BINARY, [config_file]) server.expect("Started. Listening on port 8000.") try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) sock.connect(("localhost", 8000)) sock.send("GET / HTTP/1.1\r\n") sock.send("\r\n") sock.recv(1024) sock.close() # Check it didn't crash response = requests.get("http://127.0.0.1:8000", timeout=10) self.assertEqual(response.status_code, 200) finally: server.kill(9) print "-" * 80 for line in server.readlines(): print line, print "-" * 80 os.remove(config_file) backend.shutdown()
def test_gives_error_on_long_requests(self): backend = create_and_start_backend(5) _, config_file = mkstemp(suffix=".lua") with open(config_file, "w") as f: f.writelines([ 'listenPort = "8000"' 'backendAddress = "127.0.0.1"' 'backendPort = "8888"' ]) server = pexpect.spawn(RSP_BINARY, [config_file]) server.expect("Started. Listening on port 8000.") try: sock = socket.socket() sock.connect(("localhost", 8000)) sock.send("GET / HTTP/1.1\r") while True: try: sent = sock.send("A-Header: some values\r") except: break if sent == 0: break return_value = sock.recv(10000) self.assertTrue(return_value.startswith("HTTP/1.0 414 ")) finally: server.kill(9) os.remove(config_file) backend.shutdown()
def test_multiple_roughly_simultaneous_requests_complete_in_time_roughly_equivalent_to_one_request(self): backend = create_and_start_backend(5) _, config_file = mkstemp(suffix=".lua") with open(config_file, "w") as f: f.writelines([ 'listenPort = "8000"' 'backendAddress = "127.0.0.1"' 'backendPort = "8888"' ]) server = pexpect.spawn(RSP_BINARY, [config_file]) server.expect("Started. Listening on port 8000.") try: responses = [] errors = [] def make_request(): try: responses.append(requests.get("http://127.0.0.1:8000")) except Exception, e: errors.append(e) start = time.time() client_threads = [] for i in range(4): t = Thread(target=make_request) t.daemon = True t.start() client_threads.append(t) for client_thread in client_threads: client_thread.join() self.assertTrue(time.time() - start < 6) if errors != []: raise errors[0] for response in responses: self.assertEqual(response.status_code, 200) self.assertEqual(response.text, "Hello from the mock server\n")
def test_simple_proxying(self): # We start up a backend backend = create_and_start_backend(0) # ...and an rsp option that proxies everything to it. _, config_file = mkstemp(suffix=".lua") with open(config_file, "w") as f: f.writelines([ 'listenPort = "8000"' 'backendAddress = "127.0.0.1"' 'backendPort = "8888"' ]) server = pexpect.spawn(RSP_BINARY, [config_file]) try: now = datetime.now() expected_timestamp = now.strftime("%Y-%m-%d %H:%M") server.expect("\\[%s:.*\\] Started. Listening on port 8000." % (expected_timestamp,), timeout=5) finally: server.kill(9) os.remove(config_file) backend.shutdown()