Skip to content

Commit

Permalink
OpenAPI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ondratu committed Jun 3, 2024
1 parent c245ccc commit 8e60fd4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
6 changes: 6 additions & 0 deletions examples/openapi3.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def cors_response(req, res):
@app.before_response()
def before_each_response(req):
"""Check API before process each response."""
log.warn("BEFORE")
req.api = OpenAPIRequest(req)
try:
unmarshal_request(req.api, app.openapi_spec)
Expand All @@ -100,9 +101,14 @@ def before_each_response(req):
@app.after_response()
def after_each_response(req, res):
"""Check if ansewer is valid by OpenAPI."""
log.warn("AFTER")
if not hasattr(req, "api"):
req.api = OpenAPIRequest(req)
try:
log.error("X type of request: %s", type(req))
log.error("X mro of request: %s", type(req).__mro__)
log.error("X type of request: %s", type(req.api))
log.error("X mro of request: %s", type(req.api).__mro__)
unmarshal_response(
req.api,
OpenAPIResponse(res),
Expand Down
10 changes: 9 additions & 1 deletion poorwsgi/openapi_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
:Classes: OpenAPIRequest, OpenAPIResponse
"""
from collections import OrderedDict
import logging

import re

from openapi_core.protocols import Request, Response
from openapi_core.validation.request.datatypes import RequestParameters


class OpenAPIRequest(Request):
class OpenAPIRequest():
"""Wrapper of PoorWSGI request to OpenAPIRequest.
Be careful with testing of big incoming request body property, which
Expand All @@ -29,21 +30,25 @@ def __init__(self, request):
@property
def host_url(self):
"""Return host_url for validator."""
logging.warning("-> host_url")
return self.request.construct_url('')

@property
def path(self):
"""Return method path"""
logging.warning("-> path")
return self.request.path

@property
def method(self):
"""Return method in lower case for validator."""
logging.warning("-> method")
return self.request.method.lower()

@property
def full_url_pattern(self):
"""Return full_url_pattern for validator."""
logging.warning("-> full_url_pattern")
if self.request.uri_rule is None:
return self.host_url+self.request.uri
return self.host_url+OpenAPIRequest.re_pattern.sub(
Expand All @@ -52,6 +57,7 @@ def full_url_pattern(self):
@property
def parameters(self):
"""Return RequestParameters object for validator."""
logging.warning("-> parameters")
path_args = OrderedDict()
for (key, val) in self.request.path_args.items():
# allowed openapi core types...
Expand All @@ -70,11 +76,13 @@ def parameters(self):
@property
def body(self):
"""Return request data for validator."""
logging.warning("-> body")
return self.request.data

@property
def mimetype(self):
"""Return request mime_type for validator."""
logging.warning("-> mime_type")
return self.request.mime_type


Expand Down
1 change: 1 addition & 0 deletions tests_integrity/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def test_json_post_unicode(self, url):
method="POST", json=data,
response_spec=SPEC)
assert res.json()["request"] == data
assert False

def test_json_post_unicode_struct(self, url):
data = {"city": "Česká Lípa"}
Expand Down

0 comments on commit 8e60fd4

Please sign in to comment.