1
+ import io
2
+ import multiprocessing
3
+ from wsgiref import simple_server
4
+
5
+ import requests
6
+
7
+ import falcon
8
+ from falcon import request_helpers
1
9
import falcon .testing as testing
2
10
11
+ SIZE_1_KB = 1024
12
+
3
13
4
14
class TestRequestBody (testing .TestBase ):
5
15
@@ -25,8 +35,17 @@ def test_tiny_body(self):
25
35
stream .seek (0 , 2 )
26
36
self .assertEquals (stream .tell (), 1 )
27
37
38
+ def test_tiny_body_overflow (self ):
39
+ expected_body = '.'
40
+ self .simulate_request ('' , body = expected_body )
41
+ stream = self .resource .req .stream
42
+
43
+ # Read too many bytes; shouldn't block
44
+ actual_body = stream .read (len (expected_body ) + 1 )
45
+ self .assertEquals (actual_body , expected_body .encode ('utf-8' ))
46
+
28
47
def test_read_body (self ):
29
- expected_body = testing .rand_string (2 , 1 * 1024 * 1024 )
48
+ expected_body = testing .rand_string (SIZE_1_KB / 2 , SIZE_1_KB )
30
49
expected_len = len (expected_body )
31
50
headers = {'Content-Length' : str (expected_len )}
32
51
@@ -44,3 +63,85 @@ def test_read_body(self):
44
63
self .assertEquals (stream .tell (), expected_len )
45
64
46
65
self .assertEquals (stream .tell (), expected_len )
66
+
67
+ def test_read_socket_body (self ):
68
+ expected_body = testing .rand_string (SIZE_1_KB / 2 , SIZE_1_KB )
69
+
70
+ def server ():
71
+ class Echo (object ):
72
+ def on_post (self , req , resp ):
73
+ # wsgiref socket._fileobject blocks when len not given,
74
+ # but Falcon is smarter than that. :D
75
+ body = req .stream .read ()
76
+ resp .body = body
77
+
78
+ def on_put (self , req , resp ):
79
+ # wsgiref socket._fileobject blocks when len too long,
80
+ # but Falcon should work around that for me.
81
+ body = req .stream .read (req .content_length + 1 )
82
+ resp .body = body
83
+
84
+ api = falcon .API ()
85
+ api .add_route ('/echo' , Echo ())
86
+
87
+ httpd = simple_server .make_server ('127.0.0.1' , 8989 , api )
88
+ httpd .serve_forever ()
89
+
90
+ process = multiprocessing .Process (target = server )
91
+ process .daemon = True
92
+ process .start ()
93
+
94
+ # Let it boot
95
+ process .join (1 )
96
+
97
+ url = 'http://127.0.0.1:8989/echo'
98
+ resp = requests .post (url , data = expected_body )
99
+ self .assertEquals (resp .text , expected_body )
100
+
101
+ resp = requests .put (url , data = expected_body )
102
+ self .assertEquals (resp .text , expected_body )
103
+
104
+ process .terminate ()
105
+
106
+ def test_body_stream_wrapper (self ):
107
+ data = testing .rand_string (SIZE_1_KB / 2 , SIZE_1_KB )
108
+ expected_body = data .encode ('utf-8' )
109
+ expected_len = len (expected_body )
110
+
111
+ # NOTE(kgriffs): Append newline char to each line
112
+ # to match readlines behavior
113
+ expected_lines = [(line + '\n ' ).encode ('utf-8' )
114
+ for line in data .split ('\n ' )]
115
+
116
+ # NOTE(kgriffs): Remove trailing newline to simulate
117
+ # what readlines does
118
+ expected_lines [- 1 ] = expected_lines [- 1 ][:- 1 ]
119
+
120
+ stream = io .BytesIO (expected_body )
121
+ body = request_helpers .Body (stream , expected_len )
122
+ self .assertEquals (body .read (), expected_body )
123
+
124
+ stream = io .BytesIO (expected_body )
125
+ body = request_helpers .Body (stream , expected_len )
126
+ self .assertEquals (body .read (2 ), expected_body [0 :2 ])
127
+
128
+ stream = io .BytesIO (expected_body )
129
+ body = request_helpers .Body (stream , expected_len )
130
+ self .assertEquals (body .read (expected_len + 1 ), expected_body )
131
+
132
+ stream = io .BytesIO (expected_body )
133
+ body = request_helpers .Body (stream , expected_len )
134
+ self .assertEquals (body .readline (), expected_lines [0 ])
135
+
136
+ stream = io .BytesIO (expected_body )
137
+ body = request_helpers .Body (stream , expected_len )
138
+ self .assertEquals (body .readlines (), expected_lines )
139
+
140
+ stream = io .BytesIO (expected_body )
141
+ body = request_helpers .Body (stream , expected_len )
142
+ self .assertEquals (next (body ), expected_lines [0 ])
143
+
144
+ stream = io .BytesIO (expected_body )
145
+ body = request_helpers .Body (stream , expected_len )
146
+ for i , line in enumerate (body ):
147
+ self .assertEquals (line , expected_lines [i ])
0 commit comments