def setUp(self): self.rpc_test_double = "{0}".format(random_string(40)) self.rpc_test_datetime = "{0}".format(random_string(40)) self.rpc_test_throw_exception = "{0}".format(random_string(40)) RegisterPath("/{0}".format(self.rpc_test_double), self.handler_class.__name__, is_rpc=True)(self._rpc_test_double_handler) RegisterPath("/{0}".format(self.rpc_test_datetime), self.handler_class.__name__, is_rpc=True)(self._rpc_test_datetime_handler) RegisterPath("/{0}".format(self.rpc_test_throw_exception), self.handler_class.__name__, is_rpc=True)(self._rpc_test_throw_exception) super(ServerHTTPTests, self).setUp()
def test_authentication_hash(self): username = random_string(8) password = random_string(12) password_hash = hashlib.new('md5', password.encode('utf-8')).hexdigest() self.server.auth_add_creds(username, password_hash, 'md5') self._test_authentication(username, password) password_hash = hashlib.new('md5', password.encode('utf-8')).digest() self.server.auth_add_creds(username, password_hash, 'md5') self._test_authentication(username, password)
def test_rpc_authentication(self): username = random_string(8) password = random_string(12) self.server.auth_add_creds(username, password) rpc = self.build_rpc_client() self.assertRaisesRegex(RPCError, r'the server responded with 401 \'Unauthorized\'', self.run_rpc_tests, rpc) rpc = self.build_rpc_client(username=username, password=random_string(12)) self.assertRaisesRegex(RPCError, r'the server responded with 401 \'Unauthorized\'', self.run_rpc_tests, rpc) rpc = self.build_rpc_client(username=username, password=password) self.run_rpc_tests(rpc)
def test_send_message_text(self): ws = self._get_ws() test_string = random_string(32) ws.send(test_string, websocket.ABNF.OPCODE_TEXT) resp = ws.recv() self.assertIsInstance(resp, str) self.assertEqual(test_string, resp) return
def test_build_from_config(self): config_section = random_string(8) config = ConfigParser() config.add_section(config_section) config.set(config_section, 'ip', '127.0.0.1') config.set(config_section, 'port', str(random.randint(30000, 50000))) server = build_server_from_config(config, config_section) self.assertIsInstance(server, AdvancedHTTPServer) server.shutdown()
def test_connection_keep_alive(self): headers = {'Connection': 'keep-alive'} response = self.http_request('/' + random_string(30), 'GET', headers=headers) self.assertHTTPStatus(response, 404) self.assertIsNotNone(self.http_connection.sock) response = self.http_request(self.test_resource, 'GET', headers=headers) self.assertHTTPStatus(response, 200) self.assertIsNotNone(self.http_connection.sock)
def test_text_fragmentation(self): ws = self._get_ws() string = random_string(32) ws.send_frame(websocket.ABNF.create_frame(string[:16], websocket.ABNF.OPCODE_TEXT, 0)) ws.send_frame(websocket.ABNF.create_frame(string[16:], websocket.ABNF.OPCODE_CONT, 1)) self.assertEqual(ws.recv(), string)
def test_send_without_cont(self): ws = self._get_ws() ws.send_frame(websocket.ABNF.create_frame(random_string(10), websocket.ABNF.OPCODE_TEXT, 0)) ws.send_frame(websocket.ABNF.create_frame(random_string(10), websocket.ABNF.OPCODE_TEXT, 0)) ws.recv() self.assertFalse(ws.connected)
def test_fake_resource(self): response = self.http_request('/' + random_string(30), 'GET') self.assertHTTPStatus(response, 404) response = self.http_request('/' + random_string(30), 'POST') self.assertHTTPStatus(response, 404)
def test_authentication_bad_credentials(self): self.server.auth_add_creds(random_string(8), random_string(12)) auth_headers = {'Authorization': 'Basic ' + base64.b64encode("{0}:{1}".format(random_string(8), random_string(12)).encode('utf-8')).decode('utf-8')} response = self.http_request(self.test_resource, 'GET', headers=auth_headers) self.assertHTTPStatus(response, 401)
def test_authentication_plain(self): username = random_string(8) password = random_string(12) self.server.auth_add_creds(username, password) self._test_authentication(username, password)