Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_raises_no_charset():
with pytest.raises(TypeError):
Response(content_type="image/jpeg", body=text_(b"test"))
def test_response_write_non_str():
res = Response()
with pytest.raises(TypeError):
res.write(object())
def test_body_get_body_is_None_len_app_iter_is_zero():
res = Response()
res._app_iter = io.BytesIO()
res._body = None
result = res.body
assert result == b""
def test_decode_content_with_deflate():
res = Response()
body = b"Hey Hey Hey"
# Simulate inflate by chopping the headers off
# the gzip encoded data
res.body = zlib.compress(body)[2:-4]
res.content_encoding = "deflate"
res.decode_content()
assert res.body == body
assert res.content_encoding is None
def test_content_type_params_set_ok_param_quoting():
res = Response()
res.content_type_params = {"a": ""}
assert res.headers["Content-Type"] == 'text/html; a=""'
assert res.content_encoding == "gzip"
assert (
res.body
== b"\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xffKTH\xcaO\xa9\x04\x00\xf6\x86GI\x06\x00\x00\x00"
)
res.decode_content()
assert res.content_encoding is None
assert res.body == b"a body"
res.set_cookie("x", text_(b"foo")) # test unicode value
with pytest.raises(TypeError):
Response(app_iter=iter(["a"]), body="somebody")
del req.environ
with pytest.raises(TypeError):
Response(charset=None, content_type="image/jpeg", body=text_(b"unicode body"))
with pytest.raises(TypeError):
Response(wrong_key="dummy")
with pytest.raises(TypeError):
resp = Response()
resp.body = text_(b"unicode body")
def test_response_file_body_tell():
import zipfile
from webob.response import ResponseBodyFile
rbo = ResponseBodyFile(Response())
assert rbo.tell() == 0
writer = zipfile.ZipFile(rbo, "w")
writer.writestr("zinfo_or_arcname", b"foo")
writer.close()
assert rbo.tell() == 133
empty = Response()
assert not empty.has_body
with_list = Response(app_iter=["1"])
assert with_list.has_body
with_empty_list = Response(app_iter=[b""])
assert not with_empty_list.has_body
with_body = Response(body="Seomthing")
assert with_body.has_body
with_none_app_iter = Response(app_iter=None)
assert not with_none_app_iter.has_body
with_none_body = Response(body=None)
assert not with_none_body.has_body
# key feature: has_body should not read app_iter
app_iter = iter(["1", "2"])
not_iterating = Response(app_iter=app_iter)
assert not_iterating.has_body
assert next(app_iter) == "1"
# messed with private attribute but method should nonetheless not
# return True
messing_with_privates = Response()
messing_with_privates._app_iter = None
assert not messing_with_privates.has_body
def test_merge_cookies_resp_is_Response():
inner_res = Response()
res = Response()
res.set_cookie("a", "1")
result = res.merge_cookies(inner_res)
assert result.headers.getall("Set-Cookie") == ["a=1; Path=/"]
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Base class(es) for WSGI Middleware."""
from inspect import getfullargspec
from oslo_config import cfg
import webob.dec
import webob.request
import webob.response
class NoContentTypeResponse(webob.response.Response):
default_content_type = None # prevents webob assigning content type
class NoContentTypeRequest(webob.request.Request):
ResponseClass = NoContentTypeResponse
class ConfigurableMiddleware(object):
"""Base WSGI middleware wrapper.
These classes require an application to be initialized that will be called
next. By default the middleware will simply call its wrapped app, or you
can override __call__ to customize its behavior.
"""