summaryrefslogtreecommitdiff
path: root/crocoite/test_controller.py
blob: 7216a42064105a1c4832ba4110b5954ff9180d00 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# Copyright (c) 2017–2018 crocoite contributors
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import asyncio

from yarl import URL
from aiohttp import web

import pytest

from .logger import Logger
from .controller import ControllerSettings, SinglePageController, SetEntry, \
        IdleStateTracker
from .browser import PageIdle
from .devtools import Process
from .test_browser import loader

@pytest.mark.asyncio
async def test_controller_timeout ():
    """ Make sure the controller terminates, even if the site keeps reloading/fetching stuff """

    async def f (req):
        return web.Response (body="""<html>
<body>
<p>hello</p>
<script>
window.setTimeout (function () { window.location = '/' }, 250);
window.setInterval (function () { fetch('/').then (function (e) { console.log (e) }) }, 150);
</script>
</body>
</html>""", status=200, content_type='text/html', charset='utf-8')

    url = URL.build (scheme='http', host='localhost', port=8080)
    app = web.Application ()
    app.router.add_route ('GET', '/', f)
    runner = web.AppRunner(app)
    await runner.setup()
    site = web.TCPSite(runner, url.host, url.port)
    await site.start()

    loop = asyncio.get_event_loop ()
    try:
        logger = Logger ()
        settings = ControllerSettings (idleTimeout=1, timeout=5)
        controller = SinglePageController (url=url, logger=logger,
                service=Process (), behavior=[], settings=settings)
        # give the controller a little more time to finish, since there are
        # hard-coded asyncio.sleep calls in there right now.
        # XXX fix this
        before = loop.time ()
        await asyncio.wait_for (controller.run (), timeout=settings.timeout*2)
        after = loop.time ()
        assert after-before >= settings.timeout, (settings.timeout*2, after-before)
    finally:
        # give the browser some time to close before interrupting the
        # connection by destroying the HTTP server
        await asyncio.sleep (1)
        await runner.cleanup ()

@pytest.mark.asyncio
async def test_controller_idle_timeout ():
    """ Make sure the controller terminates, even if the site keeps reloading/fetching stuff """

    async def f (req):
        return web.Response (body="""<html>
<body>
<p>hello</p>
<script>
window.setInterval (function () { fetch('/').then (function (e) { console.log (e) }) }, 2000);
</script>
</body>
</html>""", status=200, content_type='text/html', charset='utf-8')

    url = URL.build (scheme='http', host='localhost', port=8080)
    app = web.Application ()
    app.router.add_route ('GET', '/', f)
    runner = web.AppRunner(app)
    await runner.setup()
    site = web.TCPSite(runner, url.host, url.port)
    await site.start()

    loop = asyncio.get_event_loop ()
    try:
        logger = Logger ()
        settings = ControllerSettings (idleTimeout=1, timeout=60)
        controller = SinglePageController (url=url, logger=logger,
                service=Process (), behavior=[], settings=settings)
        before = loop.time ()
        await asyncio.wait_for (controller.run (), settings.timeout*2)
        after = loop.time ()
        assert settings.idleTimeout <= after-before <= settings.idleTimeout*2+3
    finally:
        await runner.cleanup ()

def test_set_entry ():
    a = SetEntry (1, a=2, b=3)
    assert a == a
    assert hash (a) == hash (a)

    b = SetEntry (1, a=2, b=4)
    assert a == b
    assert hash (a) == hash (b)

    c = SetEntry (2, a=2, b=3)
    assert a != c
    assert hash (a) != hash (c)

@pytest.mark.asyncio
async def test_idle_state_tracker ():
    # default is idle
    loop = asyncio.get_event_loop ()
    idle = IdleStateTracker (loop)
    assert idle._idle

    # idle change
    await idle.push (PageIdle (False))
    assert not idle._idle

    # nothing happens for other objects
    await idle.push ({})
    assert not idle._idle

    # no state change -> wait does not return
    with pytest.raises (asyncio.TimeoutError):
        await asyncio.wait_for (idle.wait (0.1), timeout=1)

    # wait at least timeout
    delta = 0.2
    timeout = 1
    await idle.push (PageIdle (True))
    assert idle._idle
    start = loop.time ()
    await idle.wait (timeout)
    end = loop.time ()
    assert (timeout-delta) < (end-start) < (timeout+delta)

@pytest.fixture
async def recordingServer ():
    """ Simple HTTP server that records raw requests """
    url = URL ('http://localhost:8080')
    reqs = []
    async def record (request):
        reqs.append (request)
        return web.Response(text='ok', content_type='text/plain')
    app = web.Application()
    app.add_routes([web.get(url.path, record)])
    runner = web.AppRunner(app)
    await runner.setup()
    site = web.TCPSite (runner, url.host, url.port)
    await site.start()
    yield url, reqs
    await runner.cleanup ()

from .test_devtools import tab, browser
from http.cookies import Morsel, SimpleCookie

@pytest.mark.asyncio
async def test_set_cookies (tab, recordingServer):
    """ Make sure cookies are set properly and only affect the domain they were
    set for """

    logger = Logger ()

    url, reqs = recordingServer

    cookies = []
    c = Morsel ()
    c.set ('foo', 'bar', '')
    c['domain'] = 'localhost'
    cookies.append (c)
    c = Morsel ()
    c.set ('buz', 'beef', '')
    c['domain'] = 'nonexistent.example'

    settings = ControllerSettings (idleTimeout=1, timeout=60, cookies=cookies)
    controller = SinglePageController (url=url, logger=logger,
            service=Process (), behavior=[], settings=settings)
    await asyncio.wait_for (controller.run (), settings.timeout*2)
    
    assert len (reqs) == 1
    req = reqs[0]
    reqCookies = SimpleCookie (req.headers['cookie'])
    assert len (reqCookies) == 1
    c = next (iter (reqCookies.values ()))
    assert c.key == cookies[0].key
    assert c.value == cookies[0].value