[testing] changes from requests to httpx for Starlette TestClient (breaks)

This commit is contained in:
Maxime Alves LIRMM 2023-01-14 10:55:20 +01:00
parent f3784fab7f
commit 01333a200c
7 changed files with 47 additions and 46 deletions

View File

@ -120,7 +120,7 @@ class TestDomain(TestCase):
return result_d return result_d
def check_routes(self): def check_routes(self):
r = self.client.get('/') r = self.client.request('get', '/')
assert r.status_code == 200 assert r.status_code == 200
schemas = r.json() schemas = r.json()
assert isinstance(schemas, list) assert isinstance(schemas, list)
@ -131,7 +131,7 @@ class TestDomain(TestCase):
assert 'paths' in schema assert 'paths' in schema
assert 'domain' in schema assert 'domain' in schema
r = self.client.get('/halfapi/acls') r = self.client.request('get', '/halfapi/acls')
""" """
assert r.status_code == 200 assert r.status_code == 200
d_r = r.json() d_r = r.json()

View File

@ -17,7 +17,7 @@ def test_acl_Check(dummy_app, token_debug_false_builder):
dummy_app.add_route('/test_public', test_route_public) dummy_app.add_route('/test_public', test_route_public)
test_client = TestClient(dummy_app) test_client = TestClient(dummy_app)
resp = test_client.get('/test_public?check') resp = test_client.request('get', '/test_public?check')
assert resp.status_code == 200 assert resp.status_code == 200
@HalfRoute.acl_decorator(params=[{'acl':acl.private}]) @HalfRoute.acl_decorator(params=[{'acl':acl.private}])
@ -28,10 +28,10 @@ def test_acl_Check(dummy_app, token_debug_false_builder):
dummy_app.add_route('/test_private', test_route_private) dummy_app.add_route('/test_private', test_route_private)
test_client = TestClient(dummy_app) test_client = TestClient(dummy_app)
resp = test_client.get('/test_private') resp = test_client.request('get', '/test_private')
assert resp.status_code == 401 assert resp.status_code == 401
resp = test_client.get('/test_private?check') resp = test_client.request('get', '/test_private?check')
assert resp.status_code == 200 assert resp.status_code == 200

View File

@ -15,7 +15,7 @@ def test_halfapi_whoami(application_debug):
# So we use a single function with fixture "application debug" # So we use a single function with fixture "application debug"
c = TestClient(application_debug) c = TestClient(application_debug)
r = c.get('/halfapi/whoami') r = c.request('get', '/halfapi/whoami')
assert r.status_code == 200 assert r.status_code == 200
def test_halfapi_log(application_debug): def test_halfapi_log(application_debug):
@ -24,7 +24,7 @@ def test_halfapi_log(application_debug):
c = TestClient(application_debug) c = TestClient(application_debug)
r = c.get('/halfapi/log') r = c.request('get', '/halfapi/log')
assert r.status_code == 200 assert r.status_code == 200
def test_halfapi_error_400(application_debug): def test_halfapi_error_400(application_debug):
@ -33,7 +33,7 @@ def test_halfapi_error_400(application_debug):
c = TestClient(application_debug) c = TestClient(application_debug)
r = c.get('/halfapi/error/400') r = c.request('get', '/halfapi/error/400')
assert r.status_code == 400 assert r.status_code == 400
def test_halfapi_error_404(application_debug): def test_halfapi_error_404(application_debug):
@ -42,7 +42,7 @@ def test_halfapi_error_404(application_debug):
c = TestClient(application_debug) c = TestClient(application_debug)
r = c.get('/halfapi/error/404') r = c.request('get', '/halfapi/error/404')
assert r.status_code == 404 assert r.status_code == 404
def test_halfapi_error_500(application_debug): def test_halfapi_error_500(application_debug):
@ -51,13 +51,13 @@ def test_halfapi_error_500(application_debug):
c = TestClient(application_debug) c = TestClient(application_debug)
r = c.get('/halfapi/error/500') r = c.request('get', '/halfapi/error/500')
assert r.status_code == 500 assert r.status_code == 500
def test_schema(application_debug): def test_schema(application_debug):
c = TestClient(application_debug) c = TestClient(application_debug)
r = c.get('/') r = c.request('get', '/')
schemas = r.json() schemas = r.json()
assert isinstance(schemas, list) assert isinstance(schemas, list)
for schema in schemas: for schema in schemas:

