1616import testtools
1717
1818from fdk import runner
19+ from fdk import response
1920from fdk .tests import data
2021
2122
@@ -24,6 +25,17 @@ def dummy_func(ctx, data=None, loop=None):
2425 return "Hello {0}" .format (body .get ("name" ))
2526
2627
28+ def custom_response (ctx , data = None , loop = None ):
29+ return response .RawResponse (
30+ ctx ,
31+ response_data = dummy_func (ctx , data = data , loop = loop ),
32+ status_code = 201 )
33+
34+
35+ def expectioner (ctx , data = None , loop = None ):
36+ raise Exception ("custom_error" )
37+
38+
2739class TestJSONRequestParser (testtools .TestCase ):
2840
2941 def setUp (self ):
@@ -33,15 +45,27 @@ def tearDown(self):
3345 super (TestJSONRequestParser , self ).tearDown ()
3446
3547 def test_parse_request_without_data (self ):
36- response = runner .from_request (
48+ r = runner .from_request (
3749 dummy_func , data .json_request_without_body )
38- self .assertIsNotNone (response )
39- self .assertIn ("Hello World" , response .body ())
40- self .assertEqual (200 , response .status ())
50+ self .assertIsNotNone (r )
51+ self .assertIn ("Hello World" , r .body ())
52+ self .assertEqual (200 , r .status ())
4153
4254 def test_parse_request_with_data (self ):
43- response = runner .from_request (
55+ r = runner .from_request (
4456 dummy_func , data .json_request_with_body )
45- self .assertIsNotNone (response )
46- self .assertIn ("Hello John" , response .body ())
47- self .assertEqual (200 , response .status ())
57+ self .assertIsNotNone (r )
58+ self .assertIn ("Hello John" , r .body ())
59+ self .assertEqual (200 , r .status ())
60+
61+ def test_custom_response_object (self ):
62+ r = runner .from_request (custom_response , data .json_request_with_body )
63+ self .assertIsNotNone (r )
64+ self .assertIn ("Hello John" , r .body ())
65+ self .assertEqual (201 , r .status ())
66+
67+ def test_errored_func (self ):
68+ in_bytes = data .raw_request_without_body .encode ('utf8' )
69+ r = runner .handle_request (expectioner , in_bytes )
70+ self .assertIsNotNone (r )
71+ self .assertEqual (500 , r .status ())
0 commit comments