1
+ # -*- coding: utf-8 -*-
1
2
import unittest
2
3
3
4
import balanced
5
+ from balanced .http_client import wrap_raise_for_status
4
6
import mock
5
7
6
8
7
9
class TestConfig (unittest .TestCase ):
8
-
9
10
def test_default_config (self ):
10
11
config = balanced .config .__class__ ()
11
12
# this is here because it tests taht if you add anything new
@@ -14,7 +15,7 @@ def test_default_config(self):
14
15
self .assertItemsEqual (
15
16
config .__dict__ .keys (),
16
17
['api_key_secret' , 'api_version' , 'root_uri' , 'requests' ]
17
- )
18
+ )
18
19
self .assertEqual (config .root_uri , 'https://api.balancedpayments.com' )
19
20
self .assertEqual (config .api_version , '1' )
20
21
self .assertEqual (config .api_key_secret , None )
@@ -23,26 +24,25 @@ def test_default_config(self):
23
24
24
25
25
26
class TestClient (unittest .TestCase ):
26
-
27
27
def test_http_operations (self ):
28
28
ops = ['get' , 'post' , 'put' , 'delete' ]
29
29
for op in ops :
30
30
request = getattr (balanced .http_client , op )(
31
31
'hithere' ,
32
32
return_response = False
33
- )
33
+ )
34
34
self .assertEqual (request .method , op .upper ())
35
35
self .assertEqual (
36
36
request .url , 'https://api.balancedpayments.com/v1/hithere'
37
- )
37
+ )
38
38
39
39
def test_client_reference_config (self ):
40
40
the_config = balanced .config
41
41
self .assertIsNone (balanced .http_client .config .api_key_secret )
42
42
the_config .api_key_secret = 'khalkhalash'
43
43
self .assertEqual (
44
44
balanced .http_client .config .api_key_secret , 'khalkhalash'
45
- )
45
+ )
46
46
47
47
def test_client_key_switch (self ):
48
48
the_config = balanced .config
@@ -53,12 +53,11 @@ def test_client_key_switch(self):
53
53
54
54
55
55
class TestHTTPClient (unittest .TestCase ):
56
-
57
56
def test_deserialization (self ):
58
57
resp = mock .Mock ()
59
58
resp .headers = {
60
59
'Content-Type' : 'text/html' ,
61
- }
60
+ }
62
61
resp .content = 'Unhandled Exception'
63
62
client = balanced .HTTPClient ()
64
63
with self .assertRaises (balanced .exc .BalancedError ):
@@ -67,3 +66,44 @@ def test_deserialization(self):
67
66
resp .content = '{"hi": "world"}'
68
67
deserialized = client .deserialize (resp )
69
68
self .assertItemsEqual (deserialized , {u'hi' : u'world' })
69
+
70
+ def test_deserialization_unicode (self ):
71
+ resp = mock .Mock ()
72
+ resp .headers = {
73
+ 'Content-Type' : 'text/html' ,
74
+ }
75
+ resp .content = 'Unhandled Exception'
76
+ client = balanced .HTTPClient ()
77
+ resp .headers ['Content-Type' ] = 'application/json'
78
+ resp .content = ('{"\\ uc800\\ uac74 \\ ub610 \\ ubb50\\ uc57c": "second", '
79
+ '"third": "\\ u06a9\\ u0647 \\ u0686\\ u0647 '
80
+ '\\ u06a9\\ u062b\\ u0627\\ u0641\\ u062a\\ u06cc"}' )
81
+ deserialized = client .deserialize (resp )
82
+ self .assertItemsEqual (deserialized , {
83
+ u'third' : (u'\u06a9 \u0647 \u0686 \u0647 '
84
+ u'\u06a9 \u062b \u0627 \u0641 \u062a \u06cc ' ),
85
+ u'\uc800 \uac74 \ub610 \ubb50 \uc57c ' : u'second' })
86
+
87
+ def test_wrap_raise_for_status (self ):
88
+ api_response = {'additional' : ('Valid email address formats may be '
89
+ 'found at http://tools.ietf.org/html'
90
+ '/rfc2822#section-3.4' ),
91
+ 'description' : (u'"s\xf8 ren.kierkegaard216@yahoo.web" '
92
+ u'must be a valid email address as '
93
+ u'specified by rfc2822 for email_add' ),
94
+ 'status' : 'Bad Request' ,
95
+ 'status_code' : 400 }
96
+ client = mock .Mock ()
97
+ client .deserialize .return_value = api_response
98
+ ex = balanced .exc .HTTPError ('Ooops' )
99
+ setattr (ex , 'response' , mock .Mock ())
100
+ ex .response .status_code = 400
101
+ response = mock .Mock ()
102
+ response .raise_for_status .side_effect = ex
103
+
104
+ wrapped = wrap_raise_for_status (client )
105
+ wrapped (response )
106
+
107
+ with self .assertRaises (balanced .exc .HTTPError ) as ex :
108
+ response .raise_for_status ()
109
+ self .assertEqual (ex .exception .description , api_response ['description' ])
0 commit comments