def open(self, *args, **kwargs): as_tuple = kwargs.pop("as_tuple", False) buffered = kwargs.pop("buffered", False) follow_redirects = kwargs.pop("follow_redirects", False) if (not kwargs and len(args) == 1 and isinstance(args[0], (werkzeug.test.EnvironBuilder, dict))): environ = self.environ_base.copy() if isinstance(args[0], werkzeug.test.EnvironBuilder): environ.update(args[0].get_environ()) else: environ.update(args[0]) environ["flask._preserve_context"] = self.preserve_context else: kwargs.setdefault( "environ_overrides", {})["flask._preserve_context"] = self.preserve_context kwargs.setdefault("environ_base", self.environ_base) builder = EnvironBuilder(self.application, *args, **kwargs) try: environ = builder.get_environ() finally: builder.close() return Client.open( self, environ, as_tuple=as_tuple, buffered=buffered, follow_redirects=follow_redirects, )
def test_post_wrong_content_type(simple_app): app_client = simple_app.app.test_client() resp = app_client.post('/v1.0/post_wrong_content_type', content_type="application/xml", data=json.dumps({"some": "data"}) ) assert resp.status_code == 415 resp = app_client.post('/v1.0/post_wrong_content_type', data=json.dumps({"some": "data"}) ) assert resp.status_code == 415 # this test checks exactly what the test directly above is supposed to check, # i.e. no content-type is provided in the header # unfortunately there is an issue with the werkzeug test environment # (https://github.com/pallets/werkzeug/issues/1159) # so that content-type is added to every request, we remove it here manually for our test # this test can be removed once the werkzeug issue is addressed builder = EnvironBuilder(path='/v1.0/post_wrong_content_type', method='POST', data=json.dumps({"some": "data"})) try: environ = builder.get_environ() finally: builder.close() environ.pop('CONTENT_TYPE') # we cannot just call app_client.open() since app_client is a flask.testing.FlaskClient # which overrides werkzeug.test.Client.open() but does not allow passing an environment # directly resp = Client.open(app_client, environ) assert resp.status_code == 415
def from_values(cls, *args, **kwargs): """Create a new request object based on the values provided. If environ is given missing values are filled from there. This method is useful for small scripts when you need to simulate a request from an URL. Do not use this method for unittesting, there is a full featured client object (:class:`Client`) that allows to create multipart requests, support for cookies etc. This accepts the same options as the :class:`~werkzeug.test.EnvironBuilder`. .. versionchanged:: 0.5 This method now accepts the same arguments as :class:`~werkzeug.test.EnvironBuilder`. Because of this the `environ` parameter is now called `environ_overrides`. :return: request object """ from werkzeug.test import EnvironBuilder charset = kwargs.pop('charset', cls.charset) kwargs['charset'] = charset builder = EnvironBuilder(*args, **kwargs) try: return builder.get_request(cls) finally: builder.close()
def from_values(cls, *args, **kwargs): from werkzeug.test import EnvironBuilder charset = kwargs.pop('charset', cls.charset) builder = EnvironBuilder(*args, **kwargs) try: return builder.get_request(cls) finally: builder.close()
def test_file_closing(self): closed = [] class SpecialInput(object): def read(self): return '' def close(self): closed.append(self) env = create_environ(data={'foo': SpecialInput()}) self.assert_strict_equal(len(closed), 1) builder = EnvironBuilder() builder.files.add_file('blah', SpecialInput()) builder.close() self.assert_strict_equal(len(closed), 2)
def test_file_closing(): closed = [] class SpecialInput(object): def read(self, size): return '' def close(self): closed.append(self) env = create_environ(data={'foo': SpecialInput()}) strict_eq(len(closed), 1) builder = EnvironBuilder() builder.files.add_file('blah', SpecialInput()) builder.close() strict_eq(len(closed), 2)
def test_file_closing(self): closed = [] class SpecialInput(object): def read(self): return '' def close(self): closed.append(self) env = create_environ(data={'foo': SpecialInput()}) assert len(closed) == 1 builder = EnvironBuilder() builder.files.add_file('blah', SpecialInput()) builder.close() assert len(closed) == 2
def open(self, *args, **kwargs): from werkzeug.test import EnvironBuilder pre_call = kwargs.pop('pre_call', None) post_call = kwargs.pop('post_call', None) middlewares = kwargs.pop('middlewares', None) builder = EnvironBuilder(*args, **kwargs) try: environ = builder.get_environ() finally: builder.close() return self._open(environ, pre_call=pre_call, post_call=post_call, middlewares=middlewares)
def test_file_closing(): """Test automatic closing of files in EnvironBuilder and friends""" closed = [] class SpecialInput(object): def read(self): return '' def close(self): closed.append(self) env = create_environ(data={'foo': SpecialInput()}) assert len(closed) == 1 builder = EnvironBuilder() builder.files.add_file('blah', SpecialInput()) builder.close() assert len(closed) == 2
def test_file_closing(): closed = [] class SpecialInput(object): def read(self, size): return "" def close(self): closed.append(self) create_environ(data={"foo": SpecialInput()}) strict_eq(len(closed), 1) builder = EnvironBuilder() builder.files.add_file("blah", SpecialInput()) builder.close() strict_eq(len(closed), 2)
def get_test_context(self, *args, **kwargs): """Creates a test client for this application. :param args: Positional arguments to construct a `werkzeug.test.EnvironBuilder`. :param kwargs: Keyword arguments to construct a `werkzeug.test.EnvironBuilder`. :returns: A :class:``RequestContext`` instance. """ from werkzeug.test import EnvironBuilder builder = EnvironBuilder(*args, **kwargs) try: return self.request_context_class(self, builder.get_environ()) finally: builder.close()
def test_file_closing(): closed = [] class SpecialInput: def read(self, size): return "" def close(self): closed.append(self) create_environ(data={"foo": SpecialInput()}) assert len(closed) == 1 builder = EnvironBuilder() builder.files.add_file("blah", SpecialInput()) builder.close() assert len(closed) == 2
def open(self, *args, **kwargs): from werkzeug.test import EnvironBuilder pre_call = kwargs.pop("pre_call", None) post_call = kwargs.pop("post_call", None) middlewares = kwargs.pop("middlewares", []) if middlewares is not None: m = self.sort_middlewares(middlewares) else: m = self.middlewares builder = EnvironBuilder(*args, **kwargs) try: environ = builder.get_environ() finally: builder.close() return self._open(environ, pre_call=pre_call, post_call=post_call, middlewares=m)
def test_environ_builder_basics(self): b = EnvironBuilder() self.assert_is_none(b.content_type) b.method = 'POST' self.assert_equal(b.content_type, 'application/x-www-form-urlencoded') b.files.add_file('test', BytesIO(b'test contents'), 'test.txt') self.assert_equal(b.files['test'].content_type, 'text/plain') self.assert_equal(b.content_type, 'multipart/form-data') b.form['test'] = 'normal value' req = b.get_request() b.close() self.assert_strict_equal(req.url, u'http://localhost/') self.assert_strict_equal(req.method, 'POST') self.assert_strict_equal(req.form['test'], u'normal value') self.assert_equal(req.files['test'].content_type, 'text/plain') self.assert_strict_equal(req.files['test'].filename, u'test.txt') self.assert_strict_equal(req.files['test'].read(), b'test contents')
def test_environ_builder_basics(): b = EnvironBuilder() assert b.content_type is None b.method = "POST" assert b.content_type == "application/x-www-form-urlencoded" b.files.add_file("test", BytesIO(b"test contents"), "test.txt") assert b.files["test"].content_type == "text/plain" assert b.content_type == "multipart/form-data" b.form["test"] = "normal value" req = b.get_request() b.close() strict_eq(req.url, u"http://localhost/") strict_eq(req.method, "POST") strict_eq(req.form["test"], u"normal value") assert req.files["test"].content_type == "text/plain" strict_eq(req.files["test"].filename, u"test.txt") strict_eq(req.files["test"].read(), b"test contents")
def test_environ_builder_basics(): b = EnvironBuilder() assert b.content_type is None b.method = 'POST' assert b.content_type == 'application/x-www-form-urlencoded' b.files.add_file('test', BytesIO(b'test contents'), 'test.txt') assert b.files['test'].content_type == 'text/plain' assert b.content_type == 'multipart/form-data' b.form['test'] = 'normal value' req = b.get_request() b.close() strict_eq(req.url, u'http://localhost/') strict_eq(req.method, 'POST') strict_eq(req.form['test'], u'normal value') assert req.files['test'].content_type == 'text/plain' strict_eq(req.files['test'].filename, u'test.txt') strict_eq(req.files['test'].read(), b'test contents')
def test_environ_builder_basics(self): b = EnvironBuilder() assert b.content_type is None b.method = 'POST' assert b.content_type == 'application/x-www-form-urlencoded' b.files.add_file('test', StringIO('test contents'), 'test.txt') assert b.files['test'].content_type == 'text/plain' assert b.content_type == 'multipart/form-data' b.form['test'] = 'normal value' req = b.get_request() b.close() assert req.url == 'http://localhost/' assert req.method == 'POST' assert req.form['test'] == 'normal value' assert req.files['test'].content_type == 'text/plain' assert req.files['test'].filename == 'test.txt' assert req.files['test'].read() == 'test contents'
def test_environ_builder_basics(): b = EnvironBuilder() assert b.content_type is None b.method = 'POST' assert b.content_type is None b.form['test'] = 'normal value' assert b.content_type == 'application/x-www-form-urlencoded' b.files.add_file('test', BytesIO(b'test contents'), 'test.txt') assert b.files['test'].content_type == 'text/plain' assert b.content_type == 'multipart/form-data' req = b.get_request() b.close() strict_eq(req.url, u'http://localhost/') strict_eq(req.method, 'POST') strict_eq(req.form['test'], u'normal value') assert req.files['test'].content_type == 'text/plain' strict_eq(req.files['test'].filename, u'test.txt') strict_eq(req.files['test'].read(), b'test contents')
def test_environ_builder_basics(): b = EnvironBuilder() assert b.content_type is None b.method = "POST" assert b.content_type is None b.form["test"] = "normal value" assert b.content_type == "application/x-www-form-urlencoded" b.files.add_file("test", BytesIO(b"test contents"), "test.txt") assert b.files["test"].content_type == "text/plain" b.form["test_int"] = 1 assert b.content_type == "multipart/form-data" req = b.get_request() b.close() assert req.url == "http://localhost/" assert req.method == "POST" assert req.form["test"] == "normal value" assert req.files["test"].content_type == "text/plain" assert req.files["test"].filename == "test.txt" assert req.files["test"].read() == b"test contents"