예제 #1
0
 def build_stub_environ(self, app):
     kw = {'base_url': 'http://localhost'}
     builder = EnvironBuilder(self.app, **kw)
     try:
         environ = builder.get_environ()
     finally:
         builder.close()
     return environ
예제 #2
0
def test_bad_environ_raises_bad_request():
    app = flask.Flask(__name__)

    from flask.testing import EnvironBuilder

    builder = EnvironBuilder(app)
    environ = builder.get_environ()

    # use a non-printable character in the Host - this is key to this test
    environ["HTTP_HOST"] = "\x8a"

    with app.request_context(environ):
        response = app.full_dispatch_request()
    assert response.status_code == 400
예제 #3
0
def test_client_open_environ(app, client, request):
    @app.route("/index")
    def index():
        return flask.request.remote_addr

    builder = EnvironBuilder(app, path="/index", method="GET")
    request.addfinalizer(builder.close)

    rv = client.open(builder)
    assert rv.data == b"127.0.0.1"

    environ = builder.get_environ()
    client.environ_base["REMOTE_ADDR"] = "127.0.0.2"
    rv = client.open(environ)
    assert rv.data == b"127.0.0.2"
예제 #4
0
    def send(self, request, **kwargs):
        kw = {
            'environ_base': self.environ_base,
            'method': request.method,
            'data': request.body,
            'headers': request.headers.items()
        }
        builder = EnvironBuilder(app=self.app, path=request.path_url, **kw)

        try:
            environ = builder.get_environ()
        finally:
            builder.close()

        rv = run_wsgi_app(self.app, environ, buffered=True)
        return self.build_response(request, rv)
예제 #5
0
def test_bad_environ_raises_bad_request():
    app = flask.Flask(__name__)

    # We cannot use app.test_client() for the Unicode-rich Host header,
    # because werkzeug enforces latin1 on Python 2.
    # However it works when actually passed to the server.

    from flask.testing import EnvironBuilder

    builder = EnvironBuilder(app)
    environ = builder.get_environ()

    # use a non-printable character in the Host - this is key to this test
    environ["HTTP_HOST"] = u"\x8a"

    with app.request_context(environ):
        response = app.full_dispatch_request()
    assert response.status_code == 400
예제 #6
0
def test_environ_for_valid_idna_completes():
    app = flask.Flask(__name__)

    @app.route("/")
    def index():
        return "Hello World!"

    from flask.testing import EnvironBuilder

    builder = EnvironBuilder(app)
    environ = builder.get_environ()

    # these characters are all IDNA-compatible
    environ["HTTP_HOST"] = "ąśźäüжŠßя.com"

    with app.request_context(environ):
        response = app.full_dispatch_request()

    assert response.status_code == 200
예제 #7
0
def test_environ_for_valid_idna_completes():
    app = flask.Flask(__name__)

    @app.route("/")
    def index():
        return "Hello World!"

    # We cannot use app.test_client() for the Unicode-rich Host header,
    # because werkzeug enforces latin1 on Python 2.
    # However it works when actually passed to the server.

    from flask.testing import EnvironBuilder

    builder = EnvironBuilder(app)
    environ = builder.get_environ()

    # these characters are all IDNA-compatible
    environ["HTTP_HOST"] = u"ąśźäüжŠßя.com"

    with app.request_context(environ):
        response = app.full_dispatch_request()

    assert response.status_code == 200