You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, when trying to test non-WSGI app from external domain (localhosts works well), it does not work well with cookies, which are not resent to the server with subsequent requests.
The problem can be resolved if extra_environ["HTTP_HOST"] is set properly to name of target host.
$ pip install httpbin
$ export FLASK_APP=httpbin
$ flask run
* Serving Flask app "httpbin"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Using pytest I have created simple test suite:
importpytestfromwebtestimportTestAppimporthttpbinapps= [("wsgiapp", httpbin.app),
("http", "http://httpbin.org"),
("https", "https://httpbin.org"),
("http#requests", "http://httpbin.org#requests"),
("https#requests", "https://httpbin.org#requests"),
("http#urllib3", "http://httpbin.org#urllib3"),
("http#restkit", "http://httpbin.org#restkit"),
("localhost_http", "http://localhost:5000"),
("localhost_http#requests", "http://localhost:5000#requests"),
("localhost_http#urllib3", "http://localhost:5000#urllib3"),
("localhost_http#restkit", "http://localhost:5000#restkit"),
]
@pytest.fixture(params=apps, ids=lambdaarg: arg[0])deftestapp(request):
testid, app=request.paramtestapp=TestApp(app)
returntestappdefamend_testapp(testapp):
"""Amend TestApp instance to deal with cookies properly. `testapp` is instance of webtest.TestApp. """ifhasattr(testapp.app, "uri"):
fromurlparseimporturlparseuri=testapp.app.uritestapp.extra_environ["HTTP_HOST"] =urlparse(uri).hostnamereturntestapp@pytest.mark.parametrize("amend", ["original", "amended"])deftest_bin(testapp, amend):
ifamend=="amended":
testapp=amend_testapp(testapp)
assertnottestapp.cookies, "Cookies in the testapp shall be empty initially"# list existing cookiesres=testapp.get("/cookies")
assertnotres.json["cookies"], "no cookies reported in response"# set some cookiecname, cval="james", "cook"res=testapp.get("/cookies/set", {cname: cval})
# set-cookie header shall be present in the responseprintres.headers.keys()
printres.headers["set-cookie"]
print"Set-Cookie"inres.headers, "Header Set-Cookie expected in response headers"# make sure, it is set in test testapp cookiejarasserttestapp.cookiesassertcnameintestapp.cookiesasserttestapp.cookies[cname] ==cval# make sure, testapp reports the cookies as being setres=testapp.get("/cookies")
cookies=res.json["cookies"]
assertcnameincookiesassertcookies[cname] ==cval
testapp = <webtest.app.TestApp object at 0x7f046ab84350>, amend = 'original'
@pytest.mark.parametrize("amend", ["original", "amended"])
def test_bin(testapp, amend):
if amend == "amended":
testapp = amend_testapp(testapp)
assert not testapp.cookies, "Cookies in the testapp shall be empty initially"
# list existing cookies
res = testapp.get("/cookies")
assert not res.json["cookies"], "no cookies reported in response"
# set some cookie
cname, cval = "james", "cook"
res = testapp.get("/cookies/set", {cname: cval})
# set-cookie header shall be present in the response
print res.headers.keys()
print res.headers["set-cookie"]
print "Set-Cookie" in res.headers, "Header Set-Cookie expected in response headers"
# make sure, it is set in test testapp cookiejar
assert testapp.cookies
assert cname in testapp.cookies
assert testapp.cookies[cname] == cval
# make sure, testapp reports the cookies as being set
res = testapp.get("/cookies")
cookies = res.json["cookies"]
> assert cname in cookies
E assert 'james' in {}
Workaround and how it works
Whole workaround is provided in following function amend_testapp:
defamend_testapp(testapp):
"""Amend TestApp instance to deal with cookies properly. `testapp` is instance of webtest.TestApp. """ifhasattr(testapp.app, "uri"):
fromurlparseimporturlparseuri=testapp.app.uritestapp.extra_environ["HTTP_HOST"] =urlparse(uri).hostnamereturntestapp
It gets an instance of TestApp and tries to detect, if it was created using url and not wsgi application. If the attribute "uri" of testapp.app" is present, it is likely to be based on url and not WSGI app. From theurivalue is detected hostname and set astestapp.extra_environ["HTTP_HOST"]`.
This solution shall work well in following circumstances:
application was created either as WSGI one or by providing url (which is allowed to contain the #{clientlibname} part like #requests at the end.
using environmental variable WEBTEST_TARGET_URL shall work well as we fix the testapp after it is created.
if (during creation of testapp was any extra_environ set, it is preserved (with only exception being HTTP_HOST).
How to fix it in WebTest library
Being able to test WSGI app as well as external ones is great feature and I think, WebTest shall keep this functional.
The question is, where shall be this fix put and options are:
WebTest library itself.
WsgiProxy2 library, which is used to handle applications accessible via url (it provides WSGI application by proxying requests to real url).
some CookieJar implementation.
I think, that it cannot be fixed on WsgiProxy2 and CookieJar level as it is too late to fix anything.
The only place to fix is probably the WebTest library itself.
The text was updated successfully, but these errors were encountered:
WebTest claims to support tests of WSGI as well as non WSGI applications, see Testing a non wsgi application.
However, when trying to test non-WSGI app from external domain (localhosts works well), it does not work well with cookies, which are not resent to the server with subsequent requests.
The problem can be resolved if
extra_environ["HTTP_HOST"]
is set properly to name of target host.The tests were run on httpbin running on http://httpbin.org`, https://httpbin.org and on local instance on http://localhost:5000.
Local instance of
httpbin
was run by:Using
pytest
I have created simple test suite:To run it, install required dependencies:
and run the test:
Typical failure looks like:
Workaround and how it works
Whole workaround is provided in following function
amend_testapp
:It gets an instance of TestApp and tries to detect, if it was created using url and not wsgi application. If the attribute "uri" of
testapp.app" is present, it is likely to be based on url and not WSGI app. From the
urivalue is detected hostname and set as
testapp.extra_environ["HTTP_HOST"]`.This solution shall work well in following circumstances:
#{clientlibname}
part like#requests
at the end.WEBTEST_TARGET_URL
shall work well as we fix the testapp after it is created.testapp
was anyextra_environ
set, it is preserved (with only exception beingHTTP_HOST
).How to fix it in WebTest library
Being able to test WSGI app as well as external ones is great feature and I think,
WebTest
shall keep this functional.The question is, where shall be this fix put and options are:
WebTest
library itself.WsgiProxy2
library, which is used to handle applications accessible via url (it provides WSGI application by proxying requests to real url).I think, that it cannot be fixed on
WsgiProxy2
and CookieJar level as it is too late to fix anything.The only place to fix is probably the
WebTest
library itself.The text was updated successfully, but these errors were encountered: