12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+
16
+ from google .appengine .api import urlfetch
15
17
import mock
16
18
import pytest
17
19
import rpc
@@ -23,25 +25,50 @@ def app():
23
25
return webtest .TestApp (rpc .app )
24
26
25
27
26
- @mock .patch (" rpc.urlfetch" )
28
+ @mock .patch (' rpc.urlfetch' )
27
29
def test_url_fetch (urlfetch_mock , app ):
28
30
get_result_mock = mock .Mock (
29
- return_value = mock .Mock (status_code = 200 ,
30
- content = "I'm Feeling Lucky" ))
31
+ return_value = mock .Mock (
32
+ status_code = 200 ,
33
+ content = 'I\' m Feeling Lucky' ))
31
34
urlfetch_mock .create_rpc = mock .Mock (
32
35
return_value = mock .Mock (get_result = get_result_mock ))
33
36
response = app .get ('/' )
34
37
assert response .status_int == 200
35
- assert "I'm Feeling Lucky" in response .body
38
+ assert 'I\' m Feeling Lucky' in response .body
39
+
40
+
41
+ @mock .patch ('rpc.urlfetch' )
42
+ def test_url_fetch_rpc_error (urlfetch_mock , app ):
43
+ urlfetch_mock .DownloadError = urlfetch .DownloadError
44
+ get_result_mock = mock .Mock (
45
+ side_effect = urlfetch .DownloadError ())
46
+ urlfetch_mock .create_rpc = mock .Mock (
47
+ return_value = mock .Mock (get_result = get_result_mock ))
48
+ response = app .get ('/' , status = 500 )
49
+ assert 'Error fetching URL' in response .body
36
50
37
51
38
- @mock .patch ("rpc.urlfetch" )
52
+ @mock .patch ('rpc.urlfetch' )
53
+ def test_url_fetch_http_error (urlfetch_mock , app ):
54
+ get_result_mock = mock .Mock (
55
+ return_value = mock .Mock (
56
+ status_code = 404 ,
57
+ content = 'Not Found' ))
58
+ urlfetch_mock .create_rpc = mock .Mock (
59
+ return_value = mock .Mock (get_result = get_result_mock ))
60
+ response = app .get ('/' , status = 404 )
61
+ assert '404' in response .body
62
+
63
+
64
+ @mock .patch ('rpc.urlfetch' )
39
65
def test_url_post (urlfetch_mock , app ):
40
66
get_result_mock = mock .Mock (
41
- return_value = mock .Mock (status_code = 200 ,
42
- content = "I'm Feeling Lucky" ))
67
+ return_value = mock .Mock (
68
+ status_code = 200 ,
69
+ content = 'I\' m Feeling Lucky' ))
43
70
urlfetch_mock .create_rpc = mock .Mock (
44
- return_value = mock .Mock (get_result = get_result_mock ))
71
+ return_value = mock .Mock (get_result = get_result_mock ))
45
72
46
73
rpc_mock = mock .Mock ()
47
74
urlfetch_mock .create_rpc .return_value = rpc_mock
0 commit comments