diff --git a/durable-objects/src/entry.py b/durable-objects/src/entry.py index 505924f..92799e6 100644 --- a/durable-objects/src/entry.py +++ b/durable-objects/src/entry.py @@ -20,7 +20,8 @@ class Default(WorkerEntrypoint): async def fetch(self, request): url = urlparse(request.url) - list_id = url.path.split("/")[1] + segments = url.path.split("/") + list_id = segments[1] if list_id == "": return Response( "Go to //show to see messages\nGo to //add/ to add a message", @@ -31,12 +32,13 @@ async def fetch(self, request): stub = self.env.LISTS.get(do_id) # Note: This is adding a message via GET URL path for simplicity, in a real app, use a POST - if "/add" in url.path: + action = segments[2] if len(segments) > 2 else "" + if action == "add" and len(segments) > 3: # message = await request.text() - message = url.path.split("/")[3] + message = segments[3] await stub.add_message(message) return Response("Message sent") - elif "/show" in url.path: + elif action == "show": messages = await stub.get_messages() if not messages: return Response("No messages") diff --git a/tests/test_examples.py b/tests/test_examples.py index a44e7f0..e90598e 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -112,6 +112,11 @@ def test_durable_objects(dev_server): response = requests.get(f"http://localhost:{port}/room-2/show") assert response.status_code == 200 assert response.text == "No messages" + response = requests.get(f"http://localhost:{port}/address-book/show") + assert response.status_code == 200 + assert response.text == "No messages" + response = requests.get(f"http://localhost:{port}/room-1/add") + assert response.status_code == 404 def test_sync_http_clients(dev_server):