aboutsummaryrefslogtreecommitdiff
path: root/src/http.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/http.rs')
-rw-r--r--src/http.rs46
1 files changed, 37 insertions, 9 deletions
diff --git a/src/http.rs b/src/http.rs
index db5e3a6..4ea9b96 100644
--- a/src/http.rs
+++ b/src/http.rs
@@ -1,9 +1,9 @@
-use serde_with::DisplayFromStr;
+use crate::error::{AppError, AppResult};
use http::{HeaderMap, HeaderName, HeaderValue, Method};
use reqwest::{Client, Request, Url};
use serde::{Deserialize, Serialize};
+use serde_with::DisplayFromStr;
use serde_with::serde_as;
-use crate::error::{AppError, AppResult};
#[serde_as]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
@@ -18,9 +18,12 @@ pub struct RequestConfig {
}
impl RequestConfig {
-
pub fn new(method: Method, url: Url, headers: Vec<(String, String)>) -> Self {
- Self { method, url, headers }
+ Self {
+ method,
+ url,
+ headers,
+ }
}
pub fn get(url: Url, headers: Vec<(String, String)>) -> Self {
@@ -32,6 +35,8 @@ impl RequestConfig {
if !self.headers.is_empty() {
builder = builder.headers(self.header_map()?);
}
+ builder = builder.body("OHAI");
+
Ok(builder.build()?)
}
@@ -43,25 +48,30 @@ impl RequestConfig {
let mut header_map = HeaderMap::new();
for (name, value) in &self.headers {
let name: HeaderName = name.try_into().map_err(|e| {
- AppError::InvalidHttpHeader(format!("[{}] is not a valid HTTP header name ({})", name, e))
+ AppError::InvalidHttpHeader(format!(
+ "[{}] is not a valid HTTP header name ({})",
+ name, e
+ ))
})?;
let value: HeaderValue = value.parse().map_err(|e| {
- AppError::InvalidHttpHeader(format!("[{}] is not a valid HTTP header value ({})", value, e))
+ AppError::InvalidHttpHeader(format!(
+ "[{}] is not a valid HTTP header value ({})",
+ value, e
+ ))
})?;
header_map.insert(name, value);
}
Ok(header_map)
}
-
}
#[cfg(test)]
mod tests {
+ use super::*;
use crate::AppError;
use crate::{assert_error, assert_invalid_http_header, test};
use reqwest::ClientBuilder;
- use super::*;
test! {
fn simple_get_request() {
@@ -73,6 +83,25 @@ mod tests {
}
test! {
+ fn get_with_arbitrary_header() {
+ let url = Url::parse("https://example.com")?;
+ let actual = build_get_request(&url, vec![("X-Arbitrary".to_string(), "X-Arbitrary-Value".to_string())])?;
+ assert_eq!(actual.headers().get("X-Arbitrary").unwrap().to_str()?, "X-Arbitrary-Value");
+ }
+ }
+
+ test! {
+ fn post_request_with_text_body() {
+ let url = Url::parse("https://example.com")?;
+ let config = RequestConfig::new(Method::POST, url, vec![]);
+ let client = ClientBuilder::new().build()?;
+ let request = config.build(&client)?;
+
+ assert_eq!(request.method(), Method::POST);
+ }
+ }
+
+ test! {
fn invalid_header_name() {
let url = Url::parse("https://example.com")?;
let invalid_headers = vec![("Content\x00Type".to_string(), "application/json".to_string())];
@@ -93,5 +122,4 @@ mod tests {
let client = ClientBuilder::new().build()?;
config.build(&client)
}
-
}