mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-13 16:40:50 -07:00
Handle duplicate headers in the uri module (#33792)
* Handle duplicate headers, and make it easier for users to use cookies, by providing a pre-built string * Ensure proper cookie ordering, make key plural * Add note about cookie sort order * Add tests for duplicate headers and cookies_string * Extend tests, normalize headers between py2 and py3 * Add some notes in test code * Don't use AttributeError, use six.PY3. Use better names.
This commit is contained in:
parent
f1082af73f
commit
450cfa8776
3 changed files with 55 additions and 2 deletions
|
@ -9,6 +9,7 @@ import socket
|
|||
|
||||
from ansible.module_utils.six import StringIO
|
||||
from ansible.module_utils.six.moves.http_cookiejar import Cookie
|
||||
from ansible.module_utils.six.moves.http_client import HTTPMessage
|
||||
from ansible.module_utils.urls import fetch_url, urllib_error, ConnectionError, NoSSLError, httplib
|
||||
|
||||
import pytest
|
||||
|
@ -94,6 +95,15 @@ def test_fetch_url_params(open_url_mock, fake_ansible_module):
|
|||
def test_fetch_url_cookies(mocker, fake_ansible_module):
|
||||
def make_cookies(*args, **kwargs):
|
||||
cookies = kwargs['cookies']
|
||||
r = MagicMock()
|
||||
try:
|
||||
r.headers = HTTPMessage()
|
||||
add_header = r.headers.add_header
|
||||
except TypeError:
|
||||
# PY2
|
||||
r.headers = HTTPMessage(StringIO())
|
||||
add_header = r.headers.addheader
|
||||
r.info.return_value = r.headers
|
||||
for name, value in (('Foo', 'bar'), ('Baz', 'qux')):
|
||||
cookie = Cookie(
|
||||
version=0,
|
||||
|
@ -114,14 +124,21 @@ def test_fetch_url_cookies(mocker, fake_ansible_module):
|
|||
rest=None
|
||||
)
|
||||
cookies.set_cookie(cookie)
|
||||
add_header('Set-Cookie', '%s=%s' % (name, value))
|
||||
|
||||
return MagicMock()
|
||||
return r
|
||||
|
||||
mocker = mocker.patch('ansible.module_utils.urls.open_url', new=make_cookies)
|
||||
|
||||
r, info = fetch_url(fake_ansible_module, 'http://ansible.com/')
|
||||
|
||||
assert info['cookies'] == {'Baz': 'qux', 'Foo': 'bar'}
|
||||
# Python sorts cookies in order of most specific (ie. longest) path first
|
||||
# items with the same path are reversed from response order
|
||||
assert info['cookies_string'] == 'Baz=qux; Foo=bar'
|
||||
# The key here has a `-` as opposed to what we see in the `uri` module that converts to `_`
|
||||
# Note: this is response order, which differs from cookies_string
|
||||
assert info['set-cookie'] == 'Foo=bar, Baz=qux'
|
||||
|
||||
|
||||
def test_fetch_url_nossl(open_url_mock, fake_ansible_module, mocker):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue