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_unix_domain_adapter_ok ():
19
51
with UnixSocketServerThread () as usock_thread :
20
52
session = requests_unixsocket .Session ('http+unix://' )
@@ -41,6 +73,34 @@ def test_unix_domain_adapter_ok():
41
73
assert r .text == 'Hello world!'
42
74
43
75
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
+
44
104
def test_unix_domain_adapter_url_with_query_params ():
45
105
with UnixSocketServerThread () as usock_thread :
46
106
session = requests_unixsocket .Session ('http+unix://' )
@@ -69,6 +129,33 @@ def test_unix_domain_adapter_url_with_query_params():
69
129
assert r .text == 'Hello world!'
70
130
71
131
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
+
72
159
def test_unix_domain_adapter_connection_error ():
73
160
session = requests_unixsocket .Session ('http+unix://' )
74
161
0 commit comments