def test_header_appears_in_next_request(self): key = "x-spam" value = "eggs" sut = HttpClient() sut.set_persistent_headers(**{key: value}) sut.get("http://spam") expect(self.extract_request_headers()).to(contain_key_with_value(key, value))
def test_setting_header_to_none_removes_it(self): key = "something" sut = HttpClient() sut.set_persistent_headers(**{key: "spam"}) sut.set_persistent_headers(**{key: None}) sut.get("http://albatross") expect(self.extract_request_headers().keys()).not_to(contain(key))
def test_specified_header_overrides_persistent(self): key = "page" value = "LIX" sut = HttpClient() sut.set_persistent_headers(**{key: "something-else"}) sut.get("http://yadda", headers={key: value}) expect(self.extract_request_headers()).to(contain_key_with_value(key, value))
def test_can_change_a_persistent_header(self): key = "thing" value = "new-value" sut = HttpClient() sut.set_persistent_headers(**{key: "old-value"}) sut.set_persistent_headers(**{key: value}) sut.get("http://things") expect(self.extract_request_headers()).to(contain_key_with_value(key, value))
def test_first_header_persists_after_adding_second(self): key = "x-spam" value = "eggs" sut = HttpClient() sut.set_persistent_headers(**{key: value}) sut.set_persistent_headers(something="13") sut.get("http://spam") expect(self.extract_request_headers()).to(contain_key_with_value(key, value))
def test_publish_with_test_name_on_test_failed(self): sut = HttpClient() sut.get("http://spamoni.io") name = "nombre" EventBroker.publish(event=TestEvent.test_failed, test_name=name, exception=RuntimeError()) assert self.published, "Nothing was published" expect(self.published).to(contain_key_with_value("test_name", name))
def test_publish_with_suite_name_on_suite_erred(self): sut = HttpClient() sut.get("http://spamoni.io") name = "bubba" EventBroker.publish(event=TestEvent.suite_erred, suite_name=name, exception=RuntimeError()) assert self.published, "Nothing was published" expect(self.published).to(contain_key_with_value("suite_name", name))
def test_request_headers(self): headers = {"X-yz": "yogurt humphrey", "Content-length": "infinite"} sut = HttpClient() sut.get("http://something", headers=headers) EventBroker.publish(event=TestEvent.test_failed, exception=RuntimeError()) expect(self.published["artifact"]).to( contain("\n".join(["%s: %s" % (k, v) for k, v in headers.items()])))
def test_enabled_in_session_by_default(self): requests_stub = EndlessFake() self.context.inject(requests, requests_stub) spy = MasterSpy(EndlessFake()) requests_stub.Session = lambda *a, **k: spy client = HttpClient() client.enable_cookies() client.get("http://spam") args, kwargs = spy.last_call_to("send") expect(kwargs).to(have_keys(verify=True))
def test_passes_exception_as_kwarg(self): exception_class = HttpImATeapot spy = FunctionSpy(return_value=EndlessFake()) client = HttpClient() client.set_exceptional_response_callback( exception_class=exception_class, callback=spy) self.context.inject(inspect_response, func_that_raises(exception_class())) client.get("http://foo.bar") expect(spy["exception"]).to(be_a(exception_class))
def test_response_headers(self): headers = {"X-yz": "Sam the spammer", "Content-length": "2"} self.fake_requests.response.headers = headers sut = HttpClient() sut.get("http://something") EventBroker.publish(event=TestEvent.test_failed, exception=RuntimeError()) expect(self.published["artifact"]).to( contain("\n".join(["%s: %s" % (k, v) for k, v in headers.items()])))
def test_enabling_cookies_does_not_break_this_feature(self): key = "spam" value = "eggs" sut = HttpClient() sut.enable_cookies() sut.set_persistent_headers(**{key: value}) sut.get("http://something") spy = self.spy.session_spy.attribute_spies["prepare_request"] args, kwargs = spy.call_history[-1] request = args[0] expect(request.headers).to(contain_key_with_value(key, value))
def test_sends_configured_timeout_for_session_request(self): planted = 37 self.context.set_env(HTTP_CLIENT_SOCKET_TIMEOUT=planted) session_stub = EndlessFake() self.requests_stub.Session = lambda: session_stub send_spy = FunctionSpy(return_value=EndlessFake()) session_stub.send = send_spy client = HttpClient() client.enable_cookies() client.get("http://truffles") expect(send_spy["timeout"]).to(equal(planted))
def test_disabled_in_session_when_configured(self): self.context.set_env(HTTPS_VERIFY_CERTS="FAlSe") requests_stub = EndlessFake() self.context.inject(requests, requests_stub) spy = MasterSpy(EndlessFake()) requests_stub.Session = lambda *a, **k: spy client = HttpClient() client.enable_cookies() client.get("http://spam") args, kwargs = spy.last_call_to("send") expect(kwargs).to(have_keys(verify=False))
def test_calls_callback_for_child_class(self): specified_class = Spam raised_class = SonOfSpam spy = FunctionSpy() client = HttpClient() client.set_exceptional_response_callback( exception_class=specified_class, callback=spy) self.context.inject(inspect_response, func_that_raises(raised_class())) try: client.get("http://stuffed.com.au") except raised_class: # Exception should not be raised, but this test does not care pass spy.assert_was_called()
def test_calls_callback_for_exception(self): exception_class = HttpUnauthorized spy = FunctionSpy() client = HttpClient() client.set_exceptional_response_callback( exception_class=HttpUnauthorized, callback=spy) self.context.inject(inspect_response, func_that_raises(exception_class())) try: client.get("http://spam") except exception_class: # This shouldn't be raised but this test doesn't care pass spy.assert_was_called()
def test_returns_response_returned_by_callback(self): callback_response = EndlessFake() self.context.inject(inspect_response, func_that_raises(HttpNotFound())) client = HttpClient() client.set_exceptional_response_callback( exception_class=HttpNotFound, callback=lambda *a, **k: callback_response) expect(client.get("http://stuffed")).to(be(callback_response))
def test_prepares_request_with_given_data(self): client = HttpClient() client.enable_cookies() data = "yaddayaddayadda" client.get("http://menu", data=data) expect(self.extract_request_to_prep().data).to(equal(data))
def trigger_transcript(self): sut = HttpClient() sut.get("http://yaddayadda") EventBroker.publish(event=TestEvent.test_failed, exception=RuntimeError())
def test_also_sends_specified_headers(self): headers = {"spam": "eggs", "sausage": "beans", "x": "y"} sut = HttpClient() sut.set_persistent_headers(cardinal="ximinez", ordinal="biggles") sut.get("http://larch", headers=headers) expect(self.extract_request_headers()).to(contain_all_items_in(headers))
def test_does_not_handle_305(self): self.queue_response(status_code=305, location="http://some-proxy") self.queue_response(status_code=200) client = HttpClient() expect(lambda: client.get("http://something")).to( complain(HttpUseProxy))
def test_complains_about_infinite_redirects(self): self.queue_response(status_code=301, location="http://strawberry-fields") client = HttpClient() expect(lambda: client.get("http://somewhere")).to( complain(TooManyRedirects))
def test_can_set_multiple_headers_in_one_call(self): headers = {"spam": "eggs", "sausage": "beans", "x": "y"} sut = HttpClient() sut.set_persistent_headers(**headers) sut.get("http://parrot") expect(self.extract_request_headers()).to(contain_all_items_in(headers))
def test_complains_if_location_header_is_empty(self): self.queue_response(status_code=301, location="") self.queue_response(status_code=200) client = HttpClient() expect(lambda: client.get("http://it")).to( complain(InvalidHttpResponse))
def test_maintains_session_between_requests(self): client = HttpClient() client.enable_cookies() client.get("http://spam") client.get("http://eggs") expect(self.session_class_spy.call_history).to(have_length(1))
def test_setting_non_existent_header_to_none_has_no_effect(self): key = "yadda" sut = HttpClient() sut.set_persistent_headers(**{key: None}) sut.get("http://albatross") expect(self.extract_request_headers().keys()).not_to(contain(key))
def test_prepares_request_with_given_url(self): client = HttpClient() client.enable_cookies() url = "https://stuffed.io?first=who" client.get(url) expect(self.extract_request_to_prep().url).to(equal(url))
def test_prepares_request_with_given_headers(self): client = HttpClient() client.enable_cookies() headers = {"spam": "lovely", "baked-beans": "off"} client.get("http://menu", headers=headers) expect(self.extract_request_to_prep().headers).to(equal(headers))