def test_base_request(self): client = Client(request_demo_app, RequestTestResponse) # get requests response = client.get('/?foo=bar&foo=hehe') self.assert_strict_equal( response['args'], MultiDict([('foo', u'bar'), ('foo', u'hehe')])) self.assert_strict_equal(response['args_as_list'], [('foo', [u'bar', u'hehe'])]) self.assert_strict_equal(response['form'], MultiDict()) self.assert_strict_equal(response['form_as_list'], []) self.assert_strict_equal(response['data'], b'') self.assert_environ(response['environ'], 'GET') # post requests with form data response = client.post( '/?blub=blah', data='foo=blub+hehe&blah=42', content_type='application/x-www-form-urlencoded') self.assert_strict_equal(response['args'], MultiDict([('blub', u'blah')])) self.assert_strict_equal(response['args_as_list'], [('blub', [u'blah'])]) self.assert_strict_equal( response['form'], MultiDict([('foo', u'blub hehe'), ('blah', u'42')])) self.assert_strict_equal(response['data'], b'') # currently we do not guarantee that the values are ordered correctly # for post data. ## self.assert_strict_equal(response['form_as_list'], [('foo', ['blub hehe']), ('blah', ['42'])]) self.assert_environ(response['environ'], 'POST') # patch requests with form data response = client.patch( '/?blub=blah', data='foo=blub+hehe&blah=42', content_type='application/x-www-form-urlencoded') self.assert_strict_equal(response['args'], MultiDict([('blub', u'blah')])) self.assert_strict_equal(response['args_as_list'], [('blub', [u'blah'])]) self.assert_strict_equal( response['form'], MultiDict([('foo', u'blub hehe'), ('blah', u'42')])) self.assert_strict_equal(response['data'], b'') self.assert_environ(response['environ'], 'PATCH') # post requests with json data json = b'{"foo": "bar", "blub": "blah"}' response = client.post('/?a=b', data=json, content_type='application/json') self.assert_strict_equal(response['data'], json) self.assert_strict_equal(response['args'], MultiDict([('a', u'b')])) self.assert_strict_equal(response['form'], MultiDict())
def patch(url_patch, payload, hashing=True, ssl_on=False): environ = {} if ssl_on: environ["wsgi.url_scheme"] = "https" if hashing: url_patch = hmac_hashing(url_patch, payload, ssl_on=ssl_on) c = Client(app, BaseResponse) return c.patch(url_patch, data=json.dumps(payload), headers={'Content-Type': 'application/json'}, environ_overrides=environ)
def test_base_request(): client = Client(request_demo_app, RequestTestResponse) # get requests response = client.get("/?foo=bar&foo=hehe") strict_eq(response["args"], MultiDict([("foo", u"bar"), ("foo", u"hehe")])) strict_eq(response["args_as_list"], [("foo", [u"bar", u"hehe"])]) strict_eq(response["form"], MultiDict()) strict_eq(response["form_as_list"], []) strict_eq(response["data"], b"") assert_environ(response["environ"], "GET") # post requests with form data response = client.post( "/?blub=blah", data="foo=blub+hehe&blah=42", content_type="application/x-www-form-urlencoded", ) strict_eq(response["args"], MultiDict([("blub", u"blah")])) strict_eq(response["args_as_list"], [("blub", [u"blah"])]) strict_eq(response["form"], MultiDict([("foo", u"blub hehe"), ("blah", u"42")])) strict_eq(response["data"], b"") # currently we do not guarantee that the values are ordered correctly # for post data. # strict_eq(response['form_as_list'], [('foo', ['blub hehe']), ('blah', ['42'])]) assert_environ(response["environ"], "POST") # patch requests with form data response = client.patch( "/?blub=blah", data="foo=blub+hehe&blah=42", content_type="application/x-www-form-urlencoded", ) strict_eq(response["args"], MultiDict([("blub", u"blah")])) strict_eq(response["args_as_list"], [("blub", [u"blah"])]) strict_eq(response["form"], MultiDict([("foo", u"blub hehe"), ("blah", u"42")])) strict_eq(response["data"], b"") assert_environ(response["environ"], "PATCH") # post requests with json data json = b'{"foo": "bar", "blub": "blah"}' response = client.post("/?a=b", data=json, content_type="application/json") strict_eq(response["data"], json) strict_eq(response["args"], MultiDict([("a", u"b")])) strict_eq(response["form"], MultiDict())
def test_base_request(): client = Client(request_demo_app) # get requests response = client.get("/?foo=bar&foo=hehe") request = response.request assert request.args == MultiDict([("foo", "bar"), ("foo", "hehe")]) assert request.form == MultiDict() assert request.data == b"" assert_environ(request.environ, "GET") # post requests with form data response = client.post( "/?blub=blah", data="foo=blub+hehe&blah=42", content_type="application/x-www-form-urlencoded", ) request = response.request assert request.args == MultiDict([("blub", "blah")]) assert request.form == MultiDict([("foo", "blub hehe"), ("blah", "42")]) assert request.data == b"" # currently we do not guarantee that the values are ordered correctly # for post data. # assert response['form_as_list'] == [('foo', ['blub hehe']), ('blah', ['42'])] assert_environ(request.environ, "POST") # patch requests with form data response = client.patch( "/?blub=blah", data="foo=blub+hehe&blah=42", content_type="application/x-www-form-urlencoded", ) request = response.request assert request.args == MultiDict([("blub", "blah")]) assert request.form == MultiDict([("foo", "blub hehe"), ("blah", "42")]) assert request.data == b"" assert_environ(request.environ, "PATCH") # post requests with json data json = b'{"foo": "bar", "blub": "blah"}' response = client.post("/?a=b", data=json, content_type="application/json") request = response.request assert request.data == json assert request.args == MultiDict([("a", "b")]) assert request.form == MultiDict()
def test_base_request(): client = Client(request_demo_app, RequestTestResponse) # get requests response = client.get("/?foo=bar&foo=hehe") strict_eq(response["args"], MultiDict([("foo", u"bar"), ("foo", u"hehe")])) strict_eq(response["args_as_list"], [("foo", [u"bar", u"hehe"])]) strict_eq(response["form"], MultiDict()) strict_eq(response["form_as_list"], []) strict_eq(response["data"], b"") assert_environ(response["environ"], "GET") # post requests with form data response = client.post( "/?blub=blah", data="foo=blub+hehe&blah=42", content_type="application/x-www-form-urlencoded", ) strict_eq(response["args"], MultiDict([("blub", u"blah")])) strict_eq(response["args_as_list"], [("blub", [u"blah"])]) strict_eq(response["form"], MultiDict([("foo", u"blub hehe"), ("blah", u"42")])) strict_eq(response["data"], b"") # currently we do not guarantee that the values are ordered correctly # for post data. # strict_eq(response['form_as_list'], [('foo', ['blub hehe']), ('blah', ['42'])]) assert_environ(response["environ"], "POST") # patch requests with form data response = client.patch( "/?blub=blah", data="foo=blub+hehe&blah=42", content_type="application/x-www-form-urlencoded", ) strict_eq(response["args"], MultiDict([("blub", u"blah")])) strict_eq(response["args_as_list"], [("blub", [u"blah"])]) strict_eq(response["form"], MultiDict([("foo", u"blub hehe"), ("blah", u"42")])) strict_eq(response["data"], b"") assert_environ(response["environ"], "PATCH") # post requests with json data json = b'{"foo": "bar", "blub": "blah"}' response = client.post("/?a=b", data=json, content_type="application/json") strict_eq(response["data"], json) strict_eq(response["args"], MultiDict([("a", u"b")])) strict_eq(response["form"], MultiDict())
def test_base_request(): client = Client(request_demo_app, RequestTestResponse) # get requests response = client.get('/?foo=bar&foo=hehe') strict_eq(response['args'], MultiDict([('foo', u'bar'), ('foo', u'hehe')])) strict_eq(response['args_as_list'], [('foo', [u'bar', u'hehe'])]) strict_eq(response['form'], MultiDict()) strict_eq(response['form_as_list'], []) strict_eq(response['data'], b'') assert_environ(response['environ'], 'GET') # post requests with form data response = client.post('/?blub=blah', data='foo=blub+hehe&blah=42', content_type='application/x-www-form-urlencoded') strict_eq(response['args'], MultiDict([('blub', u'blah')])) strict_eq(response['args_as_list'], [('blub', [u'blah'])]) strict_eq(response['form'], MultiDict([('foo', u'blub hehe'), ('blah', u'42')])) strict_eq(response['data'], b'') # currently we do not guarantee that the values are ordered correctly # for post data. # strict_eq(response['form_as_list'], [('foo', ['blub hehe']), ('blah', ['42'])]) assert_environ(response['environ'], 'POST') # patch requests with form data response = client.patch('/?blub=blah', data='foo=blub+hehe&blah=42', content_type='application/x-www-form-urlencoded') strict_eq(response['args'], MultiDict([('blub', u'blah')])) strict_eq(response['args_as_list'], [('blub', [u'blah'])]) strict_eq(response['form'], MultiDict([('foo', u'blub hehe'), ('blah', u'42')])) strict_eq(response['data'], b'') assert_environ(response['environ'], 'PATCH') # post requests with json data json = b'{"foo": "bar", "blub": "blah"}' response = client.post('/?a=b', data=json, content_type='application/json') strict_eq(response['data'], json) strict_eq(response['args'], MultiDict([('a', u'b')])) strict_eq(response['form'], MultiDict())
def patch(url_patch, payload, hashing=True): if hashing: url_patch = hmac_hashing(url_patch, payload) c = Client(app, BaseResponse) return c.patch(url_patch, data=json.dumps(payload), headers={'Content-Type':'application/json'})
def client(tuber): """Creates a test client with initial setup complete and the admin user logged in already. Also patches the get/post/patch/delete functions to handle CSRF tokens for you. """ tuber.database.create_tables() if os.environ['ENABLE_CIRCUITBREAKER'].lower() == "true": client = Client(tuber.backgroundjobs.AsyncMiddleware(tuber.app)) else: client = tuber.app.test_client() #with tuber.app.test_client() as client: #client = Client(tuber.backgroundjobs.AsyncMiddleware(tuber.app)) client.post('/api/initial_setup', json={ "username": "******", "email": "*****@*****.**", "password": "******" }) client.post("/api/login", json={ "username": "******", "password": "******" }, headers={"CSRF-Token": csrf(client)}) client.post("/api/event", json={ "name": "Tuber Event", "description": "It's a potato" }, headers={"CSRF-Token": csrf(client)}) _get = client.get def get(*args, **kwargs): if not 'headers' in kwargs: kwargs['headers'] = {} kwargs['headers']['CSRF-Token'] = csrf(client) rv = _get(*args, **kwargs) return rv _post = client.post def post(*args, **kwargs): if not 'headers' in kwargs: kwargs['headers'] = {} kwargs['headers']['CSRF-Token'] = csrf(client) rv = _post(*args, **kwargs) return rv _patch = client.patch def patch(*args, **kwargs): if not 'headers' in kwargs: kwargs['headers'] = {} kwargs['headers']['CSRF-Token'] = csrf(client) rv = _patch(*args, **kwargs) return rv _delete = client.delete def delete(*args, **kwargs): if not 'headers' in kwargs: kwargs['headers'] = {} kwargs['headers']['CSRF-Token'] = csrf(client) rv = _delete(*args, **kwargs) return rv client.get = get client.post = post client.patch = patch client.delete = delete yield client tuber.database.drop_tables()