|
4 | 4 | """Tests for requests_unixsocket"""
|
5 | 5 |
|
6 | 6 | import logging
|
| 7 | +import os |
| 8 | +import stat |
7 | 9 |
|
8 | 10 | import pytest
|
9 | 11 | import requests
|
| 12 | +from requests.compat import urlparse |
10 | 13 |
|
11 | 14 | import requests_unixsocket
|
12 | 15 | from requests_unixsocket.testutils import UnixSocketServerThread
|
|
15 | 18 | logger = logging.getLogger(__name__)
|
16 | 19 |
|
17 | 20 |
|
| 21 | +def is_socket(path): |
| 22 | + try: |
| 23 | + mode = os.stat(path).st_mode |
| 24 | + return stat.S_ISSOCK(mode) |
| 25 | + except OSError: |
| 26 | + return False |
| 27 | + |
| 28 | + |
| 29 | +def get_sock_prefix(path): |
| 30 | + """Keep going up directory tree until we find a socket""" |
| 31 | + |
| 32 | + sockpath = path |
| 33 | + reqpath_parts = [] |
| 34 | + |
| 35 | + while not is_socket(sockpath): |
| 36 | + sockpath, tail = os.path.split(sockpath) |
| 37 | + reqpath_parts.append(tail) |
| 38 | + |
| 39 | + return requests_unixsocket.UnixAdapter.Settings.ParseResult( |
| 40 | + sockpath=sockpath, |
| 41 | + reqpath='/' + os.path.join(*reversed(reqpath_parts)), |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +alt_settings_1 = requests_unixsocket.UnixAdapter.Settings( |
| 46 | + urlparse=lambda url: get_sock_prefix(urlparse(url).path), |
| 47 | +) |
| 48 | + |
| 49 | + |
18 | 50 | def test_use_UnixAdapter_directly():
|
19 | 51 | """Test using UnixAdapter directly, because
|
20 | 52 | https://github.com/httpie/httpie-unixsocket does this
|
@@ -54,6 +86,34 @@ def test_unix_domain_adapter_ok():
|
54 | 86 | assert r.text == 'Hello world!'
|
55 | 87 |
|
56 | 88 |
|
| 89 | +def test_unix_domain_adapter_alt_settings_1_ok(): |
| 90 | + with UnixSocketServerThread() as usock_thread: |
| 91 | + session = requests_unixsocket.Session( |
| 92 | + url_scheme='http+unix://', |
| 93 | + settings=alt_settings_1, |
| 94 | + ) |
| 95 | + url = 'http+unix://localhost%s/path/to/page' % usock_thread.usock |
| 96 | + |
| 97 | + for method in ['get', 'post', 'head', 'patch', 'put', 'delete', |
| 98 | + 'options']: |
| 99 | + logger.debug('Calling session.%s(%r) ...', method, url) |
| 100 | + r = getattr(session, method)(url) |
| 101 | + logger.debug( |
| 102 | + 'Received response: %r with text: %r and headers: %r', |
| 103 | + r, r.text, r.headers) |
| 104 | + assert r.status_code == 200 |
| 105 | + assert r.headers['server'] == 'waitress' |
| 106 | + assert r.headers['X-Transport'] == 'unix domain socket' |
| 107 | + assert r.headers['X-Requested-Path'] == '/path/to/page' |
| 108 | + assert r.headers['X-Socket-Path'] == usock_thread.usock |
| 109 | + assert isinstance(r.connection, requests_unixsocket.UnixAdapter) |
| 110 | + assert r.url.lower() == url.lower() |
| 111 | + if method == 'head': |
| 112 | + assert r.text == '' |
| 113 | + else: |
| 114 | + assert r.text == 'Hello world!' |
| 115 | + |
| 116 | + |
57 | 117 | def test_unix_domain_adapter_url_with_query_params():
|
58 | 118 | with UnixSocketServerThread() as usock_thread:
|
59 | 119 | session = requests_unixsocket.Session('http+unix://')
|
@@ -82,6 +142,33 @@ def test_unix_domain_adapter_url_with_query_params():
|
82 | 142 | assert r.text == 'Hello world!'
|
83 | 143 |
|
84 | 144 |
|
| 145 | +def test_unix_domain_adapter_url_with_fragment(): |
| 146 | + with UnixSocketServerThread() as usock_thread: |
| 147 | + session = requests_unixsocket.Session('http+unix://') |
| 148 | + urlencoded_usock = requests.compat.quote_plus(usock_thread.usock) |
| 149 | + url = ('http+unix://%s' |
| 150 | + '/containers/nginx/logs#some-fragment' % urlencoded_usock) |
| 151 | + |
| 152 | + for method in ['get', 'post', 'head', 'patch', 'put', 'delete', |
| 153 | + 'options']: |
| 154 | + logger.debug('Calling session.%s(%r) ...', method, url) |
| 155 | + r = getattr(session, method)(url) |
| 156 | + logger.debug( |
| 157 | + 'Received response: %r with text: %r and headers: %r', |
| 158 | + r, r.text, r.headers) |
| 159 | + assert r.status_code == 200 |
| 160 | + assert r.headers['server'] == 'waitress' |
| 161 | + assert r.headers['X-Transport'] == 'unix domain socket' |
| 162 | + assert r.headers['X-Requested-Path'] == '/containers/nginx/logs' |
| 163 | + assert r.headers['X-Socket-Path'] == usock_thread.usock |
| 164 | + assert isinstance(r.connection, requests_unixsocket.UnixAdapter) |
| 165 | + assert r.url.lower() == url.lower() |
| 166 | + if method == 'head': |
| 167 | + assert r.text == '' |
| 168 | + else: |
| 169 | + assert r.text == 'Hello world!' |
| 170 | + |
| 171 | + |
85 | 172 | def test_unix_domain_adapter_connection_error():
|
86 | 173 | session = requests_unixsocket.Session('http+unix://')
|
87 | 174 |
|
|
0 commit comments