Skip to content

Commit cc6fa34

Browse files
committed
Added get_cookies() and create_cookie()
1 parent 3131a96 commit cc6fa34

File tree

3 files changed

+84
-4
lines changed

3 files changed

+84
-4
lines changed

test_server.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from micropyserver import MicroPyServer
2+
import utils
3+
4+
server = MicroPyServer()
5+
6+
7+
def index(request):
8+
server.send('OK')
9+
10+
11+
def stop(request):
12+
server.stop()
13+
14+
15+
def set_cookies(request):
16+
cookies_header = utils.create_cookie("name", "value", expires="Sat, 01-Jan-2030 00:00:00 GMT")
17+
utils.send_response(server, "OK", extend_headers=[cookies_header])
18+
19+
20+
def get_cookies(request):
21+
cookies = utils.get_cookies(request)
22+
utils.send_response(server, str(cookies))
23+
24+
25+
server.add_route("/", index)
26+
server.add_route("/stop", stop)
27+
server.add_route("/set_cookies", set_cookies)
28+
server.add_route("/get_cookies", get_cookies)
29+
30+
server.start()

test_utils.py

+23-4
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ def test_parse_query_string_method_should_return_two_params(self):
2424
def test_get_request_query_params_method_should_return_two_params(self):
2525
request = "GET /?param_one=one&param_two=two HTTP/1.1\r\nHost: localhost\r\n\r\n"
2626
self.assertEqual(utils.get_request_query_params(request), {"param_one": "one", "param_two": "two"},
27-
"Should be 'param_one=one&param_two=two'")
27+
"Should be result with two params")
2828

2929
def test_get_request_post_params_method_should_return_two_params(self):
3030
request = "POST /post HTTP/1.1\r\nHost: localhost\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 27\r\n\r\nparam_one=one&param_two=two"
3131
self.assertEqual(utils.get_request_post_params(request), {"param_one": "one", "param_two": "two"},
32-
"Should be 'param_one=one&param_two=two'")
32+
"Should be result with two params")
3333

3434
def test_unquote_method_should_return_empty_string(self):
3535
self.assertEqual(utils.unquote(""), "", "Should be empty string")
@@ -40,8 +40,27 @@ def test_unquote_method_should_return_string(self):
4040
def test_unquote_method_should_return_unquoted_string(self):
4141
self.assertEqual(utils.unquote(
4242
"%D0%BF%D0%B0%D1%80%D0%B0%D0%BC%D0%B5%D1%82%D1%80%20%D0%B8%20%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20%25"),
43-
"параметр и значение %",
44-
"Should be 'параметр и значение %'")
43+
"параметр и значение %",
44+
"Should be 'параметр и значение %'")
45+
46+
def test_get_cookies_method_should_return_param(self):
47+
request = "GET / HTTP/1.1\r\nHost: localhost\r\nCookie:foo=bar"
48+
self.assertEqual(utils.get_cookies(request), {"foo": "bar"}, "Should be result with one param")
49+
50+
def test_get_cookies_method_should_return_two_params(self):
51+
request = "GET / HTTP/1.1\r\nHost: localhost\r\nCookie:foo=bar; abc=def\r\nContent-Length: 4\r\n\r\ntest"
52+
self.assertEqual(utils.get_cookies(request), {"foo": "bar", "abc": "def"}, "Should be result with two params")
53+
54+
def test_create_cookie_method_should_return_correct_cookie(self):
55+
name = "name"
56+
value = "value"
57+
path = "/"
58+
domain = "localhost"
59+
expires = "Sat, 01-Jan-2030 00:00:00 GMT"
60+
utils.create_cookie(name, value, path, domain, expires)
61+
self.assertEqual(utils.create_cookie(name, value, path, domain, expires),
62+
"Set-Cookie: name=value; path=/; domain=localhost; expires=Sat, 01-Jan-2030 00:00:00 GMT",
63+
"Should be correct cookie header")
4564

4665

4766
if __name__ == "__main__":

utils.py

+31
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,34 @@ def unquote(string):
178178
extend(item)
179179

180180
return bytes(res).decode("utf-8")
181+
182+
183+
def get_cookies(request):
184+
""" return cookies """
185+
lines = request.split("\r\n")
186+
cookie_string = None
187+
for line in lines:
188+
if line.find(":") is not -1:
189+
header, value = line.split(':', 1)
190+
if header.lower() == "cookie":
191+
cookie_string = value
192+
cookies = {}
193+
if cookie_string:
194+
for cookie in cookie_string.split('; '):
195+
name, value = cookie.strip().split('=', 1)
196+
cookies[name] = value
197+
198+
return cookies
199+
200+
201+
def create_cookie(name, value, path="/", domain=None, expires=None):
202+
""" create cookie header """
203+
cookie = "Set-Cookie: " + str(name) + "=" + str(value)
204+
if path:
205+
cookie = cookie + "; path=" + path
206+
if domain:
207+
cookie = cookie + "; domain=" + domain
208+
if expires:
209+
cookie = cookie + "; expires=" + expires
210+
211+
return cookie

0 commit comments

Comments
 (0)