PageRenderTime 36ms CodeModel.GetById 20ms RepoModel.GetById 0ms 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. import pytest
  3. from circuits import Component
  4. from circuits.web import Controller
  5. from circuits.web.client import parse_url
  6. from circuits.net.sockets import TCPClient
  7. from circuits.net.events import connect, write
  8. class Client(Component):
  9. def __init__(self, *args, **kwargs):
  10. super(Client, self).__init__(*args, **kwargs)
  11. self._buffer = []
  12. self.done = False
  13. def read(self, data):
  14. self._buffer.append(data)
  15. if data.find(b"\r\n") != -1:
  16. self.done = True
  17. def buffer(self):
  18. return b''.join(self._buffer)
  19. class Root(Controller):
  20. def index(self):
  21. return "Hello World!"
  22. def test(webapp):
  23. transport = TCPClient()
  24. client = Client()
  25. client += transport
  26. client.start()
  27. host, port, resource, secure = parse_url(webapp.server.http.base)
  28. client.fire(connect(host, port))
  29. assert pytest.wait_for(transport, "connected")
  30. client.fire(write(b"GET / HTTP/1.1\r\n"))
  31. client.fire(write(b"Content-Type: text/plain\r\n\r\n"))
  32. assert pytest.wait_for(client, "done")
  33. client.stop()
  34. s = client.buffer().decode('utf-8').split('\r\n')[0]
  35. assert s == "HTTP/1.1 200 OK"