Skip to content

Commit

Permalink
Fix for #10
Browse files Browse the repository at this point in the history
  • Loading branch information
dmuth committed Jul 22, 2023
1 parent b2fae4b commit ec8b00b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
11 changes: 10 additions & 1 deletion lib/apis/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

import json
from urllib.parse import parse_qs

from fastapi import APIRouter
from fastapi import FastAPI, Header, Request
Expand Down Expand Up @@ -47,9 +48,17 @@ async def get(request: Request):
async def post(request: Request):

data = data_default
content_type = request.headers.get("Content-Type", "application/x-www-form-urlencoded")

try:
data = await request.json()
if content_type == "application/json":
data = await request.json()

else:
# Assume that it was a normal form submission
body = await request.body()
data = parse_qs(body)

except json.decoder.JSONDecodeError as e:
logger.warning(f"{__name__}:post(): Caught error deserializing JSON: {e}")

Expand Down
2 changes: 1 addition & 1 deletion lib/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Store our app version here.
#
app_version = "0.0.46"
app_version = "0.0.47"

tags_metadata = [
{
Expand Down
25 changes: 19 additions & 6 deletions tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,51 @@ def test_get():

def test_post():

headers_json = {"Content-Type": "application/json"}
headers_form = {"Content-Type": "application/x-www-form-urlencoded"}

data = {}
response = client.post("/post", json = json.dumps(data))
response = client.post("/post", json = json.dumps(data), headers = headers_json)
assert response.status_code == 200
assert response.json()["source"]["ip"] == "testclient"
assert response.json()["headers"]["host"] == "testserver"
data = json.loads(response.json()["data"])
assert data == {}

data = "broken json"
response = client.post("/post", json = json.dumps(data))
response = client.post("/post", json = json.dumps(data), headers = headers_json)
assert response.status_code == 200
assert response.json()["source"]["ip"] == "testclient"
assert response.json()["headers"]["host"] == "testserver"
data = json.loads(response.json()["data"])
assert data == "broken json"

data = { "cheetah": "chirp", "goat": "bleat" }

response = client.post("/post", json = json.dumps(data))
response = client.post("/post", json = json.dumps(data), headers = headers_json)
assert response.status_code == 200
assert response.json()["source"]["ip"] == "testclient"
assert response.json()["headers"]["host"] == "testserver"
data = json.loads(response.json()["data"])
assert data["cheetah"] == "chirp"

data = [ data ]

response = client.post("/post", json = json.dumps(data))
response = client.post("/post", json = json.dumps(data), headers = headers_json)
assert response.status_code == 200
data = json.loads(response.json()["data"])
assert data[0]["cheetah"] == "chirp"

#
# Test regular form data
#
form_data = "birthyear=1905&press=%20OK%20"
response = client.post("/post", data = form_data, headers = headers_form)
assert response.status_code == 200
assert response.json()["source"]["ip"] == "testclient"
assert response.json()["headers"]["host"] == "testserver"
data = response.json()["data"]
assert data["birthyear"][0] == "1905"
assert data["press"][0] == " OK "


def test_put():

Expand Down

0 comments on commit ec8b00b

Please sign in to comment.