def testDealWithChunkedResponse(self): with Reactor() as reactor: sentData = [] done = [] expectedrequest = "GET / HTTP/1.1\r\nHost: localhost\r\nUser-Agent: Weightless/v%s\r\n\r\n" % WlVersion serverThread = server(self.port, "\r\n".join("""HTTP/1.1 302 Found Date: Fri, 26 Oct 2007 07:23:26 GMT Server: Apache/2.2.3 (Debian) mod_python/3.2.10 Python/2.4.4 mod_ssl/2.2.3 OpenSSL/0.9.8c Location: /page/softwarestudio.page/show Transfer-Encoding: chunked Content-Type: text/html; charset=utf-8 4F <p>The document has moved <a href="/page/softwarestudio.page/show">here</a></p> 0 """.split("\n")), expectedrequest) class Handler: def send(self, data): sentData.append(data) def throw(self, exception): if isinstance(exception, StopIteration): done.append(True) reader = HttpReader(reactor, Connector(reactor, 'localhost', int(self.port)), Handler(), 'GET', 'localhost', '/', recvSize=5) reactor.addTimer(0.2, lambda: self.fail("Test Stuck")) while not done: reactor.step() self.assertEquals("""<p>The document has moved <a href="/page/softwarestudio.page/show">here</a></p>""", "".join(sentData[1:]))
def testTimeoutOnServerGoingSilentAfterHeaders(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\n", expectedrequest, delay=1) errorArgs = [] class Handler: def send(self, data): pass def throw(self, exception): errorArgs.append(exception) def error(exception): errorArgs.append(exception) reader = HttpReader(reactor, Connector(reactor, 'localhost', port), Handler(), "GET", "localhost", "/", timeout=0.01) reactor.addTimer(0.2, lambda: self.fail("Test Stuck")) while not errorArgs: reactor.step() serverThread.join() self.assertEquals('timeout while receiving data', str(errorArgs[0]))
def testTimeoutOnSilentServer(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, "", expectedrequest) errorArgs = [] class Handler: def throw(self, exception): errorArgs.append(exception) def error(exception): errorArgs.append(exception) reader = HttpReader(reactor, Connector(reactor, 'localhost', self.port), Handler(), "GET", "localhost", "/", timeout=0.01) reactor.addTimer(0.2, lambda: self.fail("Test Stuck")) while not errorArgs: reactor.step() serverThread.join() self.assertEquals('timeout while receiving data', str(errorArgs[0]))
def testRequestAndHeaders(self): port = randint(2**10, 2**16) v = 'vx.y.z' expectedrequest = "GET /aap/noot/mies HTTP/1.1\r\nHost: localhost\r\nUser-Agent: Weightless/%s\r\n\r\n" % v dataReceived = [] serverThread = server( port, "HTTP/1.1 200 OK\r\ncOnteNt-type: text/html\r\n\r\nHello World!", expectedrequest) class Generator(object): def __init__(self): self.throw = None self.next = None def send(self, data): dataReceived.append(data) reactor = Reactor() connection = Connector(reactor, 'localhost', port) reader = HttpReader(reactor, connection, Generator(), 'GET', 'localhost', '/aap/noot/mies', recvSize=7) reactor.addTimer(0.1, lambda: self.fail("Test Stuck")) while 'Hello World!' != "".join((x for x in dataReceived[1:] if x)): reactor.step() serverThread.join() self.assertEquals( { 'HTTPVersion': '1.1', 'StatusCode': '200', 'ReasonPhrase': 'OK', 'Headers': { 'Content-Type': 'text/html' }, 'Client': ('127.0.0.1', MATCHALL) }, dataReceived[0])