File: conftest.py

package info (click to toggle)
circuits 3.1.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,756 kB
  • sloc: python: 15,945; makefile: 130
file content (89 lines) | stat: -rw-r--r-- 2,105 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Module:   conftest
# Date:     10 February 2010
# Author:   James Mills, prologic at shortcircuit dot net dot au

"""py.test config"""

import os

import pytest

from circuits.net.sockets import close
from circuits.web import Server, Static
from circuits import handler, Component, Debugger
from circuits.web.client import Client, request


DOCROOT = os.path.join(os.path.dirname(__file__), "static")


class WebApp(Component):

    channel = "web"

    def init(self):
        self.closed = False

        self.server = Server(0).register(self)
        Static("/static", DOCROOT, dirlisting=True).register(self)


class WebClient(Client):

    def init(self, *args, **kwargs):
        self.closed = False

    def __call__(self, method, path, body=None, headers={}):
        waiter = pytest.WaitEvent(self, "response", channel=self.channel)
        self.fire(request(method, path, body, headers))
        assert waiter.wait()

        return self.response

    @handler("closed", channel="*", priority=1.0)
    def _on_closed(self):
        self.closed = True


@pytest.fixture(scope="module")
def webapp(request):
    webapp = WebApp()

    if hasattr(request.module, "application"):
        from circuits.web.wsgi import Gateway
        application = getattr(request.module, "application")
        Gateway({"/": application}).register(webapp)

    Root = getattr(request.module, "Root", None)
    if Root is not None:
        Root().register(webapp)

    if request.config.option.verbose:
        Debugger().register(webapp)

    waiter = pytest.WaitEvent(webapp, "ready")
    webapp.start()
    assert waiter.wait()

    def finalizer():
        webapp.fire(close(), webapp.server)
        webapp.stop()

    request.addfinalizer(finalizer)

    return webapp


@pytest.fixture(scope="module")
def webclient(request, webapp):
    webclient = WebClient()
    waiter = pytest.WaitEvent(webclient, "ready", channel=webclient.channel)
    webclient.register(webapp)
    assert waiter.wait()

    def finalizer():
        webclient.unregister()

    request.addfinalizer(finalizer)

    return webclient