def test_session_from_environment(self): client = HttpClient() client.enable_cookies() client.post("http://something") spy = self.session_spy.attribute_spies["send"] args, kwargs = spy.call_history[-1] expect(kwargs).to(contain_key_with_value("proxies", {"http": self.http_proxy, "https": self.https_proxy}))
def test_session_from_call(self): expected = "an halibut" client = HttpClient() client.enable_cookies() client.delete("http://nothing-to.see/here", proxies=expected) spy = self.session_spy.attribute_spies["send"] args, kwargs = spy.call_history[-1] expect(kwargs).to(contain_key_with_value("proxies", expected))
def test_sends_prepared_request(self): client = HttpClient() client.enable_cookies() client.post("http://pewpewpew") prep_spy = self.extract_prep_spy() prepped = prep_spy.return_value_spies[-1] send_spy = self.extract_send_spy() args, kwargs = send_spy.call_history[-1] expect(args[0]).to(be(prepped))
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_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_returns_response(self): client = HttpClient() client.enable_cookies() response = client.head("http://pewpewpew") expect(response).to( equal(self.extract_send_spy().return_value_spies[-1]))
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 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))
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_method(self): client = HttpClient() client.enable_cookies() client.delete("http://vikings") expect(self.extract_request_to_prep().method).to(equal("DELETE"))
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 check_disables_redirect(self, method): client = HttpClient() client.enable_cookies() getattr(client, method)("http://something") expect(self.send_spy["allow_redirects"]).to(be(False))