def test_bad_response(self): response = httplib2.Response({ 'status': '500', 'content-type': 'text/plain', }) content = """ o/` an error occurred o/` """ self.body, self.headers = None, None bat = BatchClient(endpoint='http://127.0.0.1:8000/batch-processor') m = mox.Mox() m.StubOutWithMock(bat, 'request') bat.request( 'http://127.0.0.1:8000/batch-processor', method='POST', headers=self.mocksetter('headers'), body=self.mocksetter('body'), ).AndReturn((response, content)) bat.cache = None bat.authorizations = [] m.ReplayAll() def callback(url, subresponse, subcontent): self.subresponse = subresponse self.subcontent = subcontent bat.batch_request() bat.batch({'uri': 'http://example.com/moose'}, callback=callback) self.assertRaises(NonBatchResponseError, bat.complete_batch) m.VerifyAll()
def test_multi(self): response = httplib2.Response({ 'status': '207', 'content-type': 'multipart/parallel; boundary="foomfoomfoom"', }) content = """wah-ho, wah-hay --foomfoomfoom Content-Type: application/http-response Multipart-Request-ID: 2 200 OK Content-Type: application/json {"name": "drang"} --foomfoomfoom Content-Type: application/http-response Multipart-Request-ID: 1 200 OK Content-Type: application/json {"name": "sturm"} --foomfoomfoom--""" self.headers, self.body = None, None bat = BatchClient(endpoint="http://127.0.0.1:8000/") m = mox.Mox() m.StubOutWithMock(bat, 'request') bat.request( 'http://127.0.0.1:8000/batch-processor', method='POST', headers=self.mocksetter('headers'), body=self.mocksetter('body'), ).AndReturn((response, content)) bat.cache = None bat.authorizations = [] m.ReplayAll() def callbackMoose(url, subresponse, subcontent): self.subresponseMoose = subresponse self.subcontentMoose = subcontent def callbackFred(url, subresponse, subcontent): self.subresponseFred = subresponse self.subcontentFred = subcontent bat.batch_request() bat.batch({'uri': 'http://example.com/moose'}, callbackMoose) bat.batch({'uri': 'http://example.com/fred'}, callbackFred) bat.complete_batch() self.assertEquals(self.subcontentMoose, '{"name": "sturm"}') self.assertEquals(self.subcontentFred, '{"name": "drang"}') m.VerifyAll()
def test_with(self): response = httplib2.Response({ 'status': '207', 'content-type': 'multipart/parallel; boundary="=={{[[ ASFDASF ]]}}=="', }) content = """OMG HAI --=={{[[ ASFDASF ]]}}== Content-Type: application/http-response Multipart-Request-ID: 1 200 OK Content-Type: application/json {"name": "Potatoshop"} --=={{[[ ASFDASF ]]}}==--""" self.body, self.headers = None, None bat = BatchClient(endpoint='http://127.0.0.1:8000/batch-processor') m = mox.Mox() m.StubOutWithMock(bat, 'request') bat.request( 'http://127.0.0.1:8000/batch-processor', method='POST', headers=self.mocksetter('headers'), body=self.mocksetter('body'), ).AndReturn((response, content)) bat.cache = None bat.authorizations = [] m.ReplayAll() def callback(url, subresponse, subcontent): self.subresponseWith = subresponse self.subcontentWith = subcontent # Try using "with" syntax here. with bat.batch_request() as request: self.assert_(request is bat.batchrequest) bat.batch({'uri': 'http://example.com/moose'}, callback=callback) # Make sure the request happened. m.VerifyAll() self.assert_(hasattr(self, 'subresponseWith')) self.assert_(hasattr(self, 'subcontentWith'))
def test_least(self): response = httplib2.Response({ 'status': '207', 'content-type': 'multipart/parallel; boundary="=={{[[ ASFDASF ]]}}=="', }) content = """OMG HAI --=={{[[ ASFDASF ]]}}== Content-Type: application/http-response Multipart-Request-ID: 1 200 OK Content-Type: application/json {"name": "Potatoshop"} --=={{[[ ASFDASF ]]}}==--""" self.body, self.headers = None, None bat = BatchClient(endpoint='http://127.0.0.1:8000/batch-processor') m = mox.Mox() m.StubOutWithMock(bat, 'request') bat.request( 'http://127.0.0.1:8000/batch-processor', method='POST', headers=self.mocksetter('headers'), body=self.mocksetter('body'), ).AndReturn((response, content)) bat.cache = None bat.authorizations = [] m.ReplayAll() def callback(url, subresponse, subcontent): self.subresponse = subresponse self.subcontent = subcontent bat.batch_request() bat.batch({'uri': 'http://example.com/moose'}, callback=callback) bat.complete_batch() m.VerifyAll() self.assert_(self.headers is not None) headers = sorted([h.lower() for h in self.headers.keys()]) self.assertEquals(headers, ['accept-encoding', 'content-type', 'mime-version']) self.assertEquals(self.headers['MIME-Version'], '1.0') # Parse the headers through email.message to test the Content-Type value. mess = message.Message() for header, value in self.headers.iteritems(): mess[header] = value self.assertEquals(mess.get_content_type(), 'multipart/parallel') boundary = mess.get_param('boundary') self.assert_(boundary) # Check that the multipart request we sent was composed correctly. preamble, subresponse, postamble = self.body.split('--%s' % (boundary,)) self.assert_(None not in (preamble, subresponse, postamble)) # Trim leading \n left over from the boundary. self.assert_(subresponse.startswith('\n')) subresponse = subresponse[1:] subresp_msg = email.message_from_string(subresponse) self.assertEquals(subresp_msg.get_content_type(), 'application/http-request') self.assert_('Multipart-Request-ID' in subresp_msg) self.assertEquals(self.subcontent, '{"name": "Potatoshop"}')
def test_cacheful(self): response = { 'content-type': 'multipart/parallel; boundary="=={{[[ ASFDASF ]]}}=="', } content = """OMG HAI --=={{[[ ASFDASF ]]}}== Content-Type: application/http-response Multipart-Request-ID: 1 304 Not Modified Content-Type: application/json Etag: 7 --=={{[[ ASFDASF ]]}}==--""" self.body, self.headers = None, None bat = BatchClient(endpoint="http://127.0.0.1:8000/") m = mox.Mox() mr = m.CreateMock(httplib.HTTPResponse) mr.read().AndReturn(content) mr.getheaders().AndReturn(response.iteritems()) mr.status = 207 mr.reason = 'Multi-Status' mr.version = 'HTTP/1.1' mc = m.CreateMock(httplib.HTTPConnection) mc.request('POST', '/batch-processor', self.mocksetter('body'), self.mocksetter('headers')) mc.getresponse().AndReturn(mr) bat.connections = {'http:127.0.0.1:8000': mc} bat.cache = m.CreateMock(httplib2.FileCache) bat.cache.get('http://example.com/moose').AndReturn("""status: 200\r content-type: application/json\r content-location: http://example.com/moose\r etag: 7\r \r {"name": "Potatoshop"}""") bat.cache.get('http://127.0.0.1:8000/batch-processor').AndReturn(None) bat.cache.delete('http://127.0.0.1:8000/batch-processor') bat.cache.get('http://example.com/moose').AndReturn("""status: 200\r content-type: application/json\r content-location: http://example.com/moose\r etag: 7\r \r {"name": "Potatoshop"}""") bat.cache.set('http://example.com/moose', """status: 304\r etag: 7\r content-type: application/json\r content-location: http://example.com/moose\r \r {"name": "Potatoshop"}""") m.ReplayAll() def callback(url, subresponse, subcontent): self.subresponse = subresponse self.subcontent = subcontent self.assert_(bat.cache) bat.batch_request() bat.batch({'uri': 'http://example.com/moose'}, callback) bat.complete_batch() m.VerifyAll() # We captured headers after httplib2 processed them for once, so # they're already lowercase and there's a User-Agent header. headers = sorted([h for h in self.headers.keys()]) self.assertEquals(headers, ['accept-encoding', 'content-type', 'mime-version', 'user-agent']) self.assertEquals(self.headers['mime-version'], '1.0') self.assertEquals(self.subcontent, '{"name": "Potatoshop"}')
def test_not_found(self): response = httplib2.Response({ 'status': '207', 'content-type': 'multipart/parallel; boundary="foomfoomfoom"', }) content = """wah-ho, wah-hay --foomfoomfoom Content-Type: application/http-response Multipart-Request-ID: 2 200 OK Content-Type: application/json {"name": "drang"} --foomfoomfoom Content-Type: application/http-response Multipart-Request-ID: 1 404 Not Found Content-Type: application/json {"oops": null} --foomfoomfoom--""" self.headers, self.body = None, None bat = BatchClient(endpoint="http://127.0.0.1:8000/") m = mox.Mox() m.StubOutWithMock(bat, 'request') bat.request( 'http://127.0.0.1:8000/batch-processor', method='POST', headers=self.mocksetter('headers'), body=self.mocksetter('body'), ).AndReturn((response, content)) bat.cache = None bat.authorizations = [] m.ReplayAll() def callback_moose(url, subresponse, subcontent): self.subresponseMoose = subresponse self.subcontentMoose = subcontent # We might convert an errorful subresponse into an exception in # a callback, so check that exceptions that are thrown from the # callback percolate out. raise httplib.HTTPException('404 Not Found') def callback_fred(url, subresponse, subcontent): self.subresponseFred = subresponse self.subcontentFred = subcontent bat.batch_request() bat.batch({'uri': 'http://example.com/moose'}, callback_moose) bat.batch({'uri': 'http://example.com/fred'}, callback_fred) self.assertRaises(httplib.HTTPException, lambda: bat.complete_batch() ) self.assertEquals(self.subresponseMoose.status, 404) self.assertEquals(self.subcontentMoose, '{"oops": null}') # Does fred still exist? Should it? self.assertEquals(self.subcontentFred, '{"name": "drang"}') m.VerifyAll()