@@ -33,6 +33,16 @@ def main(args):
33
33
nap .parser .add_argument ("--var_true" , action = "store_true" )
34
34
nap (main , resp_delay = 0.2 , parse_args = ["nap" , "--port" , "7001" ])
35
35
36
+ def tcp_socket_autoformat_mult_args ():
37
+ def main (args ):
38
+ return vars (args )
39
+
40
+ nap = NetArgumentParser ()
41
+ nap .parser .add_argument ("-x" , type = int , nargs = "+" )
42
+ nap .parser .add_argument ("-y" , type = int , action = "append" )
43
+ nap .parser .add_argument ("-z" , type = int , nargs = "+" , action = "append" )
44
+ nap (main , resp_delay = 0.2 , parse_args = ["nap" , "--port" , "7002" ])
45
+
36
46
def http_no_autoformat ():
37
47
def main (args ):
38
48
if args .var_str == "damn" :
@@ -43,7 +53,7 @@ def main(args):
43
53
nap .parser .add_argument ("--var_str" , type = str )
44
54
nap .parser .add_argument ("--var_int" , type = int )
45
55
nap .parser .add_argument ("--var_true" , action = "store_true" )
46
- nap (main , False , 0.2 , ["nap" , "--port" , "7002 " , "--http" ])
56
+ nap (main , False , 0.2 , ["nap" , "--port" , "7003 " , "--http" ])
47
57
48
58
def http_autoformat ():
49
59
def main (args ):
@@ -55,7 +65,17 @@ def main(args):
55
65
nap .parser .add_argument ("--var_str" , type = str )
56
66
nap .parser .add_argument ("--var_int" , type = int )
57
67
nap .parser .add_argument ("--var_true" , action = "store_true" )
58
- nap (main , resp_delay = 0.2 , parse_args = ["nap" , "--port" , "7003" , "--http" ])
68
+ nap (main , resp_delay = 0.2 , parse_args = ["nap" , "--port" , "7004" , "--http" ])
69
+
70
+ def http_autoformat_mult_args ():
71
+ def main (args ):
72
+ return vars (args )
73
+
74
+ nap = NetArgumentParser ()
75
+ nap .parser .add_argument ("-x" , type = int , nargs = "+" )
76
+ nap .parser .add_argument ("-y" , type = int , action = "append" )
77
+ nap .parser .add_argument ("-z" , type = int , nargs = "+" , action = "append" )
78
+ nap (main , resp_delay = 0.2 , parse_args = ["nap" , "--port" , "7005" , "--http" ])
59
79
60
80
61
81
class TcpSocketRequest :
@@ -153,6 +173,16 @@ def test_plain_xml_a_func_exc(self):
153
173
self .assertEqual (ans , b"<nap><response></response><exception>division by zero</exception><finished>1</finished></nap>" )
154
174
self .assertResponse (ans , "xml" )
155
175
176
+ def test_plain_xml_a_ma (self ):
177
+ ans = s_tcp_a_ma .txrx (b"<nap><_x>1 2 3</_x><_x>11 22 33</_x><_y>1</_y><_y>11</_y><_z>1 2 3</_z><_z>11 22 33</_z></nap>" )
178
+ self .assertEqual (ans , b"<nap><response><x>[11, 22, 33]</x><y>[1, 11]</y><z>[[1, 2, 3], [11, 22, 33]]</z><_cmd>nap</_cmd></response><exception></exception><finished>1</finished></nap>" )
179
+ self .assertResponse (ans , "xml" )
180
+
181
+ def test_plain_xml_invalid_a_ma (self ):
182
+ ans = s_tcp_a_ma .txrx (b"<nap><_x>1 2 3</_x><_x>11 22 33</_x><_y>1 2</_y><_y>11 22</_y><_z>1 2 3</_z><_z>11 22 33</_z></nap>" )
183
+ self .assertEqual (ans , b"<nap><response></response><exception>unrecognized arguments: 2 22</exception><finished>1</finished></nap>" )
184
+ self .assertResponse (ans , "xml" )
185
+
156
186
# Plain json, autoformat
157
187
def test_plain_json_a_valid_tx (self ):
158
188
ans = s_tcp_a .txrx (b'{"--var_str": "value", "--var_int": "2"}' )
@@ -174,6 +204,21 @@ def test_plain_json_a_func_exc(self):
174
204
self .assertEqual (ans , b'{"response": "", "exception": "division by zero", "finished": 1}' )
175
205
self .assertResponse (ans , "json" )
176
206
207
+ def test_plain_json_a_ma (self ):
208
+ ans = s_tcp_a_ma .txrx (b'{"-x": ["1 2 3", "11 22 33"], "-y": [1, 11], "-z": ["1 2 3", "11 22 33"]}' )
209
+ self .assertEqual (ans , b'{"response": {"x": [11, 22, 33], "y": [1, 11], "z": [[1, 2, 3], [11, 22, 33]], "_cmd": "nap"}, "exception": "", "finished": 1}' )
210
+ self .assertResponse (ans , "json" )
211
+
212
+ def test_plain_json_a_ma_double_key (self ):
213
+ ans = s_tcp_a_ma .txrx (b'{"-x": "1 2 3", "-x": "11 22 33", "-y": 1, "-y": 11, "-z": "11 22 33", "-z": "11 22 33"}' )
214
+ self .assertEqual (ans , b'{"response": {"x": [11, 22, 33], "y": [11], "z": [[11, 22, 33]], "_cmd": "nap"}, "exception": "", "finished": 1}' )
215
+ self .assertResponse (ans , "json" )
216
+
217
+ def test_plain_json_invalid_a_ma (self ):
218
+ ans = s_tcp_a_ma .txrx (b'{"-x": ["1 2 3", "11 22 33"], "-y": ["1 2", "11 22"], "-z": ["1 2 3", "11 22 33"]}' )
219
+ self .assertEqual (ans , b'{"response": "", "exception": "unrecognized arguments: 2 22", "finished": 1}' )
220
+ self .assertResponse (ans , "json" )
221
+
177
222
# HTTP, json resp, no autoformat
178
223
def test_http_json_na_valid_tx (self ):
179
224
ans = s_http_na .txrx ("/?--var_str=value&--var_int=2" )
@@ -237,6 +282,16 @@ def test_http_json_a_func_exc(self):
237
282
self .assertEqual (ans , '{"response": "", "exception": "division by zero", "finished": 1}' )
238
283
self .assertResponse (ans , "json" )
239
284
285
+ def test_http_json_a_ma (self ):
286
+ ans = s_http_a_ma .txrx ("/?-x=1 2 3&-x=11 22 33&-y=1&-y=11&-z=1 2 3&-z=11 22 33" )
287
+ self .assertEqual (ans , '{"response": {"x": [11, 22, 33], "y": [1, 11], "z": [[1, 2, 3], [11, 22, 33]], "_cmd": "nap"}, "exception": "", "finished": 1}' )
288
+ self .assertResponse (ans , "json" )
289
+
290
+ def test_http_json_invalid_a_ma (self ):
291
+ ans = s_http_a_ma .txrx ("/?-x=1 2 3&-x=11 22 33&-y=1 2&-y=11 22&-z=1 2 3&-z=11 22 33" )
292
+ self .assertEqual (ans , '{"response": "", "exception": "unrecognized arguments: 2 22", "finished": 1}' )
293
+ self .assertResponse (ans , "json" )
294
+
240
295
# HTTP, xml resp, autoformat
241
296
def test_http_xml_a_valid_tx (self ):
242
297
ans = s_http_a .txrx ("/xml?--var_str=value&--var_int=2" )
@@ -260,7 +315,8 @@ def test_http_xml_a_func_exc(self):
260
315
261
316
262
317
if __name__ == "__main__" :
263
- for t in [tcp_socket_no_autoformat , tcp_socket_autoformat , http_no_autoformat , http_autoformat ]:
318
+ for t in [tcp_socket_no_autoformat , tcp_socket_autoformat , tcp_socket_autoformat_mult_args ,
319
+ http_no_autoformat , http_autoformat , http_autoformat_mult_args ]:
264
320
Thread (target = t , daemon = True ).start ()
265
321
266
322
for i in range (10 ):
@@ -271,10 +327,14 @@ def test_http_xml_a_func_exc(self):
271
327
s_tcp_na = TcpSocketRequest (7000 )
272
328
if not "s_tcp_a" in globals ():
273
329
s_tcp_a = TcpSocketRequest (7001 )
330
+ if not "s_tcp_a_ma" in globals ():
331
+ s_tcp_a_ma = TcpSocketRequest (7002 )
274
332
if not "s_http_na" in globals ():
275
- s_http_na = HttpRequest (7002 )
333
+ s_http_na = HttpRequest (7003 )
276
334
if not "s_http_a" in globals ():
277
- s_http_a = HttpRequest (7003 )
335
+ s_http_a = HttpRequest (7004 )
336
+ if not "s_http_a_ma" in globals ():
337
+ s_http_a_ma = HttpRequest (7005 )
278
338
break
279
339
except (ConnectionRefusedError , requests .exceptions .ConnectionError ):
280
340
time .sleep (1 )
0 commit comments