44"""Tests for requests_unixsocket"""
55
66import logging
7+ import os
8+ import stat
79
810import pytest
911import requests
12+ from requests .compat import urlparse
1013
1114import requests_unixsocket
1215from requests_unixsocket .testutils import UnixSocketServerThread
1518logger = logging .getLogger (__name__ )
1619
1720
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+
1850def test_unix_domain_adapter_ok ():
1951 with UnixSocketServerThread () as usock_thread :
2052 session = requests_unixsocket .Session ('http+unix://' )
@@ -41,6 +73,34 @@ def test_unix_domain_adapter_ok():
4173 assert r .text == 'Hello world!'
4274
4375
76+ def test_unix_domain_adapter_alt_settings_1_ok ():
77+ with UnixSocketServerThread () as usock_thread :
78+ session = requests_unixsocket .Session (
79+ url_scheme = 'http+unix://' ,
80+ settings = alt_settings_1 ,
81+ )
82+ url = 'http+unix://localhost%s/path/to/page' % usock_thread .usock
83+
84+ for method in ['get' , 'post' , 'head' , 'patch' , 'put' , 'delete' ,
85+ 'options' ]:
86+ logger .debug ('Calling session.%s(%r) ...' , method , url )
87+ r = getattr (session , method )(url )
88+ logger .debug (
89+ 'Received response: %r with text: %r and headers: %r' ,
90+ r , r .text , r .headers )
91+ assert r .status_code == 200
92+ assert r .headers ['server' ] == 'waitress'
93+ assert r .headers ['X-Transport' ] == 'unix domain socket'
94+ assert r .headers ['X-Requested-Path' ] == '/path/to/page'
95+ assert r .headers ['X-Socket-Path' ] == usock_thread .usock
96+ assert isinstance (r .connection , requests_unixsocket .UnixAdapter )
97+ assert r .url .lower () == url .lower ()
98+ if method == 'head' :
99+ assert r .text == ''
100+ else :
101+ assert r .text == 'Hello world!'
102+
103+
44104def test_unix_domain_adapter_url_with_query_params ():
45105 with UnixSocketServerThread () as usock_thread :
46106 session = requests_unixsocket .Session ('http+unix://' )
@@ -69,6 +129,33 @@ def test_unix_domain_adapter_url_with_query_params():
69129 assert r .text == 'Hello world!'
70130
71131
132+ def test_unix_domain_adapter_url_with_fragment ():
133+ with UnixSocketServerThread () as usock_thread :
134+ session = requests_unixsocket .Session ('http+unix://' )
135+ urlencoded_usock = requests .compat .quote_plus (usock_thread .usock )
136+ url = ('http+unix://%s'
137+ '/containers/nginx/logs#some-fragment' % urlencoded_usock )
138+
139+ for method in ['get' , 'post' , 'head' , 'patch' , 'put' , 'delete' ,
140+ 'options' ]:
141+ logger .debug ('Calling session.%s(%r) ...' , method , url )
142+ r = getattr (session , method )(url )
143+ logger .debug (
144+ 'Received response: %r with text: %r and headers: %r' ,
145+ r , r .text , r .headers )
146+ assert r .status_code == 200
147+ assert r .headers ['server' ] == 'waitress'
148+ assert r .headers ['X-Transport' ] == 'unix domain socket'
149+ assert r .headers ['X-Requested-Path' ] == '/containers/nginx/logs'
150+ assert r .headers ['X-Socket-Path' ] == usock_thread .usock
151+ assert isinstance (r .connection , requests_unixsocket .UnixAdapter )
152+ assert r .url .lower () == url .lower ()
153+ if method == 'head' :
154+ assert r .text == ''
155+ else :
156+ assert r .text == 'Hello world!'
157+
158+
72159def test_unix_domain_adapter_connection_error ():
73160 session = requests_unixsocket .Session ('http+unix://' )
74161
0 commit comments