Пример #1
0
    def test_https_verify(self, mock_requests):
        url = 'https://127.0.0.1:8888'
        client = HTTPClient(url=url, verify=True)
        mock_result = MockResult()

        mock_result.text = 'foo bar ponies'
        mock_result.headers = {'Content-Type': 'text/html'}
        mock_result.status_code = 200

        mock_requests.request.return_value = mock_result
        client.run()

        self.assertTrue(client.verify)

        mock_requests.request.assert_called_with('GET',
                                                 url,
                                                 allow_redirects=False,
                                                 auth=None,
                                                 cookies=None,
                                                 data='',
                                                 files=None,
                                                 headers={},
                                                 params=None,
                                                 proxies=None,
                                                 timeout=60,
                                                 verify=True)
Пример #2
0
    def test_https_verify(self, mock_requests):
        url = 'https://127.0.0.1:8888'
        client = HTTPClient(url=url, verify=True)
        mock_result = MockResult()

        mock_result.text = 'foo bar ponies'
        mock_result.headers = {'Content-Type': 'text/html'}
        mock_result.status_code = 200

        mock_requests.request.return_value = mock_result
        client.run()

        self.assertTrue(client.verify)

        mock_requests.request.assert_called_with(
            'GET', url, allow_redirects=False, auth=None, cookies=None,
            data='', files=None, headers={}, params=None, proxies=None,
            timeout=60, verify=True)
Пример #3
0
    def test_parse_response_body(self, mock_requests):
        client = HTTPClient(url='http://127.0.0.1')
        mock_result = MockResult()

        # Unknown content type, body should be returned raw
        mock_result.text = 'foo bar ponies'
        mock_result.headers = {'Content-Type': 'text/html'}
        mock_result.status_code = 200

        mock_requests.request.return_value = mock_result
        result = client.run()

        self.assertEqual(result['body'], mock_result.text)
        self.assertEqual(result['status_code'], mock_result.status_code)
        self.assertEqual(result['headers'], mock_result.headers)

        # Unknown content type, JSON body
        mock_result.text = '{"test1": "val1"}'
        mock_result.headers = {'Content-Type': 'text/html'}

        mock_requests.request.return_value = mock_result
        result = client.run()

        self.assertEqual(result['body'], mock_result.text)

        # JSON content-type and JSON body
        mock_result.text = '{"test1": "val1"}'
        mock_result.headers = {'Content-Type': 'application/json'}

        mock_requests.request.return_value = mock_result
        result = client.run()

        self.assertTrue(isinstance(result['body'], dict))
        self.assertEqual(result['body'], {'test1': 'val1'})

        # JSON content-type with charset and JSON body
        mock_result.text = '{"test1": "val1"}'
        mock_result.headers = {
            'Content-Type': 'application/json; charset=UTF-8'
        }

        mock_requests.request.return_value = mock_result
        result = client.run()

        self.assertTrue(isinstance(result['body'], dict))
        self.assertEqual(result['body'], {'test1': 'val1'})

        # JSON content-type and invalid json body
        mock_result.text = 'not json'
        mock_result.headers = {'Content-Type': 'application/json'}

        mock_requests.request.return_value = mock_result
        result = client.run()

        self.assertFalse(isinstance(result['body'], dict))
        self.assertEqual(result['body'], mock_result.text)
Пример #4
0
    def test_parse_response_body(self, mock_requests):
        client = HTTPClient(url='http://127.0.0.1')
        mock_result = MockResult()

        # Unknown content type, body should be returned raw
        mock_result.text = 'foo bar ponies'
        mock_result.headers = {'Content-Type': 'text/html'}
        mock_result.status_code = 200

        mock_requests.request.return_value = mock_result
        result = client.run()

        self.assertEqual(result['body'], mock_result.text)
        self.assertEqual(result['status_code'], mock_result.status_code)
        self.assertEqual(result['headers'], mock_result.headers)

        # Unknown content type, JSON body
        mock_result.text = '{"test1": "val1"}'
        mock_result.headers = {'Content-Type': 'text/html'}

        mock_requests.request.return_value = mock_result
        result = client.run()

        self.assertEqual(result['body'], mock_result.text)

        # JSON content-type and JSON body
        mock_result.text = '{"test1": "val1"}'
        mock_result.headers = {'Content-Type': 'application/json'}

        mock_requests.request.return_value = mock_result
        result = client.run()

        self.assertTrue(isinstance(result['body'], dict))
        self.assertEqual(result['body'], {'test1': 'val1'})

        # JSON content-type with charset and JSON body
        mock_result.text = '{"test1": "val1"}'
        mock_result.headers = {'Content-Type': 'application/json; charset=UTF-8'}

        mock_requests.request.return_value = mock_result
        result = client.run()

        self.assertTrue(isinstance(result['body'], dict))
        self.assertEqual(result['body'], {'test1': 'val1'})

        # JSON content-type and invalid json body
        mock_result.text = 'not json'
        mock_result.headers = {'Content-Type': 'application/json'}

        mock_requests.request.return_value = mock_result
        result = client.run()

        self.assertFalse(isinstance(result['body'], dict))
        self.assertEqual(result['body'], mock_result.text)