示例#1
0
    def test_send_get_request_includes_given_parameters(self):
        self.setup_responses(url='https://retdec.com/service/api')
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        conn.send_get_request(params={'key': 'value'})

        self.assertEqual(responses.calls[0].request.url,
                         'https://retdec.com/service/api?key=value')
示例#2
0
    def test_send_get_request_sends_correct_user_agent_header(self):
        self.setup_responses(url='https://retdec.com/service/api')
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        conn.send_get_request()

        self.assertEqual(responses.calls[0].request.headers['User-Agent'],
                         'retdec-python/' + platform.system())
示例#3
0
    def test_send_get_request_sends_get_request(self):
        self.setup_responses(method=responses.GET,
                             url='https://retdec.com/service/api')
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        conn.send_get_request()

        self.assertEqual(len(responses.calls), 1)
        self.assertEqual(responses.calls[0].request.method, responses.GET)
示例#4
0
    def test_send_get_request_sends_request_to_path_when_path_is_nonempty(
            self):
        self.setup_responses(url='https://retdec.com/service/api/decompiler')
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        conn.send_get_request(path='/decompiler')

        self.assertEqual(responses.calls[0].request.url,
                         'https://retdec.com/service/api/decompiler')
示例#5
0
    def test_send_get_request_raises_exception_when_there_is_connection_error(
            self, requests_session):
        requests_session.side_effect = requests.exceptions.ConnectionError(
            'Connection refused.')
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        with self.assertRaises(ConnectionError) as cm:
            conn.send_get_request()
        self.assertIn('Connection refused.', str(cm.exception))
示例#6
0
    def test_send_get_request_sends_correct_user_agent_header(self):
        self.setup_responses(url='https://retdec.com/service/api')
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        conn.send_get_request()

        self.assertEqual(
            responses.calls[0].request.headers['User-Agent'],
            'retdec-python/' + platform.system()
        )
示例#7
0
    def test_send_get_request_includes_given_parameters(self):
        self.setup_responses(url='https://retdec.com/service/api')
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        conn.send_get_request(params={'key': 'value'})

        self.assertEqual(
            responses.calls[0].request.url,
            'https://retdec.com/service/api?key=value'
        )
示例#8
0
    def test_send_get_request_sends_request_to_path_when_path_is_nonempty(self):
        self.setup_responses(url='https://retdec.com/service/api/decompiler')
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        conn.send_get_request(path='/decompiler')

        self.assertEqual(
            responses.calls[0].request.url,
            'https://retdec.com/service/api/decompiler'
        )
示例#9
0
    def test_send_get_request_raises_exception_when_there_is_connection_error(
            self, requests_session):
        requests_session.side_effect = requests.exceptions.ConnectionError(
            'Connection refused.'
        )
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        with self.assertRaises(ConnectionError) as cm:
            conn.send_get_request()
        self.assertIn('Connection refused.', str(cm.exception))
示例#10
0
    def test_send_get_request_raises_exception_when_authentication_fails(self):
        self.setup_responses(
            url='https://retdec.com/service/api',
            status=401,
            body='{"code": 401, "message": "failure", "description": "auth failed"}',
        )
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        with self.assertRaises(AuthenticationError):
            conn.send_get_request()
示例#11
0
    def test_send_get_request_sends_correct_authentication_header(self):
        self.setup_responses(url='https://retdec.com/service/api')
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        conn.send_get_request()

        # http://en.wikipedia.org/wiki/Basic_access_authentication#Client_side
        self.assertEqual(
            responses.calls[0].request.headers['Authorization'],
            'Basic S0VZOg=='  # base64-encoded string "KEY:"
        )
示例#12
0
    def test_send_get_request_sends_get_request(self):
        self.setup_responses(
            method=responses.GET,
            url='https://retdec.com/service/api'
        )
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        conn.send_get_request()

        self.assertEqual(len(responses.calls), 1)
        self.assertEqual(responses.calls[0].request.method, responses.GET)
示例#13
0
    def test_send_get_request_sends_correct_authentication_header(self):
        self.setup_responses(url='https://retdec.com/service/api')
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        conn.send_get_request()

        # http://en.wikipedia.org/wiki/Basic_access_authentication#Client_side
        self.assertEqual(
            responses.calls[0].request.headers['Authorization'],
            'Basic S0VZOg=='  # base64-encoded string "KEY:"
        )
示例#14
0
    def test_send_get_request_raises_exception_when_authentication_fails(self):
        self.setup_responses(
            url='https://retdec.com/service/api',
            status=401,
            body=
            '{"code": 401, "message": "failure", "description": "auth failed"}',
        )
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        with self.assertRaises(AuthenticationError):
            conn.send_get_request()
示例#15
0
    def test_send_get_request_raises_exception_when_api_returns_unknown_error(
            self):
        self.setup_responses(
            url='https://retdec.com/service/api',
            status=408,
            body=('{"code": 408, "message": "Request Timeout", '
                  '"description": "The request timeouted."}'),
        )
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        with self.assertRaises(UnknownAPIError) as cm:
            conn.send_get_request()
        self.assertEqual(cm.exception.code, 408)
        self.assertEqual(cm.exception.message, 'Request Timeout')
        self.assertEqual(cm.exception.description, 'The request timeouted.')
示例#16
0
    def test_send_get_request_raises_exception_when_api_returns_unknown_error(self):
        self.setup_responses(
            url='https://retdec.com/service/api',
            status=408,
            body=(
                '{"code": 408, "message": "Request Timeout", '
                '"description": "The request timeouted."}'
            ),
        )
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        with self.assertRaises(UnknownAPIError) as cm:
            conn.send_get_request()
        self.assertEqual(cm.exception.code, 408)
        self.assertEqual(cm.exception.message, 'Request Timeout')
        self.assertEqual(cm.exception.description, 'The request timeouted.')
示例#17
0
    def test_send_get_request_returns_json_body_when_request_succeeds(self):
        self.setup_responses(url='https://retdec.com/service/api',
                             body='{"key": "value"}')
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        response = conn.send_get_request()

        self.assertEqual(response, {'key': 'value'})
示例#18
0
    def test_send_get_request_returns_json_body_when_request_succeeds(self):
        self.setup_responses(
            url='https://retdec.com/service/api',
            body='{"key": "value"}'
        )
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        response = conn.send_get_request()

        self.assertEqual(response, {'key': 'value'})