99# Standard library
1010import contextlib
1111import re
12+ import socket
1213import sys
1314import unittest
1415import jsonrpclib
2930
3031# ------------------------------------------------------------------------------
3132
33+ HOST = socket .gethostbyname ("localhost" )
34+
3235
3336class HeadersTests (unittest .TestCase ):
3437 """
@@ -85,18 +88,20 @@ def captured_headers(self, check_duplicates=True):
8588
8689 # Extract headers
8790 raw_headers = request_line .splitlines ()[1 :- 1 ]
88- raw_headers = map (lambda h : re .split (r":\s?" , h , 1 ), raw_headers )
91+ raw_headers = map (
92+ lambda h : re .split (r":\s?" , h , maxsplit = 1 ), raw_headers
93+ )
8994 for header , value in raw_headers :
9095 header = header .lower ()
9196 if check_duplicates and header in headers :
9297 raise KeyError ("Header defined twice: {0}" .format (header ))
9398 headers [header ] = value
9499
95100 def test_should_extract_headers (self ):
96- """ Check client headers capture """
101+ """Check client headers capture"""
97102 # given
98103 client = jsonrpclib .ServerProxy (
99- "http://localhost: {0}" .format (self .port ), verbose = 1
104+ "http://{0}:{1} " .format (HOST , self .port ), verbose = 1
100105 )
101106
102107 # when
@@ -110,10 +115,10 @@ def test_should_extract_headers(self):
110115 self .assertEqual (headers ["content-type" ], "application/json-rpc" )
111116
112117 def test_should_add_additional_headers (self ):
113- """ Check sending of custom headers """
118+ """Check sending of custom headers"""
114119 # given
115120 client = jsonrpclib .ServerProxy (
116- "http://localhost: {0}" .format (self .port ),
121+ "http://{0}:{1} " .format (HOST , self .port ),
117122 verbose = 1 ,
118123 headers = {"X-My-Header" : "Test" },
119124 )
@@ -128,10 +133,10 @@ def test_should_add_additional_headers(self):
128133 self .assertEqual (headers ["x-my-header" ], "Test" )
129134
130135 def test_should_add_additional_headers_to_notifications (self ):
131- """ Check custom headers on notifications """
136+ """Check custom headers on notifications"""
132137 # given
133138 client = jsonrpclib .ServerProxy (
134- "http://localhost: {0}" .format (self .port ),
139+ "http://{0}:{1} " .format (HOST , self .port ),
135140 verbose = 1 ,
136141 headers = {"X-My-Header" : "Test" },
137142 )
@@ -145,10 +150,10 @@ def test_should_add_additional_headers_to_notifications(self):
145150 self .assertEqual (headers ["x-my-header" ], "Test" )
146151
147152 def test_should_override_headers (self ):
148- """ Custom headers must override default ones """
153+ """Custom headers must override default ones"""
149154 # given
150155 client = jsonrpclib .ServerProxy (
151- "http://localhost: {0}" .format (self .port ),
156+ "http://{0}:{1} " .format (HOST , self .port ),
152157 verbose = 1 ,
153158 headers = {"User-Agent" : "jsonrpclib test" , "Host" : "example.com" },
154159 )
@@ -163,10 +168,10 @@ def test_should_override_headers(self):
163168 self .assertEqual (headers ["host" ], "example.com" )
164169
165170 def test_should_not_override_content_length (self ):
166- """ Custom headers can't override Content-Length """
171+ """Custom headers can't override Content-Length"""
167172 # given
168173 client = jsonrpclib .ServerProxy (
169- "http://localhost: {0}" .format (self .port ),
174+ "http://{0}:{1} " .format (HOST , self .port ),
170175 verbose = 1 ,
171176 headers = {"Content-Length" : "invalid value" },
172177 )
@@ -181,10 +186,10 @@ def test_should_not_override_content_length(self):
181186 self .assertNotEqual (headers ["content-length" ], "invalid value" )
182187
183188 def test_should_convert_header_values_to_basestring (self ):
184- """ Custom headers values should be converted to str """
189+ """Custom headers values should be converted to str"""
185190 # given
186191 client = jsonrpclib .ServerProxy (
187- "http://localhost: {0}" .format (self .port ),
192+ "http://{0}:{1} " .format (HOST , self .port ),
188193 verbose = 1 ,
189194 headers = {"X-Test" : 123 },
190195 )
@@ -199,10 +204,10 @@ def test_should_convert_header_values_to_basestring(self):
199204 self .assertEqual (headers ["x-test" ], "123" )
200205
201206 def test_should_add_custom_headers_to_methods (self ):
202- """ Check method-based custom headers """
207+ """Check method-based custom headers"""
203208 # given
204209 client = jsonrpclib .ServerProxy (
205- "http://localhost: {0}" .format (self .port ), verbose = 1
210+ "http://{0}:{1} " .format (HOST , self .port ), verbose = 1
206211 )
207212
208213 # when
@@ -217,10 +222,10 @@ def test_should_add_custom_headers_to_methods(self):
217222 self .assertEqual (headers ["x-method" ], "Method" )
218223
219224 def test_should_override_global_headers (self ):
220- """ Method-based custom headers override context ones """
225+ """Method-based custom headers override context ones"""
221226 # given
222227 client = jsonrpclib .ServerProxy (
223- "http://localhost: {0}" .format (self .port ),
228+ "http://{0}:{1} " .format (HOST , self .port ),
224229 verbose = 1 ,
225230 headers = {"X-Test" : "Global" },
226231 )
@@ -236,10 +241,10 @@ def test_should_override_global_headers(self):
236241 self .assertEqual (headers ["x-test" ], "Method" )
237242
238243 def test_should_restore_global_headers (self ):
239- """ Check custom headers context clean up """
244+ """Check custom headers context clean up"""
240245 # given
241246 client = jsonrpclib .ServerProxy (
242- "http://localhost: {0}" .format (self .port ),
247+ "http://{0}:{1} " .format (HOST , self .port ),
243248 verbose = 1 ,
244249 headers = {"X-Test" : "Global" },
245250 )
@@ -262,10 +267,10 @@ def test_should_restore_global_headers(self):
262267 self .assertEqual (headers ["x-test" ], "Global" )
263268
264269 def test_should_allow_to_nest_additional_header_blocks (self ):
265- """ Check nested additional headers """
270+ """Check nested additional headers"""
266271 # given
267272 client = jsonrpclib .ServerProxy (
268- "http://localhost: {0}" .format (self .port ), verbose = 1
273+ "http://{0}:{1} " .format (HOST , self .port ), verbose = 1
269274 )
270275
271276 # when
0 commit comments