Repository: aio-libs/aiohttp
Pull request: Support registering OPTIONS HTTP method handlers via RouteTableDef
<!-- Thank you for your contribution! -->

## What do these changes do?

Allows to register Options method handlers for resources. Currently it gives an `AttributeError: 'RouteTableDef' object has no attribute 'options'`

<!-- Please give a short brief about these changes. -->

## Are there changes in behavior for the user?

<!-- Outline any notable behaviour for the end users. -->

## Related issue number

<!-- Are there any issues opened that will be resolved by merging this change? -->
Resolves #4663

## Checklist

- [x] I think the code is well written
- [x] Unit tests for the changes exist
- [ ] Documentation reflects the changes
- [x] If you provide code modification, please add yourself to `CONTRIBUTORS.txt`
  * The format is &lt;Name&gt; &lt;Surname&gt;.
  * Please keep alphabetical order, the file is sorted by names. 
- [x] Add a new news fragment into the `CHANGES` folder
  * name it `<issue_id>.<type>` for example (588.bugfix)
  * if you don't have an `issue_id` change it to the pr id after creating the pr
  * ensure type is one of the following:
    * `.feature`: Signifying a new feature.
    * `.bugfix`: Signifying a bug fix.
    * `.doc`: Signifying a documentation improvement.
    * `.removal`: Signifying a deprecation or removal of public API.
    * `.misc`: A ticket has been closed, but it is not of interest to users.
  * Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."


Problem statement:
RouteTableDef can't register OPTIONS request handlers
🐞 **Describe the bug**
Currently it gives an AttributeError: 'RouteTableDef' object has no attribute 'options'

💡 **To Reproduce**

```py
@route.options('/path')
def opts(request):
   ...
```

💡 **Expected behavior**
It should register decorated function as the OPTIONS request handler for that path

Hints from issue discussion:

Commit message: web_routedef: Allow RouteTableDef to register options method handlers

Fixes https://github.com/aio-libs/aiohttp/issues/4663

Patch to review:
diff --git a/CHANGES/4663.bugfix b/CHANGES/4663.bugfix
new file mode 100644
index 00000000000..46fdb8eacf7
--- /dev/null
+++ b/CHANGES/4663.bugfix
@@ -0,0 +1 @@
+Allows to register Options method handlers for resources.
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt
index d5d53170e9c..22e08d68f0f 100644
--- a/CONTRIBUTORS.txt
+++ b/CONTRIBUTORS.txt
@@ -181,6 +181,7 @@ Mathieu Dugré
 Matt VanEseltine
 Matthieu Hauglustaine
 Matthieu Rigal
+Meet Mangukiya
 Michael Ihnatenko
 Mikhail Burshteyn
 Mikhail Kashkin
diff --git a/aiohttp/web_routedef.py b/aiohttp/web_routedef.py
index 7fbc848d702..8bda2532907 100644
--- a/aiohttp/web_routedef.py
+++ b/aiohttp/web_routedef.py
@@ -191,6 +191,9 @@ def patch(self, path: str, **kwargs: Any) -> _Deco:
     def delete(self, path: str, **kwargs: Any) -> _Deco:
         return self.route(hdrs.METH_DELETE, path, **kwargs)
 
+    def options(self, path: str, **kwargs: Any) -> _Deco:
+        return self.route(hdrs.METH_OPTIONS, path, **kwargs)
+
     def view(self, path: str, **kwargs: Any) -> _Deco:
         return self.route(hdrs.METH_ANY, path, **kwargs)
 
diff --git a/tests/test_route_def.py b/tests/test_route_def.py
index 1a14a6bf5e7..6d1f6f9d389 100644
--- a/tests/test_route_def.py
+++ b/tests/test_route_def.py
@@ -232,6 +232,22 @@ async def handler(request):
     assert str(route.url_for()) == '/path'
 
 
+def test_options_deco(router) -> None:
+    routes = web.RouteTableDef()
+
+    @routes.options('/path')
+    async def handler(request):
+        pass
+
+    router.add_routes(routes)
+
+    assert len(router.routes()) == 1
+
+    route = list(router.routes())[0]
+    assert route.method == 'OPTIONS'
+    assert str(route.url_for()) == '/path'
+
+
 def test_route_deco(router) -> None:
     routes = web.RouteTableDef()
 


Code review comments:
diff_hunk: @@ -0,0 +1 @@
+Allows to register Options method handlers for resources.
path: CHANGES/4663.bugfix
text: @user1:
```suggestion
Support registering OPTIONS HTTP method handlers via RouteTableDef.
```