Example #1
0
 def test_text_response(self):
     """the text_response_server sends the given text"""
     server = Server.text_response_server("HTTP/1.1 200 OK\r\n" +
                                          "Content-Length: 6\r\n" +
                                          "\r\nroflol")
     with server as (host, port):
         r = requests.get('http://{0}:{1}'.format(host, port))
         assert r.status_code == 200
         assert r.text == u'roflol'
         assert r.headers['Content-Length'] == '6'
Example #2
0
    def test_text_response(self):
        """the text_response_server sends the given text"""
        server = Server.text_response_server("HTTP/1.1 200 OK\r\n" + "Content-Length: 6\r\n" + "\r\nroflol")

        with server as (host, port):
            r = requests.get("http://{0}:{1}".format(host, port))

            assert r.status_code == 200
            assert r.text == u"roflol"
            assert r.headers["Content-Length"] == "6"
Example #3
0
    def test_text_response(self):
        """the text_response_server sends the given text"""
        server = Server.text_response_server("HTTP/1.1 200 OK\r\n"
                                             "Content-Length: 6\r\n"
                                             "\r\nroflol")

        with server as (host, port):
            r = requests.get(f"http://{host}:{port}")

            assert r.status_code == 200
            assert r.text == "roflol"
            assert r.headers["Content-Length"] == "6"
Example #4
0
    def test_text_bom_response(self):
        """the text_response_server sends the given text with UTF-8 BOM"""
        server = Server.text_response_server(
            "HTTP/1.1 200 OK\r\n" +
            "Content-Type: text/html; charset=UTF-8\r\n" +
            u'\r\n\ufeff<doctype html><html><body>ジェーピーニック</body></html>')

        with server as (host, port):
            r = requests.get('http://{}:{}'.format(host, port))

            assert r.status_code == 200
            assert r.text == u'<doctype html><html><body>ジェーピーニック</body></html>'
            assert r.headers['Content-Type'] == 'text/html; charset=UTF-8'
Example #5
0
def test_incorrect_content_length():
    """Test ConnectionError raised for incomplete responses"""
    close_server = threading.Event()
    server = Server.text_response_server("HTTP/1.1 200 OK\r\n" +
                                         "Content-Length: 50\r\n\r\n" +
                                         "Hello World.")
    with server as (host, port):
        url = "http://{0}:{1}/".format(host, port)
        r = requests.Request("GET", url).prepare()
        s = requests.Session()
        with pytest.raises(requests.exceptions.ConnectionError) as e:
            resp = s.send(r)
        assert "12 bytes read, 38 more expected" in str(e)
        close_server.set()  # release server block
Example #6
0
    def test_json_bom_response(self):
        """the text_response_server sends the given JSON with UTF-8 BOM"""
        server = Server.text_response_server(
            "HTTP/1.1 200 OK\r\n" +
            "Content-Type: application/json; charset=utf-8\r\n" +
            u'\r\n\ufeff{"success": true}')

        with server as (host, port):
            r = requests.get('http://{}:{}'.format(host, port))

            assert r.status_code == 200
            assert r.json() == {'success': True}
            assert r.headers[
                'Content-Type'] == 'application/json; charset=utf-8'
Example #7
0
def server():
    return Server.text_response_server("HTTP/1.1 200 OK\r\n" +
                                       "Content-Length: 6\r\n" + "[\"OK\"]")