class TestHTTPDaemon(unittest.TestCase): ''' This is a unittest for the ServerHandler which lives in http_daemon.py @author: Andres Riancho <andres . riancho | gmail . com> ''' def setUp(self): self.http_daemon = HTTPDaemon() self.http_daemon.start() self.http_daemon.wait_for_start() self.requests = self.http_daemon.requests def tearDown(self): self.http_daemon.shutdown() def test_simple_GET(self): url = 'http://%s:%s/hello' % ('127.0.0.1', self.http_daemon.get_port()) response_body = urllib2.urlopen(url).read() self.assertEqual(response_body, 'ABCDEF\n') self.assertEqual(len(self.requests), 1) request = self.requests[0] self.assertEqual(request.path, '/hello') self.assertEqual(request.command, 'GET') self.assertEqual(request.request_version, 'HTTP/1.1') self.assertIn('host', request.headers) self.assertEqual(request.request_body, None)
class TestProxy(XpresserUnittest): IMAGES = os.path.join('core', 'ui', 'tests', 'gui', 'proxy', 'images') EXTRA_IMAGES = os.path.join('core', 'ui', 'tests', 'gui', 'tools_menu', 'images') def setUp(self): XpresserUnittest.setUp(self) self.click('proxy-menu-icon') self.find('proxy-tabs') self.http_daemon = HTTPDaemon() self.http_daemon.start() self.http_daemon.wait_for_start() proxy_support = urllib2.ProxyHandler({'http': '127.0.0.1:8080'}) self.opener = urllib2.build_opener(proxy_support) def tearDown(self): self.click('close-with-cross') self.click('yes') self.http_daemon.shutdown() XpresserUnittest.tearDown(self) def test_basic_forwarding(self): port = self.http_daemon.get_port() http_response = self.opener.open('http://127.0.0.1:%s/foo' % port).read() self.assertEqual('ABCDEF\n', http_response) def test_intercept(self): self.click('intercept') def ui_clicker(): # Click on the proxy button that will forward the request try: self.find('GET_http') self.click('send-request') self.find('200_OK') self.click('next_request') self.find('empty_intercept') except: pass t = threading.Thread(target=ui_clicker) t.start() port = self.http_daemon.get_port() http_response = self.opener.open('http://127.0.0.1:%s/foo' % port).read() self.assertEqual('ABCDEF\n', http_response) t.join()
class TestManualRequests(XpresserUnittest): IMAGES = os.path.join('core', 'ui', 'tests', 'gui', 'manual_requests', 'images') EXTRA_IMAGES = os.path.join('core', 'ui', 'tests', 'gui', 'tools_menu', 'images') def setUp(self): XpresserUnittest.setUp(self) self.click('manual-request') def tearDown(self): self.click('close-with-cross') XpresserUnittest.tearDown(self) def test_offline_url(self): self.double_click('localhost') self.type('moth:8081', False) self.click('send') self.find('stopped_sending_requests') # Close the error dialog self.type([ '<Enter>', ], False) def test_invalid_request(self): self.double_click('localhost') self.type('moth:8081', False) # Move to the beginning and break the syntax self.type([ '<PgUp>', ], False) self.type('x', False) self.type([ '<Enter>', ], False) self.find('send_disabled') # Remove the ugly stuff self.type([ '<PgUp>', ], False) self.type([ '<Delete>', ], False) self.type([ '<Delete>', ], False) self.sleep(1) self.not_find('send_disabled') self.click('send') self.find('stopped_sending_requests') # Close the error dialog self.type([ '<Enter>', ], False) def test_GET_request(self): self.http_daemon = HTTPDaemon() self.http_daemon.start() self.http_daemon.wait_for_start() # # Send the request to our server using the GUI # self.double_click('localhost') self.type('127.0.0.1:%s' % self.http_daemon.get_port(), False) self.click('send') # Wait until we actually get the response, and verify we got the # response body we expected: self.find('abcdef') self.find('200_OK') # # Assert that it's what we really expected # requests = self.http_daemon.requests self.assertEqual(len(requests), 1) request = requests[0] head, postdata = MANUAL_REQUEST_EXAMPLE, '' http_request = HTTPRequestParser(head, postdata) self.assertEqual(http_request.get_url().get_path(), request.path) self.assertEqual(http_request.get_method(), request.command) for header_name, header_value in http_request.get_headers().iteritems( ): self.assertIn(header_name.lower(), request.headers) self.assertEqual(header_value, request.headers[header_name.lower()]) self.http_daemon.shutdown() def test_POST_request(self): self.http_daemon = HTTPDaemon() self.http_daemon.start() self.http_daemon.wait_for_start() # # Send the request to our server using the GUI # self.double_click('localhost') self.type('127.0.0.1:%s' % self.http_daemon.get_port(), False) # Move to the beginning self.type([ '<PgUp>', ], False) # Replace GET with POST self.type([ '<Delete>', ], False) self.type([ '<Delete>', ], False) self.type([ '<Delete>', ], False) self.type('POST', False) # Move to the end (postdata) self.type([ '<PgDn>', ], False) post_data = 'foo=bar&spam=eggs' self.type(post_data, False) self.click('send') # Wait until we actually get the response, and verify we got the # response body we expected: self.find('abcdef') self.find('200_OK') # # Assert that it's what we really expected # requests = self.http_daemon.requests self.assertEqual(len(requests), 1) request = requests[0] head, postdata = MANUAL_REQUEST_EXAMPLE, '' http_request = HTTPRequestParser(head, postdata) self.assertEqual(http_request.get_url().get_path(), request.path) self.assertEqual('POST', request.command) for header_name, header_value in http_request.get_headers().iteritems( ): self.assertIn(header_name.lower(), request.headers) self.assertEqual(header_value, request.headers[header_name.lower()]) self.assertEqual(str(len(post_data)), request.headers['content-length']) self.http_daemon.shutdown()
class TestFuzzyRequestEditor(XpresserUnittest): IMAGES = os.path.join('core', 'ui', 'tests', 'gui', 'fuzzy_request_editor', 'images') EXTRA_IMAGES = os.path.join('core', 'ui', 'tests', 'gui', 'tools_menu', 'images') def setUp(self): XpresserUnittest.setUp(self) self.click('fuzzy-requests-icon') self.find('fuzzy-requests-tabs') def tearDown(self): self.click('close-with-cross') XpresserUnittest.tearDown(self) def test_offline_url(self): self.double_click('localhost') self.type('moth:8081', False) self.click('play') self.find('error') # Close the error dialog self.type(['<Enter>',], False) def test_invalid_request(self): self.double_click('localhost') self.type('moth:8081', False) # Move to the beginning and break the syntax self.type(['<PgUp>',], False) self.type('x', False) self.type(['<Enter>',], False) self.find('send-disabled') # Remove the ugly stuff self.type(['<PgUp>',], False) self.type(['<Delete>',], False) self.type(['<Delete>',], False) self.sleep(1) self.not_find('send_disabled') self.click('play') self.find('error') # Close the error dialog self.type(['<Enter>',], False) def test_GET_request(self): self.http_daemon = HTTPDaemon() self.http_daemon.start() self.http_daemon.wait_for_start() # # Send the request to our server using the GUI # self.double_click('localhost') self.type('127.0.0.1:%s' % self.http_daemon.get_port(), False) self.click('play') # Wait until we actually get the response, and verify we got the # response body we expected: self.click('response_tab') self.find('abcdef') self.find('200_OK') # # Assert that it's what we really expected # requests = self.http_daemon.requests self.assertEqual(len(requests), 10) head, postdata = FUZZY_REQUEST_EXAMPLE, '' parsed_request = HTTPRequestParser(head, postdata) for i, daemon_request in enumerate(self.http_daemon.requests): self.assertEqual('/%s' % i, daemon_request.path) self.assertEqual(parsed_request.get_method(), daemon_request.command) for header_name, header_value in parsed_request.get_headers().iteritems(): self.assertIn(header_name.lower(), daemon_request.headers) self.assertEqual(header_value, daemon_request.headers[header_name.lower()]) self.http_daemon.shutdown() def test_POST_request(self): self.http_daemon = HTTPDaemon() self.http_daemon.start() self.http_daemon.wait_for_start() # # Send the request to our server using the GUI # self.double_click('localhost') self.type('127.0.0.1:%s' % self.http_daemon.get_port(), False) # Move to the beginning self.type(['<PgUp>',], False) # Replace GET with POST self.type(['<Delete>',], False) self.type(['<Delete>',], False) self.type(['<Delete>',], False) self.type('POST', False) # Move to the end (postdata) self.type(['<PgDn>',], False) post_data = 'foo=bar&spam=eggs' self.type(post_data, False) self.click('play') # Wait until we actually get the response, and verify we got the # response body we expected: self.click('response_tab') self.find('abcdef') self.find('200_OK') # # Assert that it's what we really expected # requests = self.http_daemon.requests self.assertEqual(len(requests), 10) head, postdata = FUZZY_REQUEST_EXAMPLE, '' parsed_request = HTTPRequestParser(head, postdata) for i, daemon_request in enumerate(self.http_daemon.requests): self.assertEqual('/%s' % i, daemon_request.path) self.assertEqual('POST', daemon_request.command) for header_name, header_value in parsed_request.get_headers().iteritems(): self.assertIn(header_name.lower(), daemon_request.headers) self.assertEqual(header_value, daemon_request.headers[header_name.lower()]) self.http_daemon.shutdown()