def test_simple_request(self): client = gdata.client.GDClient() client.http_client = atom.mock_http_core.EchoHttpClient() response = client.request('GET', 'https://example.com/test') self.assertEqual(response.getheader('Echo-Host'), 'example.com:None') self.assertEqual(response.getheader('Echo-Uri'), '/test') self.assertEqual(response.getheader('Echo-Scheme'), 'https') self.assertEqual(response.getheader('Echo-Method'), 'GET') http_request = atom.http_core.HttpRequest( uri=atom.http_core.Uri(scheme='http', host='example.net', port=8080), method='POST', headers={'X': 1}) http_request.add_body_part('test', 'text/plain') response = client.request(http_request=http_request) self.assertEqual(response.getheader('Echo-Host'), 'example.net:8080') # A Uri with path set to None should default to /. self.assertEqual(response.getheader('Echo-Uri'), '/') self.assertEqual(response.getheader('Echo-Scheme'), 'http') self.assertEqual(response.getheader('Echo-Method'), 'POST') self.assertEqual(response.getheader('Content-Type'), 'text/plain') self.assertEqual(response.getheader('X'), '1') self.assertEqual(response.read(), 'test') # Use the same request object from above, but overwrite the request path # by passing in a URI. response = client.request(uri='/new/path?p=1', http_request=http_request) self.assertEqual(response.getheader('Echo-Host'), 'example.net:8080') self.assertEqual(response.getheader('Echo-Uri'), '/new/path?p=1') self.assertEqual(response.getheader('Echo-Scheme'), 'http') self.assertEqual(response.read(), 'test')
def test_simple_request(self): client = gdata.client.GDClient() client.http_client = atom.mock_http_core.EchoHttpClient() response = client.request("GET", "https://example.com/test") self.assertEqual(response.getheader("Echo-Host"), "example.com:None") self.assertEqual(response.getheader("Echo-Uri"), "/test") self.assertEqual(response.getheader("Echo-Scheme"), "https") self.assertEqual(response.getheader("Echo-Method"), "GET") http_request = atom.http_core.HttpRequest( uri=atom.http_core.Uri(scheme="http", host="example.net", port=8080), method="POST", headers={"X": 1} ) http_request.add_body_part("test", "text/plain") response = client.request(http_request=http_request) self.assertEqual(response.getheader("Echo-Host"), "example.net:8080") # A Uri with path set to None should default to /. self.assertEqual(response.getheader("Echo-Uri"), "/") self.assertEqual(response.getheader("Echo-Scheme"), "http") self.assertEqual(response.getheader("Echo-Method"), "POST") self.assertEqual(response.getheader("Content-Type"), "text/plain") self.assertEqual(response.getheader("X"), "1") self.assertEqual(response.read(), "test") # Use the same request object from above, but overwrite the request path # by passing in a URI. response = client.request(uri="/new/path?p=1", http_request=http_request) self.assertEqual(response.getheader("Echo-Host"), "example.net:8080") self.assertEqual(response.getheader("Echo-Uri"), "/new/path?p=1") self.assertEqual(response.getheader("Echo-Scheme"), "http") self.assertEqual(response.read(), "test")
def test_gdata_version_header(self): client = gdata.client.GDClient() client.http_client = atom.mock_http_core.EchoHttpClient() response = client.request('GET', 'http://example.com') self.assertEqual(response.getheader('GData-Version'), None) client.api_version = '2' response = client.request('GET', 'http://example.com') self.assertEqual(response.getheader('GData-Version'), '2')
def test_gdata_version_header(self): client = gdata.client.GDClient() client.http_client = atom.mock_http_core.EchoHttpClient() response = client.request("GET", "http://example.com") self.assertEqual(response.getheader("GData-Version"), None) client.api_version = "2" response = client.request("GET", "http://example.com") self.assertEqual(response.getheader("GData-Version"), "2")
def test_redirects(self): client = gdata.client.GDClient() client.http_client = atom.mock_http_core.MockHttpClient() # Add the redirect response for the initial request. first_request = atom.http_core.HttpRequest('http://example.com/1', 'POST') client.http_client.add_response(first_request, 302, None, {'Location': 'http://example.com/1?gsessionid=12'}) second_request = atom.http_core.HttpRequest( 'http://example.com/1?gsessionid=12', 'POST') client.http_client.AddResponse(second_request, 200, 'OK', body='Done') response = client.Request('POST', 'http://example.com/1') self.assertEqual(response.status, 200) self.assertEqual(response.reason, 'OK') self.assertEqual(response.read(), 'Done') redirect_loop_request = atom.http_core.HttpRequest( 'http://example.com/2?gsessionid=loop', 'PUT') client.http_client.add_response(redirect_loop_request, 302, None, {'Location': 'http://example.com/2?gsessionid=loop'}) try: response = client.request(method='PUT', uri='http://example.com/2?gsessionid=loop') self.fail('Loop URL should have redirected forever.') except gdata.client.RedirectError, err: self.assert_(str(err).startswith('Too many redirects from server'))
def patched_post(client, entry, uri, auth_token=None, converter=None, desired_class=None, **kwargs): if converter is None and desired_class is None: desired_class = entry.__class__ http_request = atom.http_core.HttpRequest() entry_string = entry.to_string(gdata.client.get_xml_version(client.api_version)) entry_string = entry_string.replace("ns1", "gd") # where the magic happens http_request.add_body_part(entry_string, "application/atom+xml") return client.request( method="POST", uri=uri, auth_token=auth_token, http_request=http_request, converter=converter, desired_class=desired_class, **kwargs )
def patched_post(client, entry, uri, auth_token=None, converter=None, desired_class=None, **kwargs): if converter is None and desired_class is None: desired_class = entry.__class__ http_request = atom.http_core.HttpRequest() entry_string = entry.to_string( gdata.client.get_xml_version(client.api_version)) entry_string = entry_string.replace('ns1', 'gd') # where the magic happens http_request.add_body_part(entry_string, 'application/atom+xml') return client.request(method='POST', uri=uri, auth_token=auth_token, http_request=http_request, converter=converter, desired_class=desired_class, **kwargs)
def test_redirects(self): client = gdata.client.GDClient() client.http_client = atom.mock_http_core.MockHttpClient() # Add the redirect response for the initial request. first_request = atom.http_core.HttpRequest("http://example.com/1", "POST") client.http_client.add_response(first_request, 302, None, {"Location": "http://example.com/1?gsessionid=12"}) second_request = atom.http_core.HttpRequest("http://example.com/1?gsessionid=12", "POST") client.http_client.AddResponse(second_request, 200, "OK", body="Done") response = client.Request("POST", "http://example.com/1") self.assertEqual(response.status, 200) self.assertEqual(response.reason, "OK") self.assertEqual(response.read(), "Done") redirect_loop_request = atom.http_core.HttpRequest("http://example.com/2?gsessionid=loop", "PUT") client.http_client.add_response( redirect_loop_request, 302, None, {"Location": "http://example.com/2?gsessionid=loop"} ) try: response = client.request(method="PUT", uri="http://example.com/2?gsessionid=loop") self.fail("Loop URL should have redirected forever.") except gdata.client.RedirectError, err: self.assert_(str(err).startswith("Too many redirects from server"))