Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more Flask make_response tests && added headers usage during response generation #344

Merged
merged 20 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spectree/plugins/flask_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ def validate(
response_payload = result[0]
elif isinstance(result, flask.Response):
response_payload, status = result, result.status_code
rest.append(result.headers)
else:
response_payload = result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,96 @@
"tags": []
}
},
"/api/return_make_cookies_response": {
"get": {
"description": "",
"operationId": "get__api_return_make_cookies_response",
"parameters": [
{
"description": "",
"in": "query",
"name": "name",
"required": true,
"schema": {
"title": "Name",
"type": "string"
}
},
{
"description": "",
"in": "query",
"name": "limit",
"required": true,
"schema": {
"title": "Limit",
"type": "integer"
}
}
],
"responses": {
"201": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Resp.7068f62"
}
}
},
"description": "Created"
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationError.6a07bef"
}
}
},
"description": "Unprocessable Entity"
}
},
"summary": "return_make_cookies_response_get <GET>",
"tags": []
},
"post": {
"description": "",
"operationId": "post__api_return_make_cookies_response",
"parameters": [],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/JSON.7068f62"
}
}
}
},
"responses": {
"201": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Resp.7068f62"
}
}
},
"description": "Created"
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationError.6a07bef"
}
}
},
"description": "Unprocessable Entity"
}
},
"summary": "return_make_cookies_response_post <POST>",
"tags": []
}
},
"/api/return_make_response": {
"get": {
"description": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,96 @@
"tags": []
}
},
"/api/return_make_cookies_response": {
"get": {
"description": "",
"operationId": "get__api_return_make_cookies_response",
"parameters": [
{
"description": "",
"in": "query",
"name": "name",
"required": true,
"schema": {
"title": "Name",
"type": "string"
}
},
{
"description": "",
"in": "query",
"name": "limit",
"required": true,
"schema": {
"title": "Limit",
"type": "integer"
}
}
],
"responses": {
"201": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Resp.7068f62"
}
}
},
"description": "Created"
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationError.6a07bef"
}
}
},
"description": "Unprocessable Entity"
}
},
"summary": "return_make_cookies_response_get <GET>",
"tags": []
},
"post": {
"description": "",
"operationId": "post__api_return_make_cookies_response",
"parameters": [],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/JSON.7068f62"
}
}
}
},
"responses": {
"201": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Resp.7068f62"
}
}
},
"description": "Created"
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationError.6a07bef"
}
}
},
"description": "Unprocessable Entity"
}
},
"summary": "return_make_cookies_response_post <POST>",
"tags": []
}
},
"/api/return_make_response": {
"get": {
"description": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,96 @@
"tags": []
}
},
"/api/return_make_cookies_response": {
"get": {
"description": "",
"operationId": "get__api_return_make_cookies_response",
"parameters": [
{
"description": "",
"in": "query",
"name": "name",
"required": true,
"schema": {
"title": "Name",
"type": "string"
}
},
{
"description": "",
"in": "query",
"name": "limit",
"required": true,
"schema": {
"title": "Limit",
"type": "integer"
}
}
],
"responses": {
"201": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Resp.7068f62"
}
}
},
"description": "Created"
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationError.6a07bef"
}
}
},
"description": "Unprocessable Entity"
}
},
"summary": "get <GET>",
"tags": []
},
"post": {
"description": "",
"operationId": "post__api_return_make_cookies_response",
"parameters": [],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/JSON.7068f62"
}
}
}
},
"responses": {
"201": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Resp.7068f62"
}
}
},
"description": "Created"
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationError.6a07bef"
}
}
},
"description": "Unprocessable Entity"
}
},
"summary": "post <POST>",
"tags": []
}
},
"/api/return_make_response": {
"get": {
"description": "",
Expand Down
4 changes: 4 additions & 0 deletions tests/flask_imports/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from .dry_plugin_flask import (
test_flask_doc,
test_flask_list_json_request,
test_flask_make_cookies_response_get,
test_flask_make_cookies_response_post,
test_flask_make_response_get,
test_flask_make_response_post,
test_flask_no_response,
Expand All @@ -24,4 +26,6 @@
"test_flask_return_list_request",
"test_flask_make_response_post",
"test_flask_make_response_get",
"test_flask_make_cookies_response_get",
"test_flask_make_cookies_response_post",
]
29 changes: 29 additions & 0 deletions tests/flask_imports/dry_plugin_flask.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import io
import random
import re

import pytest

Expand Down Expand Up @@ -202,6 +203,34 @@ def test_flask_make_response_get(client):
assert resp.json == {"name": payload.name, "score": [payload.limit]}


def test_flask_make_cookies_response_post(client):
payload = JSON(
limit=random.randint(1, 10),
name="user make_response name",
)
resp = client.post("/api/return_make_cookies_response", json=payload.dict())
assert resp.status_code == 201
assert resp.json == {"name": payload.name, "score": [payload.limit]}
cookie_result = re.match(
r"^test_cookie=\"((\w+\s?){3})\";\sPath=/$", resp.headers.get("Set-Cookie")
)
assert cookie_result.group(1) == payload.name


def test_flask_make_cookies_response_get(client):
payload = JSON(
limit=random.randint(1, 10),
name="user make_response name",
)
resp = client.get("/api/return_make_cookies_response", query_string=payload.dict())
assert resp.status_code == 201
assert resp.json == {"name": payload.name, "score": [payload.limit]}
cookie_result = re.match(
r"^test_cookie=\"((\w+\s?){3})\";\sPath=/$", resp.headers.get("Set-Cookie")
)
assert cookie_result.group(1) == payload.name


@pytest.mark.parametrize("pre_serialize", [False, True])
@pytest.mark.parametrize(
"return_what", ["RootResp_JSON", "RootResp_List", "JSON", "List"]
Expand Down
28 changes: 28 additions & 0 deletions tests/test_plugin_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,34 @@ def return_make_response_get():
return response


@app.route("/api/return_make_cookies_response", methods=["POST"])
@api.validate(json=JSON, resp=Response(HTTP_201=Resp))
def return_make_cookies_response_post():
model_data = JSON(**request.json)
response = make_response(
Resp(name=model_data.name, score=[model_data.limit]).dict(), 201
)
response.set_cookie(
key="test_cookie",
value=model_data.name,
)
return response


@app.route("/api/return_make_cookies_response", methods=["GET"])
@api.validate(query=JSON, resp=Response(HTTP_201=Resp))
def return_make_cookies_response_get():
model_data = JSON(**request.args)
response = make_response(
Resp(name=model_data.name, score=[model_data.limit]).dict(), 201
)
response.set_cookie(
key="test_cookie",
value=model_data.name,
)
return response


@app.route("/api/return_root", methods=["GET"])
@api.validate(resp=Response(HTTP_200=RootResp))
def return_root():
Expand Down
Loading