View File

@ -17,22 +17,22 @@ class TestDummyDomain(TestDomain):
self.check_routes() self.check_routes()
def test_html_route(self): def test_html_route(self):
res = self.client.get('/ret_type') res = self.client.request('get', '/ret_type')
assert res.status_code == 200 assert res.status_code == 200
assert isinstance(res.content.decode(), str) assert isinstance(res.content.decode(), str)
assert res.headers['content-type'].split(';')[0] == 'text/html' assert res.headers['content-type'].split(';')[0] == 'text/html'
res = self.client.get('/ret_type/h24') res = self.client.request('get', '/ret_type/h24')
assert res.status_code == 200 assert res.status_code == 200
assert isinstance(res.content.decode(), str) assert isinstance(res.content.decode(), str)
assert res.headers['content-type'].split(';')[0] == 'text/html' assert res.headers['content-type'].split(';')[0] == 'text/html'
res = self.client.get('/ret_type/h24/config') res = self.client.request('get', '/ret_type/h24/config')
assert res.status_code == 200 assert res.status_code == 200
assert isinstance(res.content.decode(), str) assert isinstance(res.content.decode(), str)
assert res.headers['content-type'].split(';')[0] == 'text/html' assert res.headers['content-type'].split(';')[0] == 'text/html'
res = self.client.post('/ret_type/h24/config', { res = self.client.request('post', '/ret_type/h24/config', data={
'trou': 'glet' 'trou': 'glet'
}) })
assert res.status_code == 200 assert res.status_code == 200
@ -40,37 +40,37 @@ class TestDummyDomain(TestDomain):
assert res.headers['content-type'].split(';')[0] == 'text/html' assert res.headers['content-type'].split(';')[0] == 'text/html'
def test_arguments__get_routes(self): def test_arguments__get_routes(self):
res = self.client.post('/arguments?foo=1&x=3') res = self.client.request('post', '/arguments?foo=1&x=3')
assert res.status_code == 400 assert res.status_code == 400
arg_dict = {'foo': '1', 'bar': '2', 'x': '3'} arg_dict = {'foo': '1', 'bar': '2', 'x': '3'}
res = self.client.get('/arguments?foo=1&bar=2&x=3') res = self.client.request('get', '/arguments?foo=1&bar=2&x=3')
assert res.json() == arg_dict assert res.json() == arg_dict
res = self.client.get('/arguments?foo=1&bar=2&x=3&y=4') res = self.client.request('get', '/arguments?foo=1&bar=2&x=3&y=4')
assert res.json() == arg_dict assert res.json() == arg_dict
def test_arguments_post_routes(self): def test_arguments_post_routes(self):
arg_dict = {} arg_dict = {}
res = self.client.post('/arguments', arg_dict) res = self.client.request('post', '/arguments', data=arg_dict)
assert res.status_code == 400 assert res.status_code == 400
arg_dict = {'foo': '1', 'bar': '3'} arg_dict = {'foo': '1', 'bar': '3'}
res = self.client.post('/arguments', arg_dict) res = self.client.request('post', '/arguments', data=arg_dict)
assert res.status_code == 400 assert res.status_code == 400
arg_dict = {'foo': '1', 'baz': '3'} arg_dict = {'foo': '1', 'baz': '3'}
res = self.client.post('/arguments', arg_dict) res = self.client.request('post', '/arguments', data=arg_dict)
assert res.json() == arg_dict assert res.json() == arg_dict
arg_dict = {'foo': '1', 'baz': '3', 'truebidoo': '4'} arg_dict = {'foo': '1', 'baz': '3', 'truebidoo': '4'}
res = self.client.post('/arguments', arg_dict) res = self.client.request('post', '/arguments', data=arg_dict)
assert res.json() == arg_dict assert res.json() == arg_dict
res = self.client.post('/arguments', { **arg_dict, 'y': '4'}) res = self.client.request('post', '/arguments', data={ **arg_dict, 'y': '4'})
assert res.json() == arg_dict assert res.json() == arg_dict

