PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/web/test_http.py

https://bitbucket.org/prologic/circuits/
Python | 50 lines | 43 code | 6 blank | 1 comment | 1 complexity | e76273114ad1c1b645965506fc3982e1 MD5 | raw file
 1#!/usr/bin/env python
 2
 3import pytest
 4
 5from circuits import Component
 6from circuits.web import Controller
 7from circuits.web.client import parse_url
 8from circuits.net.sockets import TCPClient
 9from circuits.net.events import connect, write
10
11
12class Client(Component):
13    def __init__(self, *args, **kwargs):
14        super(Client, self).__init__(*args, **kwargs)
15        self._buffer = []
16        self.done = False
17
18    def read(self, data):
19        self._buffer.append(data)
20        if data.find(b"\r\n") != -1:
21            self.done = True
22
23    def buffer(self):
24        return b''.join(self._buffer)
25
26
27class Root(Controller):
28
29    def index(self):
30        return "Hello World!"
31
32
33def test(webapp):
34    transport = TCPClient()
35    client = Client()
36    client += transport
37    client.start()
38
39    host, port, resource, secure = parse_url(webapp.server.http.base)
40    client.fire(connect(host, port))
41    assert pytest.wait_for(transport, "connected")
42
43    client.fire(write(b"GET / HTTP/1.1\r\n"))
44    client.fire(write(b"Content-Type: text/plain\r\n\r\n"))
45    assert pytest.wait_for(client, "done")
46
47    client.stop()
48
49    s = client.buffer().decode('utf-8').split('\r\n')[0]
50    assert s == "HTTP/1.1 200 OK"