示例#1
0
    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()
示例#2
0
    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()
示例#3
0
    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'))
示例#4
0
    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"}')
示例#5
0
    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()