def test_set_base_url(self) -> None: """ Tests of the method which let us communicate the base url. """ given_base_url = "https://example.org/api" expected = "https://example.org/api" requester = Requester() requester.set_base_url(given_base_url) actual = requester.base_url self.assertEqual(expected, actual)
def test_bind_endpoint_to_base_url_endpoint_not_string(self) -> None: """ Tests of the method which let us bind an endpoint with the previouly given base url for the case that the given endpoint is a non-string. """ given_base_url = "https://example.org/api" given_endpoint = True requester = Requester() requester.set_base_url(given_base_url) self.assertRaises( TypeError, lambda: requester.bind_endpoint_to_base_url(given_endpoint))
def test_set_base_url_ends_with_with_slash(self) -> None: """ Tests of the method which let us communicate the base url for the case that a slash is given at the end of the URL. """ given_base_url = "https://example.org/api/" expected = "https://example.org/api" requester = Requester() requester.set_base_url(given_base_url) actual = requester.base_url self.assertEqual(expected, actual)
def test_bind_endpoint_to_base_url(self) -> None: """ Tests of the method which let us bind an endpoint with the previously given base url. """ given_base_url = "https://example.org/api" given_endpoint = "hello/world" expected = "https://example.org/api/hello/world" requester = Requester() requester.set_base_url(given_base_url) actual = requester.bind_endpoint_to_base_url(given_endpoint) self.assertEqual(expected, actual)
def test_request_factory(self, mock_session_get): """ Tests of the request factory. """ given_base_url = "https://example.org/api/" given_endpoint = "hello/world" response = { "@type": "user", "@href": "/user/4549848944894848948949", "@representation": "standard", "@permissions": { "read": True, "sync": True }, "id": 4549848944894848948949, "login": "******", "name": "Foo Bar", "github_id": 1148942198798789784897949849484523106, "vcs_id": "1148942198798789784897949849484523106", "vcs_type": "GithubUser", "avatar_url": None, "education": False, "allow_migration": False, "email": "*****@*****.**", "is_syncing": False, "synced_at": "2020-10-14T14:53:08Z", "recently_signed_up": False, "secure_user_hash": None, } requester = Requester() requester.set_base_url(given_base_url) mock_session_get.return_value.headers = dict() mock_session_get.return_value.status_code = 200 mock_session_get.return_value.url = "https://example.org/api/hello/world" mock_session_get.return_value.json.return_value = response expected = response actual = requester.get(given_endpoint) self.assertEqual(expected, actual)
def test_request_factory_empty_esponse(self, mock_session_get): """ Tests of the request factory for the case that an empty response is given (back). """ given_base_url = "https://example.org/api/" given_endpoint = "hello/world" requester = Requester() requester.set_base_url(given_base_url) mock_session_get.return_value.status_code = 200 mock_session_get.return_value.headers = dict() mock_session_get.return_value.url = "https://example.org/api/hello/world" mock_session_get.return_value.text = "" mock_session_get.return_value.json.side_effect = json.decoder.JSONDecodeError( "hello", "world", 33) self.assertRaises(TravisCIError, lambda: requester.get(given_endpoint))
def test_request_factory_no_json_response(self, mock_session_get): """ Tests of the request factory for the case that no JSON is given as response. """ given_base_url = "https://example.org/api/" given_endpoint = "hello/world" response = { "@type": "user", "@href": "/user/4549848944894848948949", "@representation": "standard", "@permissions": { "read": True, "sync": True }, "id": 4549848944894848948949, "login": "******", "name": "Foo Bar", "github_id": 1148942198798789784897949849484523106, "vcs_id": "1148942198798789784897949849484523106", "vcs_type": "GithubUser", "avatar_url": None, "education": False, "allow_migration": False, "email": "*****@*****.**", "is_syncing": False, "synced_at": "2020-10-14T14:53:08Z", "recently_signed_up": False, "secure_user_hash": None, } requester = Requester() requester.set_base_url(given_base_url) mock_session_get.return_value.headers = dict() mock_session_get.return_value.status_code = 200 mock_session_get.return_value.url = "https://example.org/api/hello/world" mock_session_get.return_value.text = json.dumps(response) mock_session_get.return_value.json.side_effect = json.decoder.JSONDecodeError( "hello", "world", 33) self.assertRaises(TravisCIError, lambda: requester.get(given_endpoint))
def test_set_base_url_non_string(self) -> None: """ Tests of the method which let us communicate the base url for the case that a non-string is given. """ given = True requester = Requester() self.assertRaises(TypeError, lambda: requester.set_base_url(given))