Exemplo n.º 1
0
    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'))
Exemplo n.º 2
0
    def set_stub(self, url, request_method='GET',
                 response_function=None, response_file=None, response_body='',
                 response_code=200, response_headers=None,
                 response_body_processor=safe_template, **kwargs):

        set_stub(
            self._app.http_client_factory.tornado_http_client, url, request_method,
            response_function, response_file, response_body, response_code, response_headers,
            response_body_processor, **kwargs
        )
Exemplo n.º 3
0
    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 : ')
Exemplo n.º 4
0
    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')
Exemplo n.º 5
0
    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>')
Exemplo n.º 6
0
    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')
Exemplo n.º 7
0
    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"}')
Exemplo n.º 8
0
    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>')
Exemplo n.º 9
0
    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')
Exemplo n.º 10
0
    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')
Exemplo n.º 11
0
    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')