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

(feat): Add request.set_headers and request.prepend_headers #53

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions src/gleam/http/request.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,34 @@ pub fn prepend_header(
Request(..request, headers: headers)
}

/// Set a request's headers using a list.
///
/// Similar to `set_header` but for setting more than a single header at once.
/// Existing headers on the request will be replaced.
pub fn set_headers(
request: Request(body),
headers: List(#(String, String)),
) -> Request(body) {
let new_headers =
list.fold(headers, [], fn(acc, header) {
list.key_set(acc, string.lowercase(header.0), header.1)
})
Request(..request, headers: new_headers)
}

/// Prepend the request header with the given key/values.
///
/// Similar to `prepend_header` but for prepending multiple key/values at once.
/// If a header already exists it prepends another header with the same key.
pub fn prepend_headers(
request: Request(body),
headers: List(#(String, String)),
) -> Request(body) {
list.fold(headers, request, fn(acc, header) {
prepend_header(acc, header.0, header.1)
})
}

// TODO: record update syntax, which can't be done currently as body type changes
/// Set the body of the request, overwriting any existing body.
///
Expand Down
104 changes: 104 additions & 0 deletions test/gleam/http/request_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,71 @@ pub fn set_request_header_lowercases_key_test() {
|> should.equal([#("uppercase_gleam", "awesome")])
}

pub fn set_req_headers_test() {
let request =
Request(
method: http.Get,
headers: [],
body: Nil,
scheme: http.Https,
host: "example.com",
port: None,
path: "/",
query: None,
)
|> request.set_headers([
#("gleam", "awesome"),
#("ocaml", "also pretty cool"),
])

request.headers
|> should.equal([#("gleam", "awesome"), #("ocaml", "also pretty cool")])

// Set updates existing
let request =
request
|> request.set_headers([#("gleam", "the best")])

request.headers
|> should.equal([#("gleam", "the best")])
}

pub fn set_request_headers_maintains_value_casing_test() {
let request =
Request(
method: http.Get,
headers: [],
body: Nil,
scheme: http.Https,
host: "example.com",
port: None,
path: "/",
query: None,
)
|> request.set_headers([#("gleam", "UPPERCASE_AWESOME")])

request.headers
|> should.equal([#("gleam", "UPPERCASE_AWESOME")])
}

pub fn set_request_headers_lowercases_key_test() {
let request =
Request(
method: http.Get,
headers: [],
body: Nil,
scheme: http.Https,
host: "example.com",
port: None,
path: "/",
query: None,
)
|> request.set_headers([#("UPPERCASE_GLEAM", "awesome")])

request.headers
|> should.equal([#("uppercase_gleam", "awesome")])
}

pub fn prepend_req_header_test() {
let headers = []
let request =
Expand Down Expand Up @@ -401,6 +466,45 @@ pub fn prepend_req_header_test() {
])
}

pub fn prepend_req_headers_test() {
let headers = []
let request =
Request(
method: http.Get,
headers: headers,
body: Nil,
scheme: http.Https,
host: "example.com",
port: None,
path: "/",
query: None,
)
|> request.prepend_headers([#("answer", "42")])

request.headers
|> should.equal([#("answer", "42")])

let request =
request
|> request.prepend_headers([#("gleam", "awesome")])

// request should have two headers now
request.headers
|> should.equal([#("gleam", "awesome"), #("answer", "42")])

let request =
request
|> request.prepend_headers([#("gleam", "awesome")])

// request repeats the existing header
request.headers
|> should.equal([
#("gleam", "awesome"),
#("gleam", "awesome"),
#("answer", "42"),
])
}

pub fn get_req_cookies_test() {
request.new()
|> request.prepend_header("cookie", "k1=v1; k2=v2")
Expand Down
Loading