def test_fetch_words(self): response_text = '<p>java text python from p python java text</p><div>text python run python</div>' app_http_client = self.application.http_client = AsyncHTTPClient( force_instance=True) patch_http_client(app_http_client) set_stub(app_http_client, 'http://example.com', response_body=response_text) post_data = {'url': 'http://example.com'} response = yield self.http_client.fetch(self.get_url('/word_counter/'), method='POST', body=urlencode(post_data)) self.application.http_client = AsyncHTTPClient() result = yield self.application.db.query( "SELECT * FROM words ORDER BY `count` DESC") expected_words = [ ('python', 4), ('text', 3), ('java', 2), ('run', 1), ] for i, word in enumerate(result): w = Word(word['word'], word['count']) self.assertEqual(expected_words[i][0], w.get_decrypted_word(self.application.encryption)) self.assertEqual(expected_words[i][1], w.count) for word in expected_words: self.assertIn(''.join([word[0]]), response.body.decode('utf-8'))
def test_missing_mock_fail_on_unknown_true(self): patch_http_client(self.app_http_client) set_stub(self.app_http_client, self.get_url('/simple_fetch?arg1=val1&arg2=val2&arg3=val3'), request_method='POST', response_body='GET MOCK') response = self.fetch('/simple_fetch') self.assertEqual(response.body, b'599 : ')
def test_mock_response_file_other(self): patch_http_client(self.app_http_client) set_stub(self.app_http_client, self.get_url('/simple_fetch'), request_method='POST', response_file='tests/response_stub.txt', response_code=401, data_tpl='MOCK DATA') response = self.fetch('/simple_fetch') self.assertEqual(response.body, b'401 : RESPONSE : MOCK DATA')
def test_mock_response_file_xml(self): patch_http_client(self.app_http_client) set_stub(self.app_http_client, self.get_url('/simple_fetch?arg1=val1&arg2=val2'), request_method='POST', response_file='tests/response_stub.xml', response_code=400) response = self.fetch('/simple_fetch') self.assertEqual( response.body, b'400 : application/xml : <response>$data_tpl</response>')
def test_mock(self): patch_http_client(self.app_http_client) def get_response(request): return get_response_stub(request, buffer='STUB2') set_stub(self.app_http_client, 'http://example.com/path', response_body='STUB1') set_stub(self.app_http_client, 'http://test.com/path', response_function=get_response) self.assertEqual(self.fetch('/test').body, b'STUB1STUB2')
def test_mock_response_file_json(self): patch_http_client(self.app_http_client) set_stub(self.app_http_client, self.get_url('/simple_fetch'), request_method='POST', response_file='tests/response_stub.json', response_code=400, data_tpl='MOCK DATA') response = self.fetch('/simple_fetch') self.assertEqual( response.body, b'400 : application/json : {"response": "MOCK DATA"}')
def test_mock_response_body_processor(self): patch_http_client(self.app_http_client) set_stub(self.app_http_client, self.get_url('/simple_fetch?arg1=$val_tpl'), request_method='POST', response_file='tests/response_stub.xml', response_code=400, val_tpl='val1', data_tpl='MOCK DATA') response = self.fetch('/simple_fetch') self.assertEqual( response.body, b'400 : application/xml : <response>MOCK DATA</response>')
def test_identical_mocks(self): patch_http_client(self.app_http_client) set_stub(self.app_http_client, self.get_url('/simple_fetch'), request_method='POST', response_body='FIRST') set_stub(self.app_http_client, self.get_url('/simple_fetch'), request_method='POST', response_body='SECOND') response = self.fetch('/simple_fetch') self.assertEqual(response.body, b'200 : SECOND')
def test_mock_response_body(self): patch_http_client(self.app_http_client) # Test that mock for GET request is not used set_stub(self.app_http_client, self.get_url('/simple_fetch?arg1=val1'), response_body='GET MOCK') set_stub(self.app_http_client, self.get_url('/simple_fetch?arg1=val1'), request_method='POST', response_body='POST MOCK', response_code=400, response_headers={'Content-Type': 'text/plain'}) response = self.fetch('/simple_fetch') self.assertEqual(response.body, b'400 : text/plain : POST MOCK')
def test_mock_response_function(self): patch_http_client(self.app_http_client) def _response_function(request): return get_response_stub(request, code=404, buffer='RESPONSE FUNCTION', headers={'Content-Type': 'text/plain'}) set_stub(self.app_http_client, self.get_url('/simple_fetch'), request_method='POST', response_function=_response_function) response = self.fetch('/simple_fetch') self.assertEqual(response.body, b'404 : text/plain : RESPONSE FUNCTION')
def patch_app_http_client(self, app): """Patches application HTTPClient to enable requests stubbing.""" patch_http_client(app.http_client)
def patch_app_http_client(self, app): """Patches application HTTPClient to enable requests stubbing.""" patch_http_client(app.http_client_factory.tornado_http_client)
def test_missing_mock_fail_on_unknown_false(self): patch_http_client(self.app_http_client, fail_on_unknown=False) response = self.fetch('/simple_fetch') self.assertEqual(response.body, b'200 : text/html : NO MOCK')
def test_not_matching_mock(self): patch_http_client(self.app_http_client) response = self.fetch('/simple_fetch') self.assertEqual(response.body, b'599 : ')