View File

@ -4,16 +4,17 @@ import importlib
import subprocess import subprocess
import time import time
import pytest import pytest
import json
from pprint import pprint from pprint import pprint
from starlette.routing import Route from starlette.routing import Route
from starlette.testclient import TestClient from starlette.testclient import TestClient
def test_get_config_route(dummy_project, application_domain): def test_get_config_route(dummy_project, application_domain):
c = TestClient(application_domain) c = TestClient(application_domain)
r = c.get('/') r = c.request('get', '/')
assert r.status_code == 200 assert r.status_code == 200
pprint(r.json()) pprint(r.json())
r = c.get('/config') r = c.request('get', '/config')
assert r.status_code == 200 assert r.status_code == 200
pprint(r.json()) pprint(r.json())
assert 'test' in r.json() assert 'test' in r.json()
@ -36,15 +37,15 @@ def test_get_route(dummy_project, application_domain):
print(route_path) print(route_path)
try: try:
if verb.lower() == 'get': if verb.lower() == 'get':
r = c.get(route_path) r = c.request('get', route_path)
elif verb.lower() == 'post': elif verb.lower() == 'post':
r = c.post(route_path) r = c.request('post', route_path)
elif verb.lower() == 'patch': elif verb.lower() == 'patch':
r = c.patch(route_path) r = c.request('patch', route_path)
elif verb.lower() == 'put': elif verb.lower() == 'put':
r = c.put(route_path) r = c.request('put', route_path)
elif verb.lower() == 'delete': elif verb.lower() == 'delete':
r = c.delete(route_path) r = c.request('delete', route_path)
else: else:
raise Exception(verb) raise Exception(verb)
try: try:
@ -69,7 +70,7 @@ def test_get_route(dummy_project, application_domain):
path = path.format(test=str(test_uuid)) path = path.format(test=str(test_uuid))
route_path = f'/{path}' route_path = f'/{path}'
if verb.lower() == 'get': if verb.lower() == 'get':
r = c.get(f'{route_path}') r = c.request('get', f'{route_path}')
assert r.status_code == 200 assert r.status_code == 200
@ -78,7 +79,7 @@ def test_delete_route(dummy_project, application_domain):
c = TestClient(application_domain) c = TestClient(application_domain)
from uuid import uuid4 from uuid import uuid4
arg = str(uuid4()) arg = str(uuid4())
r = c.delete(f'/abc/alphabet/{arg}') r = c.request('delete', f'/abc/alphabet/{arg}')
assert r.status_code == 200 assert r.status_code == 200
assert isinstance(r.json(), str) assert isinstance(r.json(), str)
@ -86,22 +87,22 @@ def test_arguments_route(dummy_project, application_domain):
c = TestClient(application_domain) c = TestClient(application_domain)
path = '/arguments' path = '/arguments'
r = c.get(path) r = c.request('get', path)
assert r.status_code == 400 assert r.status_code == 400
r = c.get(path, params={'foo':True}) r = c.request('get', path, params={'foo':True})
assert r.status_code == 400 assert r.status_code == 400
arg = {'foo':True, 'bar':True} arg = {'foo':True, 'bar':True}
r = c.get(path, params=arg) r = c.request('get', path, params=arg)
assert r.status_code == 200 assert r.status_code == 200
for key, val in arg.items(): for key, val in arg.items():
assert r.json()[key] == str(val) assert r.json()[key] == str(val)
path = '/async_router/arguments' path = '/async_router/arguments'
r = c.get(path) r = c.request('get', path)
assert r.status_code == 400 assert r.status_code == 400
r = c.get(path, params={'foo':True}) r = c.request('get', path, params={'foo':True})
assert r.status_code == 400 assert r.status_code == 400
arg = {'foo':True, 'bar':True} arg = {'foo':True, 'bar':True}
r = c.get(path, params=arg) r = c.request('get', path, params=arg)
assert r.status_code == 200 assert r.status_code == 200
for key, val in arg.items(): for key, val in arg.items():
assert r.json()[key] == str(val) assert r.json()[key] == str(val)

