From 4066bf92f76dced04ce6f4304883a2855b53450d Mon Sep 17 00:00:00 2001 From: Homy Date: Sat, 6 May 2023 19:49:24 +0300 Subject: [PATCH] Create check-if-rest. py this must be extended to run requests other then GET, and do more checks... --- check-if-rest. py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 check-if-rest. py diff --git a/check-if-rest. py b/check-if-rest. py new file mode 100644 index 0000000..5899503 --- /dev/null +++ b/check-if-rest. py @@ -0,0 +1,38 @@ +import requests + +# Define the base URL and endpoint to test +base_url = "https://api.example.com" +endpoint = "/users" + +# Send a GET request to the endpoint and inspect the response +response = requests.get(base_url + endpoint) + +# Check the response status code +if response.status_code != 200: + print("Endpoint does not comply with REST - invalid status code") + exit(1) + +# Check the response content type +if response.headers["Content-Type"] != "application/json": + print("Endpoint does not comply with REST - invalid content type") + exit(1) + +# Check that the response contains a list of resources +if not isinstance(response.json(), list): + print("Endpoint does not comply with REST - response should be a list") + exit(1) + +# Check that each resource in the response contains a unique ID +resource_ids = set() +for resource in response.json(): + if "id" not in resource: + print("Endpoint does not comply with REST - resource has no ID") + exit(1) + resource_id = resource["id"] + if resource_id in resource_ids: + print("Endpoint does not comply with REST - duplicate resource ID") + exit(1) + resource_ids.add(resource_id) + +# If all checks pass, the endpoint complies with REST +print("Endpoint complies with REST")