PageRenderTime 24ms CodeModel.GetById 12ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/web/test_servers.py

https://bitbucket.org/prologic/circuits/
Python | 150 lines | 100 code | 49 blank | 1 comment | 4 complexity | 58f76befc57e4c96d809ef3779d8072f MD5 | raw file
  1#!/usr/bin/env python
  2
  3import pytest
  4
  5from os import path
  6from socket import gaierror
  7
  8from circuits.web import Controller
  9from circuits import handler, Component
 10from circuits.web import BaseServer, Server
 11
 12from .helpers import urlopen, URLError
 13
 14CERTFILE = path.join(path.dirname(__file__), "cert.pem")
 15
 16
 17class BaseRoot(Component):
 18
 19    channel = "web"
 20
 21    def request(self, request, response):
 22        return "Hello World!"
 23
 24
 25class Root(Controller):
 26
 27    def index(self):
 28        return "Hello World!"
 29
 30
 31class MakeQuiet(Component):
 32
 33    @handler("ready", channel="*", priority=1.0)
 34    def _on_ready(self, event, *args):
 35        event.stop()
 36
 37
 38def test_baseserver(manager, watcher):
 39    server = BaseServer(0).register(manager)
 40    MakeQuiet().register(server)
 41    watcher.wait("ready")
 42
 43    BaseRoot().register(server)
 44    watcher.wait("registered")
 45
 46    try:
 47        f = urlopen(server.http.base)
 48    except URLError as e:
 49        if type(e[0]) is gaierror:
 50            f = urlopen("http://127.0.0.1:9000")
 51        else:
 52            raise
 53
 54    s = f.read()
 55    assert s == b"Hello World!"
 56
 57    server.unregister()
 58    watcher.wait("unregistered")
 59
 60
 61def test_server(manager, watcher):
 62    server = Server(0).register(manager)
 63    MakeQuiet().register(server)
 64    watcher.wait("ready")
 65
 66    Root().register(server)
 67
 68    try:
 69        f = urlopen(server.http.base)
 70    except URLError as e:
 71        if type(e[0]) is gaierror:
 72            f = urlopen("http://127.0.0.1:9000")
 73        else:
 74            raise
 75
 76    s = f.read()
 77    assert s == b"Hello World!"
 78
 79    server.unregister()
 80    watcher.wait("unregistered")
 81
 82
 83def test_secure_server(manager, watcher):
 84    pytest.importorskip("ssl")
 85
 86    server = Server(0, secure=True, certfile=CERTFILE).register(manager)
 87    MakeQuiet().register(server)
 88    watcher.wait("ready")
 89
 90    Root().register(server)
 91
 92    try:
 93        f = urlopen(server.http.base)
 94    except URLError as e:
 95        if type(e[0]) is gaierror:
 96            f = urlopen("http://127.0.0.1:9000")
 97        else:
 98            raise
 99
100    s = f.read()
101    assert s == b"Hello World!"
102
103    server.unregister()
104    watcher.wait("unregistered")
105
106
107def test_unixserver(manager, watcher, tmpdir):
108    if pytest.PLATFORM == "win32":
109        pytest.skip("Unsupported Platform")
110
111    sockpath = tmpdir.ensure("test.sock")
112    socket = str(sockpath)
113
114    server = Server(socket).register(manager)
115    MakeQuiet().register(server)
116    watcher.wait("ready")
117
118    Root().register(server)
119
120    assert path.basename(server.host) == "test.sock"
121
122    server.unregister()
123    watcher.wait("unregistered")
124
125
126def test_multi_servers(manager, watcher):
127    pytest.importorskip("ssl")
128
129    insecure_server = Server(0, channel="insecure")
130    secure_server = Server(
131        0,
132        channel="secure", secure=True, certfile=CERTFILE
133    )
134
135    server = (insecure_server + secure_server).register(manager)
136    MakeQuiet().register(server)
137    watcher.wait("ready")
138
139    Root().register(server)
140
141    f = urlopen(insecure_server.http.base)
142    s = f.read()
143    assert s == b"Hello World!"
144
145    f = urlopen(secure_server.http.base)
146    s = f.read()
147    assert s == b"Hello World!"
148
149    server.unregister()
150    watcher.wait("unregistered")