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

(fix): Make request.get_headers fully case insensitive #54

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion src/gleam/http/request.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ pub fn from_uri(uri: Uri) -> Result(Request(String), Nil) {
/// If the request does not have that header then `Error(Nil)` is returned.
///
pub fn get_header(request: Request(body), key: String) -> Result(String, Nil) {
list.key_find(request.headers, string.lowercase(key))
use #(_, value) <- result.try(
list.find(request.headers, fn(header) {
string.lowercase(header.0) == string.lowercase(key)
dmmulroy marked this conversation as resolved.
Show resolved Hide resolved
}),
)
Ok(value)
}

/// Set the header with the given value under the given header key.
Expand Down
26 changes: 26 additions & 0 deletions test/gleam/http/request_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,32 @@ pub fn get_req_header_test() {
|> should.equal(Error(Nil))
}

pub fn get_req_case_insensitive_header_test() {
let make_request = fn(headers) {
Request(
method: http.Get,
headers: headers,
body: Nil,
scheme: http.Https,
host: "example.com",
port: None,
path: "/",
query: None,
)
}

let header_key = "GLEAM"
let request = make_request([#("ANSWER", "42"), #("GLEAM", "awesome")])
dmmulroy marked this conversation as resolved.
Show resolved Hide resolved
request
|> request.get_header(header_key)
|> should.equal(Ok("awesome"))

let request = make_request([#("answer", "42")])
request
|> request.get_header(header_key)
|> should.equal(Error(Nil))
}

pub fn set_req_body_test() {
let body =
"<html>
Expand Down
Loading