def set_cookie(self, response): try: _cookie = response.headers["set-cookie"] logger.debug("RECEIVED COOKIE") try: # add received cookies to the cookie jar set_cookie(self.cookiejar, SimpleCookie(_cookie)) except CookieError as err: logger.error(err) raise NonFatalException(response, "{}".format(err)) except (AttributeError, KeyError) as err: pass
def __call__(self, url, method="GET", **kwargs): """ Send a HTTP request to a URL using a specified method :param url: The URL to access :param method: The method to use (GET, POST, ..) :param kwargs: extra HTTP request parameters :return: A Response """ # copy the default set before starting to modify it. _kwargs = copy.copy(self.request_args) if kwargs: _kwargs.update(kwargs) # If I have cookies add them all to the request if self.cookiejar: _kwargs["cookies"] = self._cookies() logger.debug("SENT {} COOKIES".format(len(_kwargs["cookies"]))) # If I want to modify the request arguments based on URL, method # and current arguments I can use this call back function. if self.req_callback is not None: _kwargs = self.req_callback(method, url, **_kwargs) try: # Do the request r = requests.request(method, url, **_kwargs) except Exception as err: logger.error( "http_request failed: %s, url: %s, htargs: %s, method: %s" % ( err, url, sanitize(_kwargs), method)) raise if self.events is not None: self.events.store('HTTP response', r, ref=url) try: _cookie = r.headers["set-cookie"] logger.debug("RECEIVED COOKIE") try: # add received cookies to the cookie jar set_cookie(self.cookiejar, SimpleCookie(_cookie)) except CookieError as err: logger.error(err) raise NonFatalException(r, "{}".format(err)) except (AttributeError, KeyError) as err: pass # return the response return r
def test_cookie(cookie_dealer): cookie_value = "Something to pass along" cookie_typ = "sso" cookie_name = "Foobar" kaka = cookie_dealer.create_cookie(cookie_value, cookie_typ, cookie_name) _h = HTTPLib() set_cookie(_h.cookiejar, SimpleCookie(kaka[1])) res = _h._cookies() assert set(res.keys()) == {'Foobar'} kwargs = _h.add_cookies({}) assert 'cookies' in kwargs assert set(kwargs['cookies'].keys()) == {'Foobar'}
def test_set_cookie(): cookiejar = FileCookieJar() _cookie = {"value_0": "v_0", "value_1": "v_1", "value_2": "v_2"} c = SimpleCookie(_cookie) domain_0 = ".test_domain" domain_1 = "test_domain" max_age = "09 Feb 1994 22:23:32 GMT" expires = http2time(max_age) path = "test/path" c["value_0"]["max-age"] = max_age c["value_0"]["domain"] = domain_0 c["value_0"]["path"] = path c["value_1"]["domain"] = domain_1 util.set_cookie(cookiejar, c) cookies = cookiejar._cookies c_0 = cookies[domain_0][path]["value_0"] c_1 = cookies[domain_1][""]["value_1"] c_2 = cookies[""][""]["value_2"] assert not (c_2.domain_specified and c_2.path_specified) assert c_1.domain_specified and not c_1.domain_initial_dot and not \ c_1.path_specified assert c_0.domain_specified and c_0.domain_initial_dot and \ c_0.path_specified assert c_0.expires == expires assert c_0.domain == domain_0 assert c_0.name == "value_0" assert c_0.path == path assert c_0.value == "v_0" assert not c_1.expires assert c_1.domain == domain_1 assert c_1.name == "value_1" assert c_1.path == "" assert c_1.value == "v_1" assert not c_2.expires assert c_2.domain == "" assert c_2.name == "value_2" assert c_2.path == "" assert c_2.value == "v_2"