Skip to content

Commit

Permalink
use the connexion test_client in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ainquel committed Dec 18, 2018
1 parent 9eaadaf commit 10cc011
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 103 deletions.
12 changes: 11 additions & 1 deletion connexion/lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def __init__(self,
raise ValueError("{} is not a valid status code".format(status_code))
self.status_code = status_code
self.mimetype = mimetype
self.content_type = content_type
self.body = body
self.headers = headers or {}
self.content_type = content_type or self.headers.get("Content-Type")

@property
def text(self):
Expand All @@ -57,10 +57,20 @@ def json(self):
This method is naive, it will try to load JSON even
if the content_type is not JSON.
It will raise in case of a non JSON string
"""
return json.loads(self.text)

@property
def data(self):
"""return the encoded body."""
return encode(self.body)

@property
def content_length(self):
"""return the content length.
If Content-Length is not present in headers,
get the size of encoded body.
"""
return int(self.headers.get("Content-Length", len(self.data)))
56 changes: 56 additions & 0 deletions connexion/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import abc
import functools

import six


class AbstractClientMeta(abc.ABCMeta):
pass


@six.add_metaclass(AbstractClientMeta)
class AbstractClient(object):
"""A test client to run tests on top of different web frameworks."""
http_verbs = [
"head",
"get",
"delete",
"options",
"patch",
"post",
"put",
"trace",
]

def __init__(self, app):
self.app = app

@classmethod
def from_app(cls, app):
return cls(app)

@abc.abstractmethod
def _request(
self,
method,
url,
headers=None,
data=None,
json=None,
content_type=None,
):
# type: (...) -> connexion.lifecycle.ConnexionResponse
"""Make request and return a ConnexionResponse instance.
For now, the expected client has to be compatible with flask's test_client.
TODO: use the arguments from ConnexionRequest if one day Apis have a method
to translate ConnexionRequest to web framework request.
see https://github.com/pallets/werkzeug/blob/master/werkzeug/test.py#L222
for the request client.
"""

def __getattr__(self, key):
"""Call _request with method bound if key is an HTTP method."""
if key in self.http_verbs:
return functools.partial(self._request, key)
raise AttributeError(key)
30 changes: 15 additions & 15 deletions tests/api/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_app_with_relative_path(simple_api_spec_dir, spec):
debug=True)
app.add_api(spec)

app_client = app.app.test_client()
app_client = app.test_client()
get_bye = app_client.get('/v1.0/bye/jsantos') # type: flask.Response
assert get_bye.status_code == 200
assert get_bye.data == b'Goodbye jsantos'
Expand All @@ -47,7 +47,7 @@ def test_app_with_different_server_option(simple_api_spec_dir, spec):
debug=True)
app.add_api(spec)

app_client = app.app.test_client()
app_client = app.test_client()
get_bye = app_client.get('/v1.0/bye/jsantos') # type: flask.Response
assert get_bye.status_code == 200
assert get_bye.data == b'Goodbye jsantos'
Expand All @@ -61,12 +61,12 @@ def test_app_with_different_uri_parser(simple_api_spec_dir):
debug=True)
app.add_api('swagger.yaml')

app_client = app.app.test_client()
app_client = app.test_client()
resp = app_client.get(
'/v1.0/test_array_csv_query_param?items=a,b,c&items=d,e,f'
) # type: flask.Response
assert resp.status_code == 200
j = json.loads(resp.get_data(as_text=True))
j = resp.json
assert j == ['a', 'b', 'c']


Expand All @@ -77,13 +77,13 @@ def test_no_swagger_ui(simple_api_spec_dir, spec):
options=options, debug=True)
app.add_api(spec)

app_client = app.app.test_client()
app_client = app.test_client()
swagger_ui = app_client.get('/v1.0/ui/') # type: flask.Response
assert swagger_ui.status_code == 404

app2 = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
app2.add_api(spec, options={"swagger_ui": False})
app2_client = app2.app.test_client()
app2_client = app2.test_client()
swagger_ui2 = app2_client.get('/v1.0/ui/') # type: flask.Response
assert swagger_ui2.status_code == 404

Expand All @@ -93,7 +93,7 @@ def test_swagger_json_app(simple_api_spec_dir, spec):
""" Verify the spec json file is returned for default setting passed to app. """
app = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
app.add_api(spec)
app_client = app.app.test_client()
app_client = app.test_client()
url = '/v1.0/{spec}'
url = url.format(spec=spec.replace("yaml", "json"))
spec_json = app_client.get(url) # type: flask.Response
Expand All @@ -108,7 +108,7 @@ def test_no_swagger_json_app(simple_api_spec_dir, spec):
options=options, debug=True)
app.add_api(spec)

app_client = app.app.test_client()
app_client = app.test_client()
url = '/v1.0/{spec}'
url = url.format(spec=spec.replace("yaml", "json"))
spec_json = app_client.get(url) # type: flask.Response
Expand All @@ -132,7 +132,7 @@ def test_dict_as_yaml_path(simple_api_spec_dir, spec):
app = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
app.add_api(specification)

app_client = app.app.test_client()
app_client = app.test_client()
url = '/v1.0/{spec}'.format(spec=spec.replace("yaml", "json"))
swagger_json = app_client.get(url) # type: flask.Response
assert swagger_json.status_code == 200
Expand All @@ -144,7 +144,7 @@ def test_swagger_json_api(simple_api_spec_dir, spec):
app = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
app.add_api(spec)

app_client = app.app.test_client()
app_client = app.test_client()
url = '/v1.0/{spec}'.format(spec=spec.replace("yaml", "json"))
swagger_json = app_client.get(url) # type: flask.Response
assert swagger_json.status_code == 200
Expand All @@ -156,14 +156,14 @@ def test_no_swagger_json_api(simple_api_spec_dir, spec):
app = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
app.add_api(spec, options={"serve_spec": False})

app_client = app.app.test_client()
app_client = app.test_client()
url = '/v1.0/{spec}'.format(spec=spec.replace("yaml", "json"))
swagger_json = app_client.get(url) # type: flask.Response
assert swagger_json.status_code == 404


def test_swagger_json_content_type(simple_app):
app_client = simple_app.app.test_client()
app_client = simple_app.test_client()
spec = simple_app._spec_file
url = '/v1.0/{spec}'.format(spec=spec.replace("yaml", "json"))
response = app_client.get(url) # type: flask.Response
Expand All @@ -179,7 +179,7 @@ def route1():
def route2():
return 'single 2'

app_client = simple_app.app.test_client()
app_client = simple_app.test_client()

simple_app.add_url_rule('/single1', 'single1', route1, methods=['GET'])

Expand All @@ -197,13 +197,13 @@ def route2():


def test_resolve_method(simple_app):
app_client = simple_app.app.test_client()
app_client = simple_app.test_client()
resp = app_client.get('/v1.0/resolver-test/method') # type: flask.Response
assert resp.data == b'"DummyClass"\n'


def test_resolve_classmethod(simple_app):
app_client = simple_app.app.test_client()
app_client = simple_app.test_client()
resp = app_client.get('/v1.0/resolver-test/classmethod') # type: flask.Response
assert resp.data.decode('utf-8', 'replace') == '"DummyClass"\n'

Expand Down
2 changes: 1 addition & 1 deletion tests/api/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def fix_data(data):


def test_errors(problem_app):
app_client = problem_app.app.test_client()
app_client = problem_app.test_client()

greeting404 = app_client.get('/v1.0/greeting') # type: flask.Response
assert greeting404.content_type == 'application/problem+json'
Expand Down
10 changes: 5 additions & 5 deletions tests/api/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@


def test_headers_jsonifier(simple_app):
app_client = simple_app.app.test_client()
app_client = simple_app.test_client()

response = app_client.post('/v1.0/goodday/dan', data={}) # type: flask.Response
assert response.status_code == 201
assert response.headers["Location"] == "http://localhost/my/uri"


def test_headers_produces(simple_app):
app_client = simple_app.app.test_client()
app_client = simple_app.test_client()

response = app_client.post('/v1.0/goodevening/dan', data={}) # type: flask.Response
assert response.status_code == 201
assert response.headers["Location"] == "http://localhost/my/uri"


def test_header_not_returned(simple_app):
app_client = simple_app.app.test_client()
app_client = simple_app.test_client()

response = app_client.post('/v1.0/goodday/noheader', data={}) # type: flask.Response
assert response.status_code == 500 # view_func has not returned what was promised in spec
Expand All @@ -31,14 +31,14 @@ def test_header_not_returned(simple_app):


def test_no_content_response_have_headers(simple_app):
app_client = simple_app.app.test_client()
app_client = simple_app.test_client()
resp = app_client.get('/v1.0/test-204-with-headers')
assert resp.status_code == 204
assert 'X-Something' in resp.headers


def test_no_content_object_and_have_headers(simple_app):
app_client = simple_app.app.test_client()
app_client = simple_app.test_client()
resp = app_client.get('/v1.0/test-204-with-headers-nocontent-obj')
assert resp.status_code == 204
assert 'X-Something' in resp.headers
Loading

0 comments on commit 10cc011

Please sign in to comment.