def test_contemporaneous_requests(self): ''' We're going to create two request-response cycles here: Cycle 1 will begin. Cycle 2 will begin. Cycle 2 will return. Cycle 1 will return. This way, we can prove that the crosstown_traffic created by cycle 1 is not resolved by the return of cycle 2. ''' tp = ThreadPool(maxthreads=20) tp.start() self.addCleanup(tp.stop) log.debug("\n\nStarting the two stream stuff.") request1 = DummyRequest([b'r1']) request1.isSecure = lambda: False request1.content = "Nothing really here." request1.requestHeaders.addRawHeader('llamas', 'dingo') request1.client = IPv4Address("TCP", b"50.0.50.0", 5000) nameSpace.test_case = self hr = HendrixWSGIResource(reactor, tp, wsgi_application) d1 = deferToThreadPool(reactor, tp, hr.render, request1) request2 = DummyRequest([b'r2']) request2.isSecure = lambda: False request2.content = b"Nothing really here." request2.requestHeaders.addRawHeader('llamas', 'dingo') request2.client = IPv4Address("TCP", b"100.0.50.0", 5000) d2 = deferToThreadPool(reactor, tp, hr.render, request2) def woah_stop(failure): nameSpace.async_task_was_done.put_nowait(False) nameSpace.second_cycle_complete.put_nowait(False) nameSpace.ready_to_proceed_with_second_cycle.put_nowait(False) d1.addErrback(woah_stop) d2.addErrback(woah_stop) combo_deferred = gatherResults([d1, d2]) def wait_for_queue_resolution(): nameSpace.async_task_was_done.get(True, 3) combo_deferred.addCallback(lambda _: deferToThreadPool( reactor, tp, wait_for_queue_resolution)) combo_deferred.addCallback( lambda _: self.assertTrue(nameSpace.async_task_was_run)) return combo_deferred
def test_pathInfo(self): """ L{twcgi.CGIScript.render} sets the process environment I{PATH_INFO} from the request path. """ class FakeReactor: """ A fake reactor recording the environment passed to spawnProcess. """ def spawnProcess(self, process, filename, args, env, wdir): """ Store the C{env} L{dict} to an instance attribute. @param process: Ignored @param filename: Ignored @param args: Ignored @param env: The environment L{dict} which will be stored @param wdir: Ignored """ self.process_env = env _reactor = FakeReactor() resource = twcgi.CGIScript(self.mktemp(), reactor=_reactor) request = DummyRequest(["a", "b"]) request.client = address.IPv4Address("TCP", "127.0.0.1", 12345) _render(resource, request) self.assertEqual(_reactor.process_env["PATH_INFO"], "/a/b")
def test_useReactorArgument(self): """ L{twcgi.FilteredScript.runProcess} uses the reactor passed as an argument to the constructor. """ class FakeReactor: """ A fake reactor recording whether spawnProcess is called. """ called = False def spawnProcess(self, *args, **kwargs): """ Set the C{called} flag to C{True} if C{spawnProcess} is called. @param args: Positional arguments. @param kwargs: Keyword arguments. """ self.called = True fakeReactor = FakeReactor() request = DummyRequest(["a", "b"]) request.client = address.IPv4Address("TCP", "127.0.0.1", 12345) resource = twcgi.FilteredScript("dummy-file", reactor=fakeReactor) _render(resource, request) self.assertTrue(fakeReactor.called)
def request_same_or_different_thread_thread(self): hr = HendrixWSGIResource(reactor, self.tp, self.wsgi_thing) request1 = DummyRequest([b'r1']) request1.isSecure = lambda: False request1.content = b"llamas" request1.client = IPv4Address("TCP", b"50.0.50.0", 5000) d = deferToThreadPool(reactor, self.tp, hr.render, request1) d.addCallback(lambda _: request1.notifyFinish()) return d
def forge_request(uri='https://www.globaleaks.org/', headers={}, method=b'GET', client_addr=None): """ Creates a twisted.web.Request compliant request that is from an external IP address. """ _, host, path, query, frag = urlparse.urlsplit(uri) x = host.split(':') if len(x) > 1: port = int(x[1]) else: port = 80 ret = DummyRequest(['']) ret.method = method ret.uri = uri ret.path = path ret._serverName = bytes(host) if client_addr is None: ret.client = IPv4Address('TCP', '1.2.3.4', 12345) else: ret.client = client_addr def getHost(): return IPv4Address('TCP', '127.0.0.1', port) ret.getHost = getHost def notifyFinish(): return Deferred() ret.notifyFinish = notifyFinish for k, v in headers.iteritems(): ret.requestHeaders.setRawHeaders(bytes(k), [bytes(v)]) ret.headers = ret.getAllHeaders() return ret
def test_bad_status_codes_cause_no_go_in_wsgi_response(self): self.no_go_status_codes = [404, '6xx'] request = DummyRequest([b'r1']) request.isSecure = lambda: False request.content = "llamas" request.client = IPv4Address("TCP", b"50.0.50.0", 5000) finished = request.notifyFinish() self.resource.render(request) # This must wait until the WSGI response is closed. finished.addCallback( lambda _: self.assertFalse(self.nameSpace.async_task_was_run)) return finished
def test_bad_status_codes_cause_no_go_in_wsgi_response(self): self.no_go_status_codes = [404, '6xx'] request = DummyRequest([b'r1']) request.isSecure = lambda: False request.content = "llamas" request.client = IPv4Address("TCP", b"50.0.50.0", 5000) finished = request.notifyFinish() self.resource.render(request) # This must wait until the WSGI response is closed. finished.addCallback( lambda _: self.assertFalse( self.nameSpace.async_task_was_run ) ) return finished
def forge_request(uri=b'https://www.globaleaks.org/', headers=None, body='', client_addr=None, method=b'GET'): """ Creates a twisted.web.Request compliant request that is from an external IP address. """ if headers is None: headers = {} _, host, path, query, frag = urlsplit(uri) x = host.split(b':') if len(x) > 1: port = int(x[1]) else: port = 80 request = DummyRequest([b'']) request.tid = 1 request.method = method request.uri = uri request.path = path request._serverName = host request.code = 200 request.client_ip = b'127.0.0.1' request.client_proto = b'https' request.client_using_tor = False def getResponseBody(): # Ugh, hack. Twisted returns this all as bytes, and we want it as str if isinstance(request.written[0], binary_type): return b''.join(request.written) else: return ''.join(request.written) request.getResponseBody = getResponseBody if client_addr is None: request.client = IPv4Address('TCP', b'1.2.3.4', 12345) else: request.client = client_addr def getHost(): return IPv4Address('TCP', b'127.0.0.1', port) request.getHost = getHost def notifyFinish(): return Deferred() request.notifyFinish = notifyFinish request.requestHeaders.setRawHeaders('host', [b'127.0.0.1']) request.requestHeaders.setRawHeaders('user-agent', [b'NSA Agent']) for k, v in headers.items(): request.requestHeaders.setRawHeaders(k, [v]) request.headers = request.getAllHeaders() class fakeBody(object): def read(self): if isinstance(body, dict): ret = json.dumps(body) else: ret = body if isinstance(ret, text_type): ret = ret.encode('utf-8') return ret def close(self): pass request.content = fakeBody() return request
def forge_request(uri=b'https://www.globaleaks.org/', headers=None, body='', client_addr=None, method=b'GET'): """ Creates a twisted.web.Request compliant request that is from an external IP address. """ if headers is None: headers = {} _, host, path, query, frag = urlsplit(uri) x = host.split(b':') if len(x) > 1: port = int(x[1]) else: port = 80 request = DummyRequest([b'']) request.tid = 1 request.method = method request.uri = uri request.path = path request._serverName = host request.code = 200 request.client_ip = b'127.0.0.1' request.client_proto = b'https' request.client_using_tor = False def getResponseBody(): # Ugh, hack. Twisted returns this all as bytes, and we want it as str if isinstance(request.written[0], binary_type): return b''.join(request.written) else: return ''.join(request.written) request.getResponseBody = getResponseBody if client_addr is None: request.client = IPv4Address('TCP', b'1.2.3.4', 12345) else: request.client = client_addr def getHost(): return IPv4Address('TCP', b'127.0.0.1', port) request.getHost = getHost def notifyFinish(): return Deferred() request.notifyFinish = notifyFinish request.requestHeaders.setRawHeaders('host', [b'127.0.0.1']) request.requestHeaders.setRawHeaders('user-agent', [b'NSA Agent']) for k, v in headers.items(): request.requestHeaders.setRawHeaders(k, [v]) request.headers = request.getAllHeaders() class fakeBody(object): def read(self): if isinstance(body, dict): ret = json.dumps(body) else: ret = body if isinstance(ret, text_type): ret = ret.encode('utf-8') return ret def close(self): pass request.content = fakeBody() return request
def test_contemporaneous_requests(): ''' We're going to create two request-response cycles here: Cycle 1 will begin. Cycle 2 will begin. Cycle 2 will return. Cycle 1 will return. This way, we can prove that the crosstown_traffic created by cycle 1 is not resolved by the return of cycle 2. ''' tp = ThreadPool(maxthreads=20) tp.start() log.debug("\n\nStarting the two stream stuff.") request1 = DummyRequest([b'r1']) request1.isSecure = lambda: False request1.content = "Nothing really here." request1.requestHeaders.addRawHeader('llamas', 'dingo') request1.client = IPv4Address("TCP", b"50.0.50.0", 5000) hr = HendrixWSGIResource(reactor, tp, wsgi_application) yield deferToThreadPool(reactor, tp, hr.render, request1) request2 = DummyRequest([b'r2']) request2.isSecure = lambda: False request2.content = b"Nothing really here." request2.requestHeaders.addRawHeader('llamas', 'dingo') request2.client = IPv4Address("TCP", b"100.0.50.0", 5000) yield deferToThreadPool(reactor, tp, hr.render, request2) # def woah_stop(failure): # nameSpace.async_task_was_done.put_nowait(False) # nameSpace.second_cycle_complete.put_nowait(False) # nameSpace.ready_to_proceed_with_second_cycle.put_nowait(False) # # d1.addErrback(woah_stop) # d2.addErrback(woah_stop) # combo_deferred = gatherResults([d1, d2]) # yield d1 # yield d2 # combo_deferred = DeferredList([d1, d2]) def wait_for_queue_resolution(): nameSpace.async_task_was_done.get(True, 3) # combo_deferred.addCallback( # lambda _: # ) # yield deferToThreadPool(reactor, tp, wait_for_queue_resolution) # combo_deferred.addCallback( # lambda _: # ) assert nameSpace.async_task_was_run tp.stop()
def forge_request(uri='https://www.globaleaks.org/', headers=None, body='', client_addr=None, method='GET', handler_cls=None, attached_file={}): """ Creates a twisted.web.Request compliant request that is from an external IP address. """ if headers is None: headers = {} _, host, path, query, frag = urlparse.urlsplit(uri) x = host.split (':') if len(x) > 1: port = int(x[1]) else: port = 80 request = DummyRequest(['']) request.method = method request.uri = uri request.path = path request._serverName = bytes(host) request.code = 200 request.client_ip = '127.0.0.1' request.client_proto = 'https' request.client_using_tor = False def getResponseBody(): return ''.join(request.written) request.getResponseBody = getResponseBody if client_addr is None: request.client = IPv4Address('TCP', '1.2.3.4', 12345) else: request.client = client_addr def getHost(): return IPv4Address('TCP', '127.0.0.1', port) request.getHost = getHost def notifyFinish(): return Deferred() request.notifyFinish = notifyFinish for k, v in headers.items(): request.requestHeaders.setRawHeaders(bytes(k), [bytes(v)]) request.headers = request.getAllHeaders() request.args = {} if attached_file is not None: request.args = {'file': [attached_file]} class fakeBody(object): def read(self): if isinstance(body, dict): return json.dumps(body) else: return body def close(self): pass request.content = fakeBody() return request
def request(self, jbody=None, user_id=None, role=None, headers=None, body='', path=None, remote_ip='0.0.0.0', method='MOCK', handler_cls=None, attached_file={}, kwargs={}): """ Constructs a handler for preforming mock requests using the bag of params described below. Args: jbody: The body of the request as a dict (it will be automatically converted to string) body: The body of the request as a string user_id: when simulating authentication the session should be bound to a certain user_id. role: when simulating authentication the session should be bound to a certain role. method: HTTP method, e.g. "GET" or "POST" headers: Dict of headers to pass on the request remote_ip: If a particular remote_ip should be set. handler_cls: The type of handler that will respond to the request. If this is not set self._handler is used. attached_file: A dict to place in the request.args.files obj """ if jbody and not body: body = json.dumps(jbody) elif body and jbody: raise ValueError('jbody and body in conflict') if handler_cls is None: handler_cls = self._handler request = DummyRequest(['']) def getResponseBody(): return ''.join(request.written) request.path = '' request.code = 200 request.language = 'en' request.client_ip = '127.0.0.1' request.client_proto = 'https' request.client_using_tor = False request.getResponseBody = getResponseBody request.client = IPv4Address('TCP', '1.2.3.4', 12345) request.args = {} if attached_file is not None: request.args = {'file': [attached_file]} if headers is not None: for k, v in headers.iteritems(): request.requestHeaders.setRawHeaders(bytes(k), [bytes(v)]) request.headers = request.getAllHeaders() from globaleaks.rest import api x = api.APIResourceWrapper() x.preprocess(request) if path is not None: if not path.startswith('/'): raise ValueError('Must pass a valid url path') request.path = path class fakeBody(object): def read(self): return body def close(self): pass request.content = fakeBody() from globaleaks.rest.api import decorate_method if not getattr(handler_cls, 'decorated', False): for method in ['get', 'post', 'put', 'delete']: if getattr(handler_cls, method, None) is not None: decorate_method(handler_cls, method) handler_cls.decorated = True handler = handler_cls(request, **kwargs) if user_id is None and role is not None: if role == 'admin': user_id = self.dummyAdminUser['id'] elif role == 'receiver': user_id = self.dummyReceiverUser_1['id'] elif role == 'custodian': user_id = self.dummyCustodianUser['id'] if role is not None: session = GLSession(user_id, role, 'enabled') handler.request.headers['x-session'] = session.id return handler