Exemplo n.º 1
0
def mocked_requests_post(*args, **kwargs):
    class MockResponse:
        def __init__(self, json_data, status_code):
            self.json_data = json_data
            self.status_code = status_code

        def json(self):
            return self.json_data

    # case statements for different test URLs.
    # NOTE that these URLs are not real, only mocked!
    if args[0] == session.ApiUrl.CLASSIFY_BASE_URL + "/test_only_args":
        blob = load_json_fixture("response_classify_only_args.json")
        return MockResponse(blob["response"], 200)
    elif args[0] == "http://someotherurl.com/anothertest.json":
        blob = load_json_fixture("response_classify_only_args.json")

        return MockResponse({"key2": "value2"}, 200)

    return MockResponse(None, 404)
Exemplo n.º 2
0
    def test_fetch_timeout(self, mock_post):
        expected = load_json_fixture("response_classify_only_args.json")
        payload = expected["payload"]

        mock_resp = self._mock_response(
            status=408,
            json_data=None,
            raise_for_status=requests.Timeout("mocked timeout"),
        )
        mock_post.return_value = mock_resp

        with self.assertRaises(errors.NotResponding):
            _ = session.fetch(
                session.ApiUrl.CLASSIFY_BASE_URL + "/test_only_args", payload)
Exemplo n.º 3
0
    def test_fetch_bad_payload(self, mock_post):
        expected = load_json_fixture("response_classify_500_bad_payload.json")
        payload = expected["payload"]

        mock_resp = self._mock_response(
            status=expected["status_code"],
            json_data=expected["response"],
            raise_for_status=requests.HTTPError(
                "500 Server Error: INTERNAL SERVER ERROR for url: https://api.argumentsearch.com/en/classify"
            ),
        )
        mock_post.return_value = mock_resp

        with self.assertRaises(errors.InternalGatewayError):
            _ = session.fetch(
                session.ApiUrl.CLASSIFY_BASE_URL + "/test_only_args", payload)
Exemplo n.º 4
0
    def test_fetch_gateway_error(self, mock_post):
        expected = load_json_fixture("response_classify_gateway_error.json")
        payload = expected["payload"]
        print("response json")
        print(expected["response"])

        mock_resp = self._mock_response(
            status=expected["status_code"],
            json_data=expected["response"],
            raise_for_status=requests.HTTPError(
                "400 Client Error: BAD REQUEST for url: https://api.argumentsearch.com/en/classify"
            ),
        )
        mock_post.return_value = mock_resp

        with self.assertRaises(errors.ArgumenTextGatewayError):
            _ = session.fetch(
                session.ApiUrl.CLASSIFY_BASE_URL + "/test_only_args", payload)
Exemplo n.º 5
0
    def test_fetch_only_args(self, mock_post):
        expected = load_json_fixture("response_classify_only_args.json")
        payload = expected["payload"]
        mock_resp = self._mock_response(status=expected["status_code"],
                                        json_data=expected["response"])
        mock_post.return_value = mock_resp

        # Assert requests.post calls
        response = session.fetch(
            session.ApiUrl.CLASSIFY_BASE_URL + "/test_only_args", payload)
        self.assertEqual(response, expected["response"])

        # Assert that our mocked method was called with the right parameters
        print(mock_post.call_args_list)
        self.assertIn(
            mock.call(
                session.ApiUrl.CLASSIFY_BASE_URL + "/test_only_args",
                json=payload,
                timeout=session.DEFAULT_TIMEOUT,
            ),
            mock_post.call_args_list,
        )