From 0c64dad97d31d214f17a27311a6f31a0be4f7bef Mon Sep 17 00:00:00 2001 From: Emrys Roef Date: Thu, 2 Jun 2022 16:00:19 +0200 Subject: [PATCH] #73 405 method not allowed on put/post/delete request methods --- tests/test_functional.py | 4 ++++ urihandler/views.py | 11 ++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/test_functional.py b/tests/test_functional.py index f8d3fc9..6e2f15b 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -23,6 +23,10 @@ def test_redirect(self, app): res = app.get("/foobar/18", status=303) assert res.status == "303 See Other" + def test_redirect_not_allowed(self, app): + res = app.post("/foobar/18", status=405) + assert res.status == "405 Method Not Allowed" + def test_redirect_no_match(self, app): res = app.get("/test", status=404) assert res.status == "404 Not Found" diff --git a/urihandler/views.py b/urihandler/views.py index 8631da0..1193797 100644 --- a/urihandler/views.py +++ b/urihandler/views.py @@ -1,16 +1,16 @@ +from pyramid.httpexceptions import HTTPMethodNotAllowed from pyramid.view import view_config from pyramid.httpexceptions import ( HTTPSeeOther, HTTPBadRequest, - HTTPNotFound, - HTTPNotModified + HTTPNotFound ) from urihandler.utils import create_version_hash -@view_config(route_name="redirect") +@view_config(route_name="redirect", request_method=("GET", "HEAD", "OPTIONS")) def redirect(request): uri = request.host_url + "/" + request.matchdict["uri"] redirect = request.uri_handler.handle(uri, request) @@ -19,6 +19,11 @@ def redirect(request): return HTTPSeeOther(redirect) +@view_config(route_name="redirect") +def redirect_not_allowed(request): + raise HTTPMethodNotAllowed() + + @view_config(route_name="handle") def handle(request): uri = request.params.get("uri", None)