🧚 A practical web framework for Gleam

Test cookie helper

+26 -37
+3 -16
src/wisp.gleam
··· 1802 1802 |> request.get_cookies 1803 1803 |> list.key_find(name), 1804 1804 ) 1805 - case security { 1805 + use value <- result.try(case security { 1806 1806 PlainText -> base.decode64(value) 1807 1807 Signed -> verify_signed_message(request, value) 1808 - } 1809 - |> result.try(bit_string.to_string) 1808 + }) 1809 + bit_string.to_string(value) 1810 1810 } 1811 - 1812 - /// Get all the cookies for a request. 1813 - /// 1814 - /// Any malformed cookies will be ignored, as specified in RFC6265. 1815 - /// 1816 - /// # Examples 1817 - /// 1818 - /// ```gleam 1819 - /// wisp.get_cookies(request) 1820 - /// // -> [#("accepted", "1"), #("group", "A")] 1821 - /// ``` 1822 - /// 1823 - pub const get_cookies = request.get_cookies 1824 1811 1825 1812 // 1826 1813 // Testing
+23
test/wisp/testing_test.gleam
··· 581 581 #("accept", "application/json"), 582 582 ]) 583 583 } 584 + 585 + pub fn set_cookie_plain_text_test() { 586 + let req = 587 + testing.get("/", []) 588 + |> testing.set_cookie("abc", "1234", wisp.PlainText) 589 + |> testing.set_cookie("def", "5678", wisp.PlainText) 590 + req.headers 591 + |> should.equal([#("cookie", "abc=MTIzNA; def=NTY3OA")]) 592 + } 593 + 594 + pub fn set_cookie_signed_test() { 595 + let req = 596 + testing.get("/", []) 597 + |> testing.set_cookie("abc", "1234", wisp.Signed) 598 + |> testing.set_cookie("def", "5678", wisp.Signed) 599 + req.headers 600 + |> should.equal([ 601 + #( 602 + "cookie", 603 + "abc=SFM1MTI.MTIzNA.QWGuB_lZLssnh71rC6R5_WOr8MDr8dxE3C_2JvLRAAC4ad4SnmQk0Fl_6_RrtmzdH2O3WaNPExkJsuwBixtWIA; def=SFM1MTI.NTY3OA.R3HRe5woa1qwxvjRUC5ggQVd3hTqGCXIk_4ybU35SXPtGvLrFpHBXWGIjyG5QeuEk9j3jnWIL3ct18olJiSCMw", 604 + ), 605 + ]) 606 + }
-21
test/wisp_test.gleam
··· 968 968 out 969 969 |> should.equal(message) 970 970 } 971 - 972 - pub fn get_cookies_test() { 973 - let request = 974 - testing.get( 975 - "/", 976 - [ 977 - #("cookie", "id=123; flash=hi-there; other=456"), 978 - #("cookie", "wibble=789; id=456"), 979 - ], 980 - ) 981 - 982 - request 983 - |> wisp.get_cookies 984 - |> should.equal([ 985 - #("id", "123"), 986 - #("flash", "hi-there"), 987 - #("other", "456"), 988 - #("wibble", "789"), 989 - #("id", "456"), 990 - ]) 991 - }