class TestHandlers(object): def setup(self): self.server = HTTPServer(debug=True) self.client = MockClient(self.server) def test_static(self): self.server.register(*serve_static(address='/', root=".")) reply, headers, body = self.client('GET', '/handlers.py') data = open('handlers.py').read() eq_(body, data) reply, headers, body = self.client( 'GET', '/handlers.py', if_modified_since=headers['LAST-MODIFIED']) eq_(reply[1], '304') def test_parts_handlers(self): self.server.register(lambda r: True, lambda r: ["123", "456", "789"]) request, header, body = self.client('GET', '/') eq_(body, "123456789")
class TestHandlers(object): def setup(self): self.server = HTTPServer(debug=True) self.client = MockClient(self.server) def test_static(self): self.server.register(*serve_static(address='/', root=".")) reply, headers, body = self.client('GET', '/handlers.py') data = open('handlers.py').read() eq_(body, data) reply, headers, body = self.client('GET', '/handlers.py', if_modified_since=headers['LAST-MODIFIED']) eq_(reply[1], '304') def test_parts_handlers(self): self.server.register(lambda r: True, lambda r: ["123", "456", "789"]) request, header, body = self.client('GET', '/') eq_(body, "123456789")
class TestHandlers(object): def setup(self): self.server = HTTPServer(debug=True) self.client = MockClient(self.server) def test_static(self): self.server.register(*serve_static(address='/', root=".")) reply, headers, body = self.client('GET', '/handlers.py') data = open('handlers.py').read() eq_(body, data) def test_parts_handlers(self): self.server.register(lambda r: True, lambda r: ["123", "456", "789"]) request, header, body = self.client('GET', '/') eq_(body, "123456789") def test_autoindex(self): self.server.register(*serve_static(address='/', root="/home/an4ezz", autoindex=True)) reply, headers, body = self.client('GET', '/') assert '<a href="buzannka-second/"><b>buzannka-second/</b></a><br/>\n' in body
class TestHandlers(object): def setup(self): self.server = HTTPServer(debug=True) self.client = MockClient(self.server) def test_static(self): self.server.register(*serve_static(address='/', root=".")) reply, headers, body = self.client('GET', '/handlers.py') data = open('handlers.py').read() eq_(body, data) def test_parts_handlers(self): self.server.register(lambda r: True, lambda r: ["123", "456", "789"]) request, header, body = self.client('GET', '/') eq_(body, "123456789") def test_autoindex(self): self.server.register( *serve_static(address='/', root="/home/an4ezz", autoindex=True)) reply, headers, body = self.client('GET', '/') assert '<a href="buzannka-second/"><b>buzannka-second/</b></a><br/>\n' in body
def setup(self): self.server = HTTPServer(11112, debug=True) self.client = MockClient(self.server)
class TestHTTPServer: def setup(self): self.server = HTTPServer(11112, debug=True) self.client = MockClient(self.server) def test_on_connect(self): handler = lambda request: request.reply('<H1>"Hello world!!!!"</H1>', content_type='text/html') pattern = lambda request: request.url == "/html/" self.server.register(pattern, handler) reply, headers, body = self.client('GET', '/html/') eq_(reply, ['HTTP/1.0', '200', 'OK']) eq_(headers['CONTENT-TYPE'], 'text/html') def test_change_handlers(self): def pattern(request): if request.url == '/html/': request.test = 'hell world' return True self.server.register( pattern, lambda request: request.reply(request.test, content_type='text/html')) reply, headers, body = self.client('GET', '/html/') eq_(body, 'hell world') def test_multihandlers(self): self.server.register(lambda request: request.method == "POST", lambda request: request.reply('post')) self.server.register(lambda request: request.url == '/test/', lambda request: request.reply('other')) reply, headers, body = self.client('POST', '/test/') eq_('post', body) reply, headers, body = self.client('GET', '/test/') eq_('other', body) def test_multiline_body(self): self.server.register( lambda request: request.url == '/multiline', lambda request: request.reply('<strong>multi-\r\nline\r\nbody</strong>', content_type='text/html')) reply, headers, body = self.client('GET', '/multiline') eq_('<strong>multi-\r\nline\r\nbody</strong>', body) def test_handlers(self): self.server.register(lambda r: r.url == True, lambda r: None) request, headers, body = self.client('GET', '/') eq_(request[1], '404') eq_(body, '<h1>404 Not Found</h1>') self.server.register(lambda r: r.url == '/str', lambda r: 'This is a string reply') request, headers, body = self.client('GET', '/str') eq_(request[1], '200') eq_(body, 'This is a string reply') self.server.register(lambda r: r.url == 'utf', lambda r: 'This is a utf-8 reply'.decode('utf-8')) request, headers, body = self.client('GET', 'utf') eq_(request[1], '200') eq_(body, 'This is a utf-8 reply') self.server.register(lambda r: r.url == '/iter', lambda r: ['asd', 'fgh']) request, headers, body = self.client('GET', '/iter') eq_(request[1], '200') eq_(body, 'asdfgh') self.server.register(lambda r: r.url == '/file', lambda r: open('handlers.py').read(9)) request, headers, body = self.client('GET', '/file') eq_(request[1], '200') eq_(body, 'import os') self.server.options["debug"] = False self.server.register(lambda r: r.url == '/crash/me/', lambda r: no_you) request, headers, body = self.client('GET', '/crash/me/') eq_(request[1], '500') eq_(request[2], 'Internal Server Error')
class TestHTTPServer: def setup(self): self.server = HTTPServer(11112, debug=True) self.client = MockClient(self.server) def test_on_connect(self): handler = lambda request: request.reply('<H1>"Hello world!!!!"</H1>',content_type = 'text/html') pattern = lambda request: request.url =="/html/" self.server.register(pattern, handler) reply, headers, body = self.client('GET', '/html/') eq_(reply, ['HTTP/1.0', '200', 'OK']) eq_(headers['CONTENT-TYPE'], 'text/html') def test_change_handlers(self): def pattern(request): if request.url == '/html/': request.test = 'hell world' return True self.server.register(pattern, lambda request: request.reply(request.test, content_type='text/html')) reply, headers, body = self.client('GET', '/html/') eq_(body, 'hell world') def test_multihandlers(self): self.server.register(lambda request: request.method == "POST", lambda request: request.reply('post')) self.server.register(lambda request: request.url == '/test/', lambda request: request.reply('other')) reply, headers, body = self.client ('POST', '/test/') eq_('post', body) reply, headers, body = self.client('GET', '/test/') eq_('other', body) def test_multiline_body(self): self.server.register( lambda request: request.url == '/multiline', lambda request: request.reply('<strong>multi-\r\nline\r\nbody</strong>', content_type='text/html') ) reply, headers, body = self.client('GET', '/multiline') eq_('<strong>multi-\r\nline\r\nbody</strong>', body) def test_handlers(self): self.server.register(lambda r: r.url == True, lambda r: None) request, headers, body = self.client('GET', '/') eq_(request[1], '404') eq_(body, '<h1>404 Not Found</h1>') self.server.register(lambda r: r.url == '/str', lambda r: 'This is a string reply') request, headers, body = self.client('GET', '/str') eq_(request[1], '200') eq_(body, 'This is a string reply') self.server.register(lambda r: r.url == 'utf', lambda r: 'This is a utf-8 reply'.decode('utf-8')) request, headers, body = self.client('GET', 'utf') eq_(request[1], '200') eq_(body, 'This is a utf-8 reply') self.server.register(lambda r: r.url == '/iter', lambda r: ['asd', 'fgh']) request, headers, body = self.client('GET', '/iter') eq_(request[1], '200') eq_(body, 'asdfgh') self.server.register(lambda r: r.url == '/file', lambda r: open('handlers.py').read(9)) request, headers, body = self.client('GET', '/file') eq_(request[1], '200') eq_(body, 'import os') self.server.options["debug"] = False self.server.register(lambda r: r.url == '/crash/me/', lambda r: no_you) request, headers, body = self.client('GET', '/crash/me/') eq_(request[1], '500') eq_(request[2], 'Internal Server Error')
class TestHandlers(object): def setup(self): self.server = HTTPServer(debug=True) self.client = MockClient(self.server) def test_yield(self): def handler(request): request.start_response(content_type='text/plain') request.headers_sent = True def gen(): yield 'first\n' yield 'second\n' yield 'third\n' return gen() self.server.register(lambda request: request.url == "/yield", handler) reply, headers, body = self.client('GET', '/yield') eq_(reply[1], '200') eq_(body, 'first\nsecond\nthird\n') def test_static(self): stat = os.stat('handlers.py') self.server.register(*serve_static(address='/', root=".")) reply, headers, body = self.client('GET', '/handlers.py') data = open('handlers.py').read() eq_(body, data) reply, headers, body = self.client('GET', '/handlers.py', if_modified_since=headers['LAST-MODIFIED']) eq_(reply[1], '304') eq_(body, '') date = datetime.datetime.utcfromtimestamp(stat.st_mtime + 3600) reply, headers, body = self.client('GET', '/handlers.py', if_modified_since=date.strftime(DATETIME)) eq_(reply[1], '304') eq_(body, '') date = datetime.datetime.utcfromtimestamp(stat.st_mtime - 3600) reply, headers, body = self.client('GET', '/handlers.py', if_modified_since=date.strftime(DATETIME)) eq_(reply[1], '200') eq_(body, data) reply, headers, body = self.client('GET', '/handlers.py', if_modified_since=None) eq_(reply[1], '200') eq_(body, data) reply, headers, body = self.client('GET', '/handlers.py', if_modified_since='a lot of garbage') eq_(reply[1], '200') eq_(body, data) reply, headers, body = self.client('GET', '/404errorpage.html') eq_(reply[1], '404') reply, headers, body = self.client('GET', '/../../../../root/forbidden.html') eq_(reply[1], '403') reply, headers, body = self.client('GET', '/.') print reply, headers, body eq_(reply[1], '403') def test_parts_handlers(self): self.server.register(lambda r: True, lambda r: ["123", "456", "789"]) request, header, body = self.client('GET', '/') eq_(body, "123456789")