PageRenderTime 28ms CodeModel.GetById 13ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/tests/web/test_unicode.py

https://bitbucket.org/prologic/circuits/
Python | 105 lines | 80 code | 23 blank | 2 comment | 0 complexity | f5f40e3c08861888e640595fda090e69 MD5 | raw file
  1#!/usr/bin/env python
  2# -*- coding: utf-8 -*-
  3
  4try:
  5    from httplib import HTTPConnection
  6except ImportError:
  7    from http.client import HTTPConnection  # NOQA
  8
  9from circuits.six import b
 10from circuits.web import Controller
 11from circuits.web.client import Client, request
 12
 13from .helpers import urlopen
 14
 15
 16class Root(Controller):
 17
 18    def index(self):
 19        return "Hello World!"
 20
 21    def request_body(self):
 22        return self.request.body.read()
 23
 24    def response_body(self):
 25        return "ä"
 26
 27    def request_headers(self):
 28        return self.request.headers["A"]
 29
 30    def response_headers(self):
 31        self.response.headers["A"] = "ä"
 32        return "ä"
 33
 34
 35def test_index(webapp):
 36    f = urlopen(webapp.server.http.base)
 37    s = f.read()
 38    assert s == b("Hello World!")
 39
 40
 41def test_request_body(webapp):
 42    connection = HTTPConnection(webapp.server.host, webapp.server.port)
 43    connection.connect()
 44
 45    body = b("ä")
 46    connection.request("GET", "/request_body", body)
 47    response = connection.getresponse()
 48    assert response.status == 200
 49    assert response.reason == "OK"
 50    s = response.read()
 51    assert s == b("ä")
 52
 53    connection.close()
 54
 55
 56def test_response_body(webapp):
 57    connection = HTTPConnection(webapp.server.host, webapp.server.port)
 58    connection.connect()
 59
 60    connection.request("GET", "/response_body")
 61    response = connection.getresponse()
 62    assert response.status == 200
 63    assert response.reason == "OK"
 64    s = response.read()
 65    assert s == b("ä")
 66
 67    connection.close()
 68
 69
 70def test_request_headers(webapp):
 71    connection = HTTPConnection(webapp.server.host, webapp.server.port)
 72    connection.connect()
 73
 74    body = b("")
 75    headers = {"A": "ä"}
 76    connection.request("GET", "/request_headers", body, headers)
 77    response = connection.getresponse()
 78    assert response.status == 200
 79    assert response.reason == "OK"
 80    s = response.read()
 81    assert s == b("ä")
 82
 83    connection.close()
 84
 85
 86def test_response_headers(webapp):
 87    client = Client()
 88    client.start()
 89    client.fire(
 90        request(
 91            "GET",
 92            "http://%s:%s/response_headers" % (
 93                webapp.server.host, webapp.server.port
 94            )
 95        )
 96    )
 97
 98    while client.response is None:
 99        pass
100    assert client.response.status == 200
101    assert client.response.reason == 'OK'
102    s = client.response.read()
103    a = client.response.headers.get('A')
104    assert a == "ä"
105    assert s == b("ä")