class ServerTestCase(StangoTestCase): def setup(self): self.manager = Stango() self.manager.index_file = 'index.html' def serve(func): @functools.wraps(func) def wrapper(self): gen = func(self) httpd = next(gen) httpd.allow_reuse_address = True server_thread = Thread(target=httpd.serve_forever) server_thread.start() try: self.assert_raises(StopIteration, gen.send, None) finally: httpd.shutdown() server_thread.join() httpd.socket.close() return wrapper @serve def test_simple(self): self.manager.files = Files( ('', view_value('foobar')), ) yield self.manager.make_server('127.0.0.1', 8080) data = urlopen('http://127.0.0.1:8080/') self.eq(data.read(), b'foobar') self.eq(data.info()['Content-Type'], 'text/html') @serve def test_real_path(self): self.manager.files = Files( ('', view_value('bazbuzz')), ) yield self.manager.make_server('127.0.0.1', 8080) data = urlopen('http://127.0.0.1:8080/index.html') self.eq(data.read(), b'bazbuzz') self.eq(data.info()['Content-Type'], 'text/html') @serve def test_404(self): self.manager.files = Files( ('', view_value('foobar')), ) yield self.manager.make_server('127.0.0.1', 8080) url = 'http://127.0.0.1:8080/nonexistent' exc = self.assert_raises(HTTPError, urlopen, url) self.eq(exc.code, 404)
class ServerTestCase(StangoTestCase): def setup(self): self.manager = Stango() self.manager.index_file = 'index.html' def serve(func): @functools.wraps(func) def wrapper(self): gen = func(self) httpd = next(gen) httpd.allow_reuse_address = True server_thread = Thread(target=httpd.serve_forever) server_thread.start() try: self.assert_raises(StopIteration, gen.send, None) finally: httpd.shutdown() server_thread.join() httpd.socket.close() return wrapper @serve def test_simple(self): self.manager.files = Files(('', view_value('foobar')), ) yield self.manager.make_server('127.0.0.1', 8080) data = urlopen('http://127.0.0.1:8080/') self.eq(data.read(), b'foobar') self.eq(data.info()['Content-Type'], 'text/html') @serve def test_real_path(self): self.manager.files = Files(('', view_value('bazbuzz')), ) yield self.manager.make_server('127.0.0.1', 8080) data = urlopen('http://127.0.0.1:8080/index.html') self.eq(data.read(), b'bazbuzz') self.eq(data.info()['Content-Type'], 'text/html') @serve def test_404(self): self.manager.files = Files(('', view_value('foobar')), ) yield self.manager.make_server('127.0.0.1', 8080) url = 'http://127.0.0.1:8080/nonexistent' exc = self.assert_raises(HTTPError, urlopen, url) self.eq(exc.code, 404)