def testClearTimer(self): port = randint(2**10, 2**16) reactor = Reactor() expectedrequest = "GET / HTTP/1.1\r\nHost: localhost\r\nUser-Agent: Weightless/v%s\r\n\r\n" % WlVersion serverThread = server(port, "HTTP/1.1 200 OK\r\n\r\nresponse", expectedrequest) self.exception = None sentData = [] def send(data): sentData.append(data) def throw(exception): self.exception = exception reader = HttpReaderFacade(reactor, "http://localhost:%s" % port, send, throw, timeout=0.01, recvSize=3) while not self.exception: reactor.step() sleep(0.02) # 2 * timeout, just to be sure self.assertTrue(isinstance(self.exception, StopIteration))
def testPost(self): with Reactor() as reactor: request = "POST / HTTP/1.1\r\nHost: localhost\r\nTransfer-Encoding: chunked\r\nSOAPAction: blah\r\nUser-Agent: Weightless/v%s\r\n\r\n1\r\nA\r\n1\r\nB\r\n1\r\nC\r\n0\r\n\r\n" % WlVersion serverThread = server(self.port, "HTTP/1.1 200 OK\r\n\r\nresponse", request, loop=9) sentData = [] done = [] def send(data): sentData.append(data) def throw(exception): if isinstance(exception, StopIteration): done.append(True) def next(): yield "A" yield "B" yield "C" yield None reader = HttpReaderFacade(reactor, "http://localhost:%s" % self.port, send, errorHandler=throw, timeout=0.5, headers={'SOAPAction': 'blah'}, bodyHandler=next) reactor.addTimer(3.0, lambda: self.fail("Test Stuck")) while not done: reactor.step() self.assertEquals(['response'], sentData[1:]) self.assertEquals('200', sentData[0]['StatusCode']) expected = 'POST / HTTP/1.1\r\nHost: localhost\r\nTransfer-Encoding: chunked\r\nSOAPAction: blah\r\nUser-Agent: Weightless/v%s\r\n\r\n' % WlVersion + '1\r\nA\r\n' + '1\r\nB\r\n' + '1\r\nC\r\n' + '0\r\n\r\n' self.assertEquals(expected, "".join(request))
def testEmptyPath(self): with stdout_replaced(): with Reactor() as reactor: request = "GET / HTTP/1.1\r\nHost: localhost\r\nUser-Agent: Weightless/v%s\r\n\r\n" % WlVersion serverThread = server(self.port, "HTTP/1.1 200 OK\r\n\r\n", request) reader = HttpReaderFacade(reactor, "http://localhost:%s" % self.port, lambda data: 'a') reactor.step() reactor.step() serverThread.join()
def testEmptyPath(self): port = randint(2**10, 2**16) reactor = Reactor() request = "GET / HTTP/1.1\r\nHost: localhost\r\nUser-Agent: Weightless/v%s\r\n\r\n" % WlVersion serverThread = server(port, "HTTP/1.1 200 OK\r\n\r\n", request) reader = HttpReaderFacade(reactor, "http://localhost:%s" % port, lambda data: 'a') reactor.step() reactor.step() serverThread.join()
def testTimeoutOnInvalidRequest(self): with Reactor() as reactor: expectedrequest = "GET / HTTP/1.1\r\nHost: localhost\r\nUser-Agent: Weightless/v%s\r\n\r\n" % WlVersion serverThread = server(self.port, "HTTP/1.1 *invalid reponse* 200 OK\r\n\r\n", expectedrequest) errorArgs = [] def error(exception): errorArgs.append(exception) reader = HttpReaderFacade(reactor, "http://localhost:%s" % self.port, None, error, timeout=0.01) while not errorArgs: reactor.step() serverThread.join() self.assertEquals('timeout while receiving data', str(errorArgs[0]))