@@ -28,39 +28,68 @@ def _make_one(self, *args, **kw):
28
28
return self ._get_target_class ()(* args , ** kw )
29
29
30
30
def test_build_api_url_no_extra_query_params (self ):
31
+ from six .moves .urllib .parse import parse_qsl
32
+ from six .moves .urllib .parse import urlsplit
33
+
31
34
conn = self ._make_one (object ())
32
- URI = "/" .join (
33
- [
34
- conn .DEFAULT_API_ENDPOINT ,
35
- "language" ,
36
- "translate" ,
37
- conn .API_VERSION ,
38
- "foo" ,
39
- ]
35
+ uri = conn .build_api_url ("/foo" )
36
+ scheme , netloc , path , qs , _ = urlsplit (uri )
37
+ self .assertEqual ("%s://%s" % (scheme , netloc ), conn .API_BASE_URL )
38
+ self .assertEqual (
39
+ path , "/" .join (["" , "language" , "translate" , conn .API_VERSION , "foo" ])
40
40
)
41
- self .assertEqual (conn .build_api_url ("/foo" ), URI )
41
+ parms = dict (parse_qsl (qs ))
42
+ pretty_print = parms .pop ("prettyPrint" , "false" )
43
+ self .assertEqual (pretty_print , "false" )
44
+ self .assertEqual (parms , {})
42
45
43
46
def test_build_api_url_w_custom_endpoint (self ):
47
+ from six .moves .urllib .parse import parse_qsl
48
+ from six .moves .urllib .parse import urlsplit
49
+
44
50
custom_endpoint = "https://foo-translation.googleapis.com"
45
51
conn = self ._make_one (object (), api_endpoint = custom_endpoint )
46
- URI = "/" .join (
47
- [custom_endpoint , "language" , "translate" , conn .API_VERSION , "foo" ]
52
+ uri = conn .build_api_url ("/foo" )
53
+ scheme , netloc , path , qs , _ = urlsplit (uri )
54
+ self .assertEqual ("%s://%s" % (scheme , netloc ), custom_endpoint )
55
+ self .assertEqual (
56
+ path , "/" .join (["" , "language" , "translate" , conn .API_VERSION , "foo" ])
48
57
)
49
- self .assertEqual (conn .build_api_url ("/foo" ), URI )
58
+ parms = dict (parse_qsl (qs ))
59
+ pretty_print = parms .pop ("prettyPrint" , "false" )
60
+ self .assertEqual (pretty_print , "false" )
61
+ self .assertEqual (parms , {})
50
62
51
63
def test_build_api_url_w_extra_query_params (self ):
52
64
from six .moves .urllib .parse import parse_qsl
53
65
from six .moves .urllib .parse import urlsplit
54
66
67
+ conn = self ._make_one (object ())
68
+ uri = conn .build_api_url ("/foo" , {"bar" : "baz" })
69
+ scheme , netloc , path , qs , _ = urlsplit (uri )
70
+ self .assertEqual ("%s://%s" % (scheme , netloc ), conn .API_BASE_URL )
71
+ self .assertEqual (
72
+ path , "/" .join (["" , "language" , "translate" , conn .API_VERSION , "foo" ])
73
+ )
74
+ parms = dict (parse_qsl (qs ))
75
+ self .assertEqual (parms ["bar" ], "baz" )
76
+
77
+ def test_build_api_url_w_extra_query_params_tuple (self ):
78
+ from six .moves .urllib .parse import parse_qsl
79
+ from six .moves .urllib .parse import urlsplit
80
+
55
81
conn = self ._make_one (object ())
56
82
query_params = [("q" , "val1" ), ("q" , "val2" )]
57
83
uri = conn .build_api_url ("/foo" , query_params = query_params )
58
84
scheme , netloc , path , qs , _ = urlsplit (uri )
59
85
self .assertEqual ("%s://%s" % (scheme , netloc ), conn .API_BASE_URL )
60
86
expected_path = "/" .join (["" , "language" , "translate" , conn .API_VERSION , "foo" ])
61
87
self .assertEqual (path , expected_path )
62
- params = parse_qsl (qs )
63
- self .assertEqual (params , query_params )
88
+ params = list (
89
+ sorted (param for param in parse_qsl (qs ) if param [0 ] != "prettyPrint" )
90
+ )
91
+ expected_params = [("q" , "val1" ), ("q" , "val2" )]
92
+ self .assertEqual (params , expected_params )
64
93
65
94
def test_extra_headers (self ):
66
95
import requests
0 commit comments