def wsgi_app(self, environ, start_response): """A WSGI application callable.""" path = environ.get('SCRIPT_NAME', '') + environ.get('PATH_INFO', '') if 'QUERY_STRING' in environ and environ['QUERY_STRING']: path += '?' + environ['QUERY_STRING'] headers = {} for k, v in environ.items(): if k.startswith('HTTP_'): h = '-'.join([p.title() for p in k[5:].split('_')]) headers[h] = v req = Request( self, (environ['REMOTE_ADDR'], int(environ.get('REMOTE_PORT', '0'))), environ['REQUEST_METHOD'], path, environ['SERVER_PROTOCOL'], headers, stream=environ['wsgi.input']) req.environ = environ res = self.dispatch_request(req) res.complete() reason = res.reason or ('OK' if res.status_code == 200 else 'N/A') header_list = [] for name, value in res.headers.items(): if not isinstance(value, list): header_list.append((name, value)) else: for v in value: header_list.append((name, v)) start_response(str(res.status_code) + ' ' + reason, header_list) return res.body_iter()
def test_form(self): fd = get_request_fd( 'GET', '/foo', headers={'Content-Type': 'application/x-www-form-urlencoded'}, body='foo=bar&abc=def&x=%2f%%') req = Request.create('app', fd, 'addr') form = req.form self.assertEqual(form, MultiDict({ 'foo': 'bar', 'abc': 'def', 'x': '/%%' })) self.assertTrue(req.form is form) fd = get_request_fd( 'GET', '/foo', headers={'Content-Type': 'application/x-www-form-urlencoded'}, body='') req = Request.create('app', fd, 'addr') form = req.form self.assertEqual(form, MultiDict({})) self.assertTrue(req.form is form) fd = get_request_fd('GET', '/foo', headers={'Content-Type': 'application/json'}, body='foo=bar&abc=def&x=%2f%%') req = Request.create('app', fd, 'addr') self.assertIsNone(req.form)
def test_form(self): fd = self._get_request_fd('GET', '/foo', headers={ 'Content-Type': 'application/x-www-form-urlencoded'}, body='foo=bar&abc=def&x=%2f%%') req = Request(fd, 'addr') self.assertEqual(req.form, {'foo': 'bar', 'abc': 'def', 'x': '/%%'}) fd = self._get_request_fd('GET', '/foo', headers={ 'Content-Type': 'application/json'}, body='foo=bar&abc=def&x=%2f%%') req = Request(fd, 'addr') self.assertIsNone(req.form)
def test_large_line(self): saved_max_readline = Request.max_readline Request.max_readline = 16 fd = get_request_fd( 'GET', '/foo', headers={'Content-Type': 'application/x-www-form-urlencoded'}, body='foo=bar&abc=def&x=y') with self.assertRaises(ValueError): Request.create('app', fd, 'addr') Request.max_readline = saved_max_readline
def test_json(self): fd = self._get_request_fd('GET', '/foo', headers={ 'Content-Type': 'application/json'}, body='{"foo":"bar"}') req = Request(fd, 'addr') self.assertEqual(req.json, {'foo': 'bar'}) fd = self._get_request_fd('GET', '/foo', headers={ 'Content-Type': 'application/json'}, body='[1, "2"]') req = Request(fd, 'addr') self.assertEqual(req.json, [1, '2']) fd = self._get_request_fd('GET', '/foo', headers={ 'Content-Type': 'application/xml'}, body='[1, "2"]') req = Request(fd, 'addr') self.assertIsNone(req.json)
def test_create_request(self): fd = self._get_request_fd('GET', '/foo') req = Request(fd, 'addr') req.close() self.assertEqual(req.client_sock, fd) self.assertEqual(req.client_addr, 'addr') self.assertEqual(req.method, 'GET') self.assertEqual(req.path, '/foo') self.assertEqual(req.http_version, '1.0') self.assertIsNone(req.query_string) self.assertEqual(req.args, {}) self.assertEqual(req.headers, {'Host': 'example.com:1234'}) self.assertEqual(req.cookies, {}) self.assertEqual(req.content_length, 0) self.assertEqual(req.content_type, None) self.assertEqual(req.body, b'') self.assertEqual(req.json, None) self.assertEqual(req.form, None)
def test_args(self): fd = get_request_fd('GET', '/?foo=bar&abc=def&x=%2f%%') req = Request.create('app', fd, 'addr') self.assertEqual(req.query_string, 'foo=bar&abc=def&x=%2f%%') self.assertEqual(req.args, MultiDict({ 'foo': 'bar', 'abc': 'def', 'x': '/%%' }))
def test_render_template_in_app(self): app = Microdot() @app.route('/') def index(req): return render_template('hello.utemplate.txt', name='foo') req = Request.create(app, get_request_fd('GET', '/'), 'addr') res = app.dispatch_request(req) self.assertEqual(res.status_code, 200) self.assertEqual(list(res.body_iter()), ['Hello, ', 'foo', '!\n'])
def request(self, method, path, headers=None, body=None): headers = headers or {} body, headers = self._process_body(body, headers) cookies, headers = self._process_cookies(headers) request_bytes = self._render_request(method, path, headers, body) req = Request.create(self.app, BytesIO(request_bytes), ('127.0.0.1', 1234)) res = self.app.dispatch_request(req) res.complete() self._update_cookies(res) return TestResponse.create(res)
def test_body(self): fd = get_request_fd('GET', '/foo', headers={ 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': '19' }, body='foo=bar&abc=def&x=y') req = Request.create('app', fd, 'addr') self.assertEqual(req.body, b'foo=bar&abc=def&x=y') with self.assertRaises(RuntimeError): req.stream
def test_headers(self): fd = get_request_fd('GET', '/foo', headers={ 'Content-Type': 'application/json', 'Cookie': 'foo=bar;abc=def', 'Content-Length': '3'}, body='aaa') req = Request.create(fd, 'addr') self.assertEqual(req.headers, { 'Host': 'example.com:1234', 'Content-Type': 'application/json', 'Cookie': 'foo=bar;abc=def', 'Content-Length': '3'}) self.assertEqual(req.content_type, 'application/json') self.assertEqual(req.cookies, {'foo': 'bar', 'abc': 'def'}) self.assertEqual(req.content_length, 3) self.assertEqual(req.body, b'aaa')
def test_large_payload(self): saved_max_content_length = Request.max_content_length saved_max_body_length = Request.max_body_length Request.max_content_length = 32 Request.max_body_length = 16 fd = get_request_fd( 'GET', '/foo', headers={'Content-Type': 'application/x-www-form-urlencoded'}, body='foo=bar&abc=def&x=y') req = Request.create('app', fd, 'addr') self.assertEqual(req.body, b'') self.assertEqual(req.stream.read(), b'foo=bar&abc=def&x=y') Request.max_content_length = saved_max_content_length Request.max_body_length = saved_max_body_length
def test_args(self): fd = self._get_request_fd('GET', '/?foo=bar&abc=def&x=%2f%%') req = Request(fd, 'addr') self.assertEqual(req.query_string, 'foo=bar&abc=def&x=%2f%%') self.assertEqual(req.args, {'foo': 'bar', 'abc': 'def', 'x': '/%%'})