def testAllRequestMappers(self): """Test all_request_mappers method.""" configuration = service_handlers.ServiceHandlerFactory(Service) mapper1 = service_handlers.RPCMapper(['whatever'], 'whatever', None) mapper2 = service_handlers.RPCMapper(['whatever'], 'whatever', None) configuration.add_request_mapper(mapper1) self.assertEquals([mapper1], list(configuration.all_request_mappers())) configuration.add_request_mapper(mapper2) self.assertEquals([mapper1, mapper2], list(configuration.all_request_mappers()))
def testContentTypes_JustDefault(self): """Test content type attributes.""" self.mox.ReplayAll() mapper = service_handlers.RPCMapper(['GET', 'POST'], 'my-content-type', self.protocol) self.assertEquals(frozenset(['GET', 'POST']), mapper.http_methods) self.assertEquals('my-content-type', mapper.default_content_type) self.assertEquals(frozenset(['my-content-type']), mapper.content_types) self.mox.VerifyAll()
def testBuildRequest(self): """Test building a request.""" expected_request = Request1() self.protocol.decode_message(Request1, 'my body').AndReturn(expected_request) self.mox.ReplayAll() mapper = service_handlers.RPCMapper(['POST'], 'my-content-type', self.protocol) request = mapper.build_request(self.handler, Request1) self.assertTrue(expected_request is request)
def testBuildResponse(self): """Test building a response.""" response = Response1() self.protocol.encode_message(response).AndRaise( messages.ValidationError('xyz')) self.mox.ReplayAll() mapper = service_handlers.RPCMapper(['POST'], 'my-content-type', self.protocol) self.assertRaisesWithRegexpMatch(service_handlers.ResponseError, 'Unable to encode message: xyz', mapper.build_response, self.handler, response)
def testBuildResponse(self): """Test building a response.""" response = Response1() self.protocol.encode_message(response).AndReturn('encoded') self.mox.ReplayAll() mapper = service_handlers.RPCMapper(['POST'], 'my-content-type', self.protocol) request = mapper.build_response(self.handler, response) self.assertEquals('my-content-type', self.handler.response.headers['Content-Type']) self.assertEquals('encoded', self.handler.response.out.getvalue())
def testBuildRequest_DecodeError(self): """Test building a request generating a decode error.""" expected_request = Request1() self.protocol.decode_message(Request1, 'my body').AndRaise( messages.DecodeError('xyz')) self.mox.ReplayAll() mapper = service_handlers.RPCMapper(['POST'], 'my-content-type', self.protocol) self.assertRaisesWithRegexpMatch( service_handlers.RequestError, 'Unable to parse request content: xyz', mapper.build_request, self.handler, Request1)