Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds "remove cookie" function to request module #57

Merged
merged 5 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/gleam/http/request.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,28 @@ pub fn get_cookies(req) -> List(#(String, String)) {
})
|> list.flatten()
}

/// Remove a cookie from a request
///
/// Remove a cookie from the request. If no cookie is found return the request unchanged.
/// This will not remove the cookie from the client.
pub fn remove_cookie(req: Request(body), name: String) {
case list.key_pop(req.headers, "cookie") {
Ok(#(cookies_string, headers)) -> {
let new_cookies_string =
string.split(cookies_string, ";")
|> list.filter(fn(str) {
string.trim(str)
|> string.split_once("=")
// Keep cookie if name does not match
|> result.map(fn(tup) { tup.0 != name })
// Don't do anything with malformed cookies
|> result.unwrap(True)
})
|> string.join(";")

Request(..req, headers: [#("cookie", new_cookies_string), ..headers])
}
Error(_) -> req
}
}
37 changes: 37 additions & 0 deletions test/gleam/http/request_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,40 @@ pub fn set_req_cookies_test() {
|> request.get_header("cookie")
|> should.equal(Ok("k1=v1; k2=v2"))
}

pub fn remove_cookie_from_request_test() {
let req =
request.new()
|> request.set_cookie("FIRST_COOKIE", "first")
|> request.set_cookie("SECOND_COOKIE", "second")
|> request.set_cookie("THIRD_COOKIE", "third")

req
|> request.get_header("cookie")
|> should.be_ok
|> should.equal(
"FIRST_COOKIE=first; SECOND_COOKIE=second; THIRD_COOKIE=third",
)

let modified_req =
req
|> request.remove_cookie("SECOND_COOKIE")

modified_req
|> request.get_header("cookie")
|> should.be_ok
|> should.equal("FIRST_COOKIE=first; THIRD_COOKIE=third")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have a test to check that if you try and remove "SECOND" it won't remove "SECOND_COOKIE" please 🙏

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, good catch that was a bug. I had to rework the function a tiny bit to fix it. Update pushed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you


pub fn only_remove_matching_cookies_test() {
request.new()
|> request.set_cookie("FIRST_COOKIE", "first")
|> request.set_cookie("SECOND_COOKIE", "second")
|> request.set_cookie("THIRD_COOKIE", "third")
|> request.remove_cookie("SECOND")
|> request.get_header("cookie")
|> should.be_ok
|> should.equal(
"FIRST_COOKIE=first; SECOND_COOKIE=second; THIRD_COOKIE=third",
)
}