Skip to content

Commit

Permalink
Merge pull request #461 from MasoniteFramework/fix/460
Browse files Browse the repository at this point in the history
fixed cookie jar
  • Loading branch information
josephmancuso authored Dec 11, 2021
2 parents a507474 + 2137715 commit 7b3bb76
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
4 changes: 2 additions & 2 deletions config/database.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Database Settings """
from masonite import env
from src.masonite import env
from masoniteorm.query import QueryBuilder
from masoniteorm.connections import ConnectionResolver
from masonite.environment import LoadEnvironment
from src.masonite.environment import LoadEnvironment


LoadEnvironment()
Expand Down
3 changes: 0 additions & 3 deletions config/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
UploadProvider, ViewProvider,
WhitenoiseProvider)

from masoniteorm.providers.ORMProvider import ORMProvider

"""Providers List
Providers are a simple way to remove or add functionality for Masonite
The providers in this list are either ran on server start or when a
Expand All @@ -26,7 +24,6 @@
WhitenoiseProvider,
ViewProvider,
AuthenticationProvider,
ORMProvider,

# Optional Framework Providers
MailProvider,
Expand Down
30 changes: 22 additions & 8 deletions src/masonite/cookies/CookieJar.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pendulum
from .Cookie import Cookie
from ..helpers import cookie_expire_time

from ..helpers.time import cookie_expire_time


class CookieJar:
Expand All @@ -12,26 +14,37 @@ def add(self, name, value, **options):
self.cookies.update({name: Cookie(name, value, **options)})

def all(self):
cookies = self.loaded_cookies
cookies.update(self.cookies)
return cookies

def all_added(self):
return self.cookies

def get(self, name):
aggregate = self.loaded_cookies
aggregate.update(self.cookies)
aggregate = self.all()
return aggregate.get(name)

def exists(self, name):
return name in self.cookies or name in self.loaded_cookies

def is_expired(self, name):
cookie = self.get(name)
return cookie.expires < pendulum.now()

def delete(self, name):
self.deleted_cookies.update(
{
name: Cookie(
name, "", expires=cookie_expire_time("2 months"), timezone="GMT"
name, "", expires=cookie_expire_time("expired"), timezone="GMT"
)
}
)
if name in self.cookies:
return self.cookies.pop(name)
self.cookies.pop(name)

if name in self.loaded_cookies:
self.loaded_cookies.pop(name)

def load_cookie(self, key, value):
self.loaded_cookies.update({key: Cookie(key, value)})
Expand All @@ -47,13 +60,14 @@ def to_dict(self):

def load(self, cookie_string):
for compound_value in cookie_string.split("; "):
key, value = compound_value.split("=", 1)
self.load_cookie(key, value)
if "=" in compound_value:
key, value = compound_value.split("=", 1)
self.load_cookie(key, value)
return self

def render_response(self):
cookies = []
for name, cookie in self.all().items():
for name, cookie in {**self.deleted_cookies, **self.all_added()}.items():
cookies.append(("Set-Cookie", cookie.render()))

return cookies
2 changes: 1 addition & 1 deletion src/masonite/drivers/session/SessionCookieDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def __collect_data(self, flash_only=False):
dict
"""
cookies = {}
all_cookies = self.request.get_cookies().to_dict()
all_cookies = self.request.get_cookies()
for key, value in all_cookies.items():
if not (key.startswith("f_") or key.startswith("s_")):
continue
Expand Down
2 changes: 1 addition & 1 deletion src/masonite/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ def get_cookies(self):
Returns:
dict -- Returns all the cookies.
"""
return self.cookie_jar
return self.cookie_jar.all()

def get_raw_cookie(self, provided_cookie):
return self.cookie_jar.get(provided_cookie)
Expand Down
5 changes: 4 additions & 1 deletion tests/core/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ def test_request_mini_field_storage_returns_a_list(self):
self.assertEqual(self.request.input('test'), ['1', '2'])

def test_request_get_cookies_returns_cookies(self):
self.assertEqual(self.request.get_cookies(), self.request.cookie_jar)
wsgi = generate_wsgi()
request = Request(wsgi)
print(request.get_cookies())
self.assertTrue('setcookie' in self.request.get_cookies())

def test_request_set_user_sets_object(self):
self.assertEqual(self.request.set_user(object), self.request)
Expand Down

0 comments on commit 7b3bb76

Please sign in to comment.