Repository: huge-success/sanic
Pull request #1457 (2019-01-03 23:02:29)

enforce integer for max-age cookie
Closes: https://github.com/huge-success/sanic/issues/1452

codecov[bot]: # [Codecov](https://codecov.io/gh/huge-success/sanic/pull/1457?src=pr&el=h1) Report
> Merging [#1457](https://codecov.io/gh/huge-success/sanic/pull/1457?src=pr&el=desc) into [master](https://codecov.io/gh/huge-success/sanic/commit/f4f90cada452c7c634540d34ea922b6ed6d44591?src=pr&el=desc) will **increase** coverage by `0.07%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/huge-success/sanic/pull/1457/graphs/tree.svg?width=650&token=NqLbJHUqoR&height=150&src=pr)](https://codecov.io/gh/huge-success/sanic/pull/1457?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##           master   #1457      +/-   ##
=========================================
+ Coverage   91.32%   91.4%   +0.07%     
=========================================
  Files          17      17              
  Lines        1718    1722       +4     
  Branches      322     324       +2     
=========================================
+ Hits         1569    1574       +5     
  Misses        123     123              
+ Partials       26      25       -1
```


| [Impacted Files](https://codecov.io/gh/huge-success/sanic/pull/1457?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [sanic/cookies.py](https://codecov.io/gh/huge-success/sanic/pull/1457/diff?src=pr&el=tree#diff-c2FuaWMvY29va2llcy5weQ==) | `100% <100%> (ø)` | :arrow_up: |
| [sanic/router.py](https://codecov.io/gh/huge-success/sanic/pull/1457/diff?src=pr&el=tree#diff-c2FuaWMvcm91dGVyLnB5) | `95.89% <0%> (+0.45%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/huge-success/sanic/pull/1457?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/huge-success/sanic/pull/1457?src=pr&el=footer). Last update [f4f90ca...b4b37f3](https://codecov.io/gh/huge-success/sanic/pull/1457?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

yunstanford: hey, can you fix the linting issue ?
r0fls: Hmm, seems there are some unrelated issues with the Travis build maybe, e.g.: `pluggy.PluginValidationError: Plugin 'pytest_cov' could not be loaded: (pytest 3.3.2 (/home/travis/build/huge-success/sanic/.tox/py35/lib/python3.5/site-packages), Requirement.parse('pytest>=3.6'))!`
yunstanford: @r0fls the issue has been fixed in master branch. you can merge/rebase from master. it should be fixed.

Patch:
diff --git a/sanic/cookies.py b/sanic/cookies.py
index 13526555..c53156ee 100644
--- a/sanic/cookies.py
+++ b/sanic/cookies.py
@@ -2,6 +2,8 @@ import re
 import string
 
 
+DEFAULT_MAX_AGE = 0
+
 # ------------------------------------------------------------ #
 #  SimpleCookie
 # ------------------------------------------------------------ #
@@ -103,6 +105,9 @@ class Cookie(dict):
         if key not in self._keys:
             raise KeyError("Unknown cookie property")
         if value is not False:
+            if key.lower() == "max-age":
+                if not str(value).isdigit():
+                    value = DEFAULT_MAX_AGE
             return super().__setitem__(key, value)
 
     def encode(self, encoding):


Test patch:
diff --git a/tests/test_cookies.py b/tests/test_cookies.py
index 4a7d4e18..9ad635d2 100644
--- a/tests/test_cookies.py
+++ b/tests/test_cookies.py
@@ -2,7 +2,7 @@ from datetime import datetime, timedelta
 from http.cookies import SimpleCookie
 from sanic.response import text
 import pytest
-from sanic.cookies import Cookie
+from sanic.cookies import Cookie, DEFAULT_MAX_AGE
 
 # ------------------------------------------------------------ #
 #  GET
@@ -138,7 +138,7 @@ def test_cookie_set_same_key(app):
     assert response.cookies["test"].value == "pass"
 
 
-@pytest.mark.parametrize("max_age", ["0", 30, "30"])
+@pytest.mark.parametrize("max_age", ["0", 30, 30.0, 30.1, "30", "test"])
 def test_cookie_max_age(app, max_age):
     cookies = {"test": "wait"}
 
@@ -153,7 +153,11 @@ def test_cookie_max_age(app, max_age):
     assert response.status == 200
 
     assert response.cookies["test"].value == "pass"
-    assert response.cookies["test"]["max-age"] == str(max_age)
+
+    if str(max_age).isdigit() and int(max_age) == float(max_age):
+        assert response.cookies["test"]["max-age"] == str(max_age)
+    else:
+        assert response.cookies["test"]["max-age"] == str(DEFAULT_MAX_AGE)
 
 
 @pytest.mark.parametrize(