def serve_request(self, path): """Given an URL path, return response. """ request = Request(uri=path) request.website = self response = self.handle_safely(request) return response
def load_simplate(self, path, request=None, return_request_too=False): """Given an URL path, return a simplate (Resource) object. """ if request is None: request = Request(uri=path) if not hasattr(request, 'website'): request.website = self self.do_inbound(request) resource = resources.get(request) if return_request_too: return resource, request else: return resource
def test_transport_handles_roundtrip(make_transport): transport = make_transport(state=1, content="socket.send(socket.recv())") request = Request('POST', '/echo.sock', body=StringIO(b"3::/echo.sock:ping")) transport.respond(request) transport.socket.tick() # do it manually request = Request('GET', '/echo.sock') response = transport.respond(request) expected = FFFD + b"18" + FFFD + b"3::/echo.sock:ping" actual = response.body.next() assert actual == expected
def test_transport_can_minimally_respond(make_transport): transport = make_transport() request = Request() expected = Response actual = transport.respond(request).__class__ assert actual is expected
def get_request(self, path, method="GET", body=None, **extra): env = StubWSGIRequest(path) env['REQUEST_METHOD'] = method env['wsgi.input'] = StringIO(body) env['HTTP_COOKIE'] = self.cookies.output(header='', sep='; ') env.update(extra) return Request.from_wsgi(env)
def get_request(self, path, method="GET", body=None, **extra): env = StubWSGIRequest(path) env["REQUEST_METHOD"] = method env["wsgi.input"] = StringIO(body) env["HTTP_COOKIE"] = self.cookies.output(header="", sep="; ") env.update(extra) return Request.from_wsgi(env)
def test_intercept_socket_intercepts_transported(): request = Request(uri="/foo.sock/1/websocket/46327hfjew3?foo=bar") gauntlet.intercept_socket(request) expected = ('/foo.sock', '1/websocket/46327hfjew3') actual = (request.line.uri.path.decoded, request.socket) assert actual == expected, actual
def test_intercept_socket_intercepts_handshake(): request = Request(uri="/foo.sock/1") gauntlet.intercept_socket(request) expected = ('/foo.sock', '1') actual = (request.line.uri.path.decoded, request.socket) assert actual == expected, actual
def test_transport_starts_in_state_0(make_transport): transport = make_transport() request = Request() expected = 0 actual = transport.state assert actual == expected
def test_transport_goes_to_state_1_after_first_request(make_transport): transport = make_transport() request = Request() transport.respond(request) expected = 1 actual = transport.state assert actual == expected
def __call__(self, environ, start_response): """WSGI interface. """ request = Request.from_wsgi(environ) # too big to fail :-/ response = self.handle_safely(request) response.request = request # Stick this on here at the last minute # in order to support close hooks. return response(environ, start_response)
def make_transport(content='', state=0): mk(('echo.sock.spt', content)) socket = make_socket() transport = XHRPollingTransport(socket) transport.timeout = 0.05 # for testing, could screw up the test if state == 1: transport.respond(Request(uri='/echo.sock')) return transport
def get_request(self, path, method="GET", body=None, **extra): env = StubWSGIRequest(path.encode('utf8')) env[b'REQUEST_METHOD'] = method.encode('utf8') env[b'wsgi.input'] = StringIO(body) env[b'HTTP_COOKIE'] = self.cookies.output(header='', sep='; ').encode('utf8') for k,v in extra.items(): env[k.encode('utf8')] = v.encode('utf8') return Request.from_wsgi(env)
def get_request(self, path, method="GET", body=None, **extra): env = StubWSGIRequest(path.encode('utf8')) env[b'REQUEST_METHOD'] = method.encode('utf8') env[b'wsgi.input'] = StringIO(body) env[b'HTTP_COOKIE'] = self.cookies.output(header='', sep='; ').encode('utf8') for k, v in extra.items(): env[k.encode('utf8')] = v.encode('utf8') return Request.from_wsgi(env)
def test_transport_GET_gets_data_from_socket(make_transport): transport = make_transport(state=1) message = Message.from_bytes(b"3:::Greetings, program!") transport.socket.outgoing.put(message) request = Request('GET') response = transport.respond(request) expected = FFFD + b'23' + FFFD + b'3:::Greetings, program!' actual = response.body.next() assert actual == expected
def test_transport_POST_gives_data_to_socket(make_transport): transport = make_transport(state=1) request = Request('POST', '/echo.sock', body=StringIO(b'3::/echo.sock:Greetings, program!')) transport.respond(request) expected = deque(['Greetings, program!']) actual = transport.socket.incoming.queue assert actual == expected
def from_fs(cls, fs): """Takes a path under ./fsfix using / as the path separator. """ fs = os.sep.join(fs.split('/')) request = Request.from_wsgi(StubWSGIRequest(fs)) c = Configurable.from_argv(['fsfix']) c.copy_configuration_to(request) request.fs = fs request.namespace = {} request.website = Stub() request.website.template_loader = Stub() return request
def from_fs(cls, fs): """Takes a path under ./fsfix using / as the path separator. """ fs = os.sep.join(fs.split(os.sep)) request = Request.from_wsgi(StubWSGIRequest(fs)) website = Configurable.from_argv(['--root', 'fsfix']) website.copy_configuration_to(request) request.root = join(dirname(__file__), 'fsfix') request.fs = fs request.namespace = {} request.website = website request.website.template_loader = Stub() return request
def from_fs(cls, fs, *a): """Takes a path under FSFIX using / as the path separator. """ fs = os.sep.join(fs.split(os.sep)) request = Request.from_wsgi(StubWSGIRequest(fs)) website = Website([ '--www_root', FSFIX , '--project_root', '.aspen' ] + list(a)) request.www_root = os.path.join(os.path.dirname(__file__), FSFIX) request.fs = fs request.context = {} request.website = website request._media_type = None return request
def from_fs(cls, fs, *a): """Takes a path under FSFIX using / as the path separator. """ fs = os.sep.join(fs.split(os.sep)) request = Request.from_wsgi(StubWSGIRequest(fs)) website = Configurable.from_argv([ '--www_root', FSFIX , '--project_root', '.aspen' ] + list(a)) request.www_root = os.path.join(os.path.dirname(__file__), FSFIX) request.fs = fs request.context = {} request.website = website request._media_type = None return request
def wsgi_app(self, environ, start_response): """WSGI interface. Wrap this method instead of the website object itself when to use WSGI middleware:: website = Website() website.wsgi_app = WSGIMiddleware(website.wsgi_app) """ request = Request.from_wsgi(environ) # too big to fail :-/ request.website = self response = self.handle_safely(request) response.request = request # Stick this on here at the last minute # in order to support close hooks. return response(environ, start_response)
def from_fs(cls, fs, *a): """Takes a path under FSFIX using / as the path separator. """ fs = os.sep.join(fs.split(os.sep)) uri_path = fs if fs.endswith('.spt'): uri_path = fs[:-4] request = Request.from_wsgi(StubWSGIRequest(uri_path)) website = Website([ '--www_root', FSFIX , '--project_root', os.path.join(FSFIX, '.aspen') ] + list(a)) request.www_root = os.path.join(os.path.dirname(__file__), FSFIX) request.fs = fs request.context = {} request.website = website request._media_type = None return request
def make_request(): return Request.from_diesel(DieselReq())
def test_blank_by_default(): assert_raises(AttributeError, lambda: Request().version)
def handle(path): website = Website(Configuration(['fsfix'])) return website.handle(Request.from_diesel(DieselReq(path)))
def __call__(cls, path='/'): return Request.from_wsgi(StubWSGIRequest(path))
def test_allows_certain_methods(self): for allowed in ('GEt', 'HEaD', 'PosT'): request = Request(allowed) assert security.only_allow_certain_methods(request) is None
def __call__(cls, uripath=b'/'): typecheck(uripath, str) return Request.from_wsgi(StubWSGIRequest(uripath))
def make_request(filename='echo.sock'): request = Request(uri='/echo.sock') request.website = Website([]) request.fs = fix(filename) return request
def make_request(filename='echo.sock'): request = Request(url='/echo.sock') request.website = Website([]) request.website.copy_configuration_to(request) request.fs = fix(filename) return request
def __call__(cls, uripath='/'): return Request.from_wsgi(StubWSGIRequest(uripath))
def make_request(filename='echo.sock'): request = Request(url='/echo.sock') request.website = Website([]) request.website.copy_configuration_to(request) request.fs = os.sep.join([os.path.dirname(__file__), 'fsfix', filename]) return request
def make_request(filename="echo.sock.spt"): request = Request(uri="/echo.sock") request.website = Website([]) request.fs = fix(filename) return request
def get_simplate_context(website, fs): request = Request() request.fs = fs request.website = website resource = resources.get(request) return {} if isinstance(resource, StaticResource) else resource.pages[0]
def make_socket_request(self, filename="echo.sock.spt"): request = Request(uri="/echo.sock") request.website = self.client.website request.fs = self.fs.www.resolve(filename) return request
def test_sockets_get_nonsock_returns_None(): request = Request() request.socket = None expected = None actual = sockets.get(request) assert actual is expected, actual
def test_raw_is_raw(): request = Request() expected = b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n" actual = request assert actual == expected
def test_request_redirect_works_on_instance(): request = Request() actual = raises(Response, request.redirect, '/').value.code assert actual == 302
def test_intercept_socket_intercepts_transported(): request = Request(uri="/foo.sock/1/websocket/46327hfjew3?foo=bar") actual = dispatcher.extract_socket_info(request.line.uri.path.decoded) expected = ('/foo.sock', '1/websocket/46327hfjew3') assert actual == expected
def test_intercept_socket_intercepts_handshake(): request = Request(uri="/foo.sock/1") actual = dispatcher.extract_socket_info(request.line.uri.path.decoded) expected = ('/foo.sock', '1') assert actual == expected
def __call__(self, environ, start_response): """WSGI interface. """ request = Request.from_wsgi(environ) # too big to fail :-/ response = self.handle(request) return response(environ, start_response)
def test_intercept_socket_protects_direct_access(): request = Request(uri="/foo.sock") raises(Response, dispatcher.dispatch, request)
def test_disallows_a_bunch_of_other_stuff(self): for disallowed in ('OPTIONS', 'TRACE', 'TRACK', 'PUT', 'DELETE'): request = Request(disallowed) response = raises(Response, security.only_allow_certain_methods, request).value assert response.code == 405
def __call__(self, diesel_request): """Given a Diesel request, return a Diesel response. """ request = Request.from_diesel(diesel_request) # too big to fail :-/ response = self.handle(request) return response._to_diesel(diesel_request) # sends bits, returns bool
def parse_environ_into_request(environ): return {'request': Request.from_wsgi(environ)}
def check(): website = Website(Configuration(['fsfix'])) response = website.handle(Request.from_diesel(DieselReq())) return response