diff options
author | Michael McVady <femtonaut@gmail.com> | 2023-02-16 18:26:12 -0600 |
---|---|---|
committer | Michael McVady <femtonaut@gmail.com> | 2023-02-16 18:26:12 -0600 |
commit | c5cd89c0b21e5bb9a79d445f2ed4630c5bf678f0 (patch) | |
tree | c9f44bba8d76ecad0dd26a9ed2bfc5c6c2ce8182 | |
parent | 4bfab86f636f2392f92037058c783c2634ac2060 (diff) |
Fix DELETE and tests
-rw-r--r-- | src/clog.c | 29 | ||||
-rwxr-xr-x | tests.py | 43 |
2 files changed, 40 insertions, 32 deletions
@@ -40,7 +40,8 @@ static const char *accept_json = "application/json"; static const char *database = "db"; static const char * const error_msg[] = { - [201] = "OK DOKEY", + [HTTP_STATUS_OK] = "OK", // 200 + [HTTP_STATUS_CREATED] = "Resource created successfully.", // 201 [HTTP_STATUS_BAD_REQUEST] = "There was an error processing the request data.", // 403? [HTTP_STATUS_NOT_FOUND] = "Resource not found.", // 404 [HTTP_STATUS_INTERNAL_ERROR] = "There was an error processing the request.", // 500 @@ -150,7 +151,8 @@ get_content_type(struct http_request *req) { return HTML; } -int http_ok_resp( +int +http_ok_resp( struct http_request *req, enum content_type type, enum http_status_code status, @@ -189,7 +191,8 @@ int http_ok_resp( return KORE_RESULT_OK; } -int http_err_resp( +int +http_err_resp( struct http_request *req, enum content_type type, enum http_status_code status @@ -296,8 +299,6 @@ post_posts(struct http_request *req) { const char *title = NULL; const char *body = NULL; - struct post_query pq; - struct kore_json_item *item = NULL; struct kore_json json; @@ -356,23 +357,7 @@ post_posts(struct http_request *req) { out: ; - // XXX Not sure about this hack to return the created resource. - if (status == HTTP_STATUS_CREATED) { - - post_query_init(&pq, type, id); - - (void) sql_select_posts(&pq); - if (pq.status == QUERY_STATUS_NOT_FOUND) - http_err_resp(req, pq.type, HTTP_STATUS_NOT_FOUND); - else if (pq.status == QUERY_STATUS_ERROR) - http_err_resp(req, pq.type, HTTP_STATUS_INTERNAL_ERROR); - else - http_ok_resp(req, pq.type, HTTP_STATUS_OK, pq.result); - - post_query_cleanup(&pq); - } - else - http_err_resp(req, type, status); + http_err_resp(req, type, status); kore_json_cleanup(&json); @@ -1,5 +1,6 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 +import logging import json import sys import uuid @@ -7,10 +8,13 @@ import uuid import requests BASE_URL = "http://localhost:8888" +DELETE_UUID = "00000000-0000-dddd-0000-000000000000" POST_UUID = "2dc42f26-884e-4b10-abfb-4fd394aa9d62" BAD_UUID = "00000000-0000-1111-0000-000000000000" INVALID_UUID = "0" +log = logging.getLogger(__name__) + def test_redirect(): url = f"{BASE_URL}/asdf" @@ -100,14 +104,12 @@ def test_json_post_posts(): uuid_ = str(uuid.uuid4()) data = {"id": uuid_, "title": "title", "body": "body"} r = requests.post(url, headers={"Accept": "application/json"}, json=data) - # FIXME need to refactor to fix status code here. - # assert r.status_code == 201 - assert r.status_code == 200 + + assert r.status_code == 201 + j = r.json() - assert len(j) == 1 - assert "title" == j[0]["title"] - assert "body" == j[0]["body"] + assert "Resource created successfully." == j url = f"{BASE_URL}/posts/{uuid_}" r = requests.get(url, headers={"Accept": "application/json"}) @@ -119,6 +121,27 @@ def test_json_post_posts(): assert "body" == j[0]["body"] +def test_json_delete_posts(): + headers = {"Accept": "application/json"} + url = f"{BASE_URL}/posts" + uuid_ = DELETE_UUID + data = {"id": uuid_, "title": "title", "body": "body"} + + # Create post copy-pasta. + r = requests.post(url, headers=headers, json=data) + assert r.status_code == 201 + j = r.json() + + url = f"{BASE_URL}/posts/{uuid_}" + r = requests.delete(url, headers=headers) + assert r.status_code == 200 + j = r.json() + + r = requests.get(url, headers=headers) + assert r.status_code == 404 + j = r.json() + + def test_json_get_post_not_found(): url = f"{BASE_URL}/posts/{BAD_UUID}" r = requests.get(url, headers={"Accept": "application/json"}) @@ -128,7 +151,7 @@ def test_json_get_post_not_found(): assert "Resource not found" in j url = f"{BASE_URL}/posts/{INVALID_UUID}" - r = requests.get(url, headers={"Accept": "application/json"}) + r = requests.delete(url, headers={"Accept": "application/json"}) assert r.status_code == 404 j = r.json() @@ -143,10 +166,10 @@ def main(): globals()[func]() except Exception: winner = False - print(f"{func} failed.") + log.exception(f"🚫 {func} failed 🚫🏆") if winner: - print("🏆 You're winner !") + log.info("🏆 You're winner !") if __name__ == "__main__": |