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(Request): add with_headers function #110

Merged
merged 1 commit into from
Jul 16, 2024
Merged
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/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ impl Request {
}
}

/// Add headers to the request this is called on. Use this
/// function to add headers to your requests.
pub fn with_headers<T, K, V>(mut self, headers: T) -> Request
where
T: IntoIterator<Item = (K, V)>,
K: Into<String>,
V: Into<String>,
{
let headers = headers.into_iter().map(|(k, v)| (k.into(), v.into()));
self.headers.extend(headers);
self
}

/// Adds a header to the request this is called on. Use this
/// function to add headers to your requests.
pub fn with_header<T: Into<String>, U: Into<String>>(mut self, key: T, value: U) -> Request {
Expand Down Expand Up @@ -514,8 +527,23 @@ pub fn patch<T: Into<URL>>(url: T) -> Request {

#[cfg(test)]
mod parsing_tests {

use std::collections::HashMap;

use super::{get, ParsedRequest};

#[test]
fn test_headers() {
let headers = HashMap::from([
(format!("foo"), format!("bar")),
(format!("foo"), format!("baz")),
]);

let req = get("http://www.example.org/test/res").with_headers(headers.clone());

assert_eq!(req.headers, headers);
}

#[test]
fn test_multiple_params() {
let req = get("http://www.example.org/test/res")
Expand Down
Loading