def test_default_client(self): """ When default_client is passed a client it should return that client. """ client = treq_HTTPClient(Agent(reactor)) assert_that(default_client(client, reactor), Is(client))
def default_client(client, reactor): """ Set up a default client if one is not provided. Set up the default ``twisted.web.client.Agent`` using the provided reactor. """ if client is None: from twisted.web.client import Agent client = treq_HTTPClient(Agent(reactor)) return client
def setUp(self): super(TestHTTPClientBase, self).setUp() self.requests = DeferredQueue() self.fake_server = FakeHttpServer(self.handle_request) fake_client = treq_HTTPClient(self.fake_server.get_agent()) self.client = self.get_client(fake_client) # Spin the reactor once at the end of each test to clean up any # cancelled deferreds self.addCleanup(wait0)
def test_request_fallback_all_failed(self): """ When we make a request and there are multiple Marathon endpoints specified, and all the endpoints fail, the last failure should be returned. """ agent = PerLocationAgent() agent.add_agent(b'localhost:8080', FailingAgent(RuntimeError('8080'))) agent.add_agent(b'localhost:9090', FailingAgent(RuntimeError('9090'))) client = MarathonClient( ['http://localhost:8080', 'http://localhost:9090'], client=treq_HTTPClient(agent)) d = self.cleanup_d(client.request('GET', path='/my-path')) yield wait0() self.assertThat(d, failed(WithErrorTypeAndMessage( RuntimeError, '9090'))) flush_logged_errors(RuntimeError)
def test_request_success(self): """ When we make a request and there are multiple Marathon endpoints specified, the first endpoint is used. """ agent = PerLocationAgent() agent.add_agent(b'localhost:8080', self.fake_server.get_agent()) agent.add_agent(b'localhost:9090', FailingAgent()) client = MarathonClient( ['http://localhost:8080', 'http://localhost:9090'], client=treq_HTTPClient(agent)) d = self.cleanup_d(client.request('GET', path='/my-path')) request = yield self.requests.get() self.assertThat(request, HasRequestProperties( method='GET', url='http://localhost:8080/my-path')) request.setResponseCode(200) request.finish() yield d