Example #1
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'})
Example #2
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')
Example #3
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())
Example #4
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))
Example #5
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)
Example #6
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')
Example #7
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))
Example #8
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'})
Example #9
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'
        )
Example #10
0
    def test_get_file_sets_no_name_when_response_does_not_provide_header(self):
        self.setup_responses(method=responses.GET,
                             url='https://retdec.com/service/api',
                             body='data',
                             stream=True)
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        file = conn.get_file()

        self.assertIsNone(file.name)
Example #11
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()
Example #12
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'
        )
Example #13
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()
        )
Example #14
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:"
        )
Example #15
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:"
        )
Example #16
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()
Example #17
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)
Example #18
0
    def test_get_file_sets_no_name_when_response_header_does_not_contain_name(
            self):
        self.setup_responses(method=responses.GET,
                             url='https://retdec.com/service/api',
                             body='data',
                             headers={'Content-Disposition': 'attachment'},
                             stream=True)
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        file = conn.get_file()

        self.assertIsNone(file.name)
Example #19
0
    def test_get_file_sets_no_name_when_response_does_not_provide_header(self):
        self.setup_responses(
            method=responses.GET,
            url='https://retdec.com/service/api',
            body='data',
            stream=True
        )
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        file = conn.get_file()

        self.assertIsNone(file.name)
Example #20
0
    def test_get_file_sends_get_request(self):
        self.setup_responses(
            method=responses.GET,
            url='https://retdec.com/service/api',
            adding_headers={'Content-Disposition': 'filename=test.c'},
            stream=True)
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        conn.get_file()

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

        files = {'input': ('test.c', io.StringIO('main()'))}
        conn.send_post_request(files=files)

        body = str(responses.calls[0].request.body)
        self.assertIn(
            'Content-Disposition: form-data; name="input"; filename="test.c"',
            body)
        self.assertIn('main()', body)
Example #22
0
    def test_get_file_returns_file_with_correct_name_when_header_has_quotes(
            self):
        self.setup_responses(
            method=responses.GET,
            url='https://retdec.com/service/api',
            body='data',
            headers={'Content-Disposition': 'attachment; filename="test.c"'},
            stream=True)
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        file = conn.get_file()

        self.assertEqual(file.name, 'test.c')
Example #23
0
    def test_get_file_sets_no_name_when_response_header_does_not_contain_name(self):
        self.setup_responses(
            method=responses.GET,
            url='https://retdec.com/service/api',
            body='data',
            adding_headers={'Content-Disposition': 'attachment'},
            stream=True
        )
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        file = conn.get_file()

        self.assertIsNone(file.name)
Example #24
0
    def test_get_file_sends_get_request(self):
        self.setup_responses(
            method=responses.GET,
            url='https://retdec.com/service/api',
            adding_headers={'Content-Disposition': 'filename=test.c'},
            stream=True
        )
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        conn.get_file()

        self.assertEqual(len(responses.calls), 1)
        self.assertEqual(responses.calls[0].request.method, responses.GET)
Example #25
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.')
Example #26
0
    def test_get_file_returns_file_with_correct_name_when_header_has_quotes(self):
        self.setup_responses(
            method=responses.GET,
            url='https://retdec.com/service/api',
            body='data',
            adding_headers={
                'Content-Disposition': 'attachment; filename="test.c"'
            },
            stream=True
        )
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        file = conn.get_file()

        self.assertEqual(file.name, 'test.c')
Example #27
0
    def test_repr_returns_correct_value(self):
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        self.assertEqual(
            repr(conn),
            "<retdec.conn.APIConnection base_url='https://retdec.com/service/api'>"
        )
Example #28
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.')
Example #29
0
    def test_send_post_request_includes_given_files(self):
        self.setup_responses(
            method=responses.POST,
            url='https://retdec.com/service/api'
        )
        conn = APIConnection('https://retdec.com/service/api', 'KEY')

        files = {'input': ('test.c', io.StringIO('main()'))}
        conn.send_post_request(files=files)

        body = str(responses.calls[0].request.body)
        self.assertIn(
            'Content-Disposition: form-data; name="input"; filename="test.c"',
            body
        )
        self.assertIn('main()', body)
Example #30
0
    def _create_new_api_connection(self, path):
        """Creates a new API connection from the given path.

        :param str path: Path that is appended after the API URL.
        """
        return APIConnection(self.api_url + path, self.api_key)