View File

@ -38,7 +38,7 @@ def test_jwt_NoToken(dummy_app):
dummy_app.add_route('/test', test_route) dummy_app.add_route('/test', test_route)
test_client = TestClient(dummy_app) test_client = TestClient(dummy_app)
resp = test_client.get('/test') resp = test_client.request('get', '/test')
assert resp.status_code == 200 assert resp.status_code == 200
def test_jwt_Token(dummy_app, token_builder): def test_jwt_Token(dummy_app, token_builder):
@ -50,7 +50,7 @@ def test_jwt_Token(dummy_app, token_builder):
dummy_app.add_route('/test', test_route) dummy_app.add_route('/test', test_route)
test_client = TestClient(dummy_app) test_client = TestClient(dummy_app)
resp = test_client.get('/test', resp = test_client.request('get', '/test',
headers={ headers={
'Authorization': token_builder 'Authorization': token_builder
}) })
@ -65,7 +65,7 @@ def test_jwt_DebugFalse(dummy_app, token_debug_false_builder):
dummy_app.add_route('/test', test_route) dummy_app.add_route('/test', test_route)
test_client = TestClient(dummy_app) test_client = TestClient(dummy_app)
resp = test_client.get('/test', resp = test_client.request('get', '/test',
headers={ headers={
'Authorization': token_debug_false_builder 'Authorization': token_debug_false_builder
}) })
@ -82,7 +82,7 @@ def test_jwt_DebugTrue(dummy_app, token_debug_true_builder):
dummy_app.add_route('/test', test_route) dummy_app.add_route('/test', test_route)
test_client = TestClient(dummy_app) test_client = TestClient(dummy_app)
resp = test_client.get('/test', resp = test_client.request('get', '/test',
headers={ headers={
'Authorization': token_debug_true_builder 'Authorization': token_debug_true_builder
}) })
@ -100,7 +100,7 @@ def test_jwt_DebugTrue_DebugApp(dummy_debug_app, token_debug_true_builder):
dummy_debug_app.add_route('/test', test_route) dummy_debug_app.add_route('/test', test_route)
test_client = TestClient(dummy_debug_app) test_client = TestClient(dummy_debug_app)
resp = test_client.get('/test', resp = test_client.request('get', '/test',
headers={ headers={
'Authorization': token_debug_true_builder 'Authorization': token_debug_true_builder
}) })

View File

@ -12,12 +12,12 @@ def test_init():
def test_call(application_debug): def test_call(application_debug):
c = TestClient(application_debug) c = TestClient(application_debug)
r = c.get('/abc/alphabet') r = c.request('get', '/abc/alphabet')
assert r.status_code == 200 assert r.status_code == 200
assert r.headers['x-domain'] == 'dummy_domain' assert r.headers['x-domain'] == 'dummy_domain'
assert r.headers['x-acl'] == 'public' assert r.headers['x-acl'] == 'public'
r = c.get('/arguments') r = c.request('get', '/arguments')
assert r.status_code == 400 assert r.status_code == 400
assert r.headers['x-domain'] == 'dummy_domain' assert r.headers['x-domain'] == 'dummy_domain'
assert r.headers['x-acl'] == 'public' assert r.headers['x-acl'] == 'public'
@ -26,7 +26,7 @@ def test_call(application_debug):
assert r.headers['x-args-optional'] == 'x' assert r.headers['x-args-optional'] == 'x'
c = TestClient(application_debug) c = TestClient(application_debug)
r = c.post('/arguments') r = c.request('post', '/arguments')
assert r.status_code == 400 assert r.status_code == 400
assert r.headers['x-domain'] == 'dummy_domain' assert r.headers['x-domain'] == 'dummy_domain'
assert r.headers['x-acl'] == 'public' assert r.headers['x-acl'] == 'public'