-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add http body to the request data structure
implements #5 done: - [x] add `HttpVersion` to `HttpRequest` - [x] implement http template file body parsing + test todos: - [ ] implement http body to curl parameter mapping - [ ] implement external file enum behavior - [ ] implement templating on body content Signed-off-by: Sven Kanoldt <[email protected]>
- Loading branch information
Showing
11 changed files
with
191 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use std::path::PathBuf; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] | ||
pub enum HttpBody { | ||
InlineText(String), | ||
InlineBinary(Vec<u8>), | ||
Extern(PathBuf), | ||
None, | ||
} | ||
|
||
impl Default for HttpBody { | ||
fn default() -> Self { | ||
Self::None | ||
} | ||
} | ||
|
||
impl HttpBody { | ||
pub fn contents(&self) -> std::io::Result<Option<String>> { | ||
Ok(match self { | ||
HttpBody::InlineText(c) => Some(c.to_owned()), | ||
HttpBody::InlineBinary(_) => todo!("Binary data cannot be represented as string yet"), | ||
HttpBody::Extern(f) => Some(std::fs::read_to_string(f)?), | ||
HttpBody::None => None, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,34 @@ | ||
use assert_cmd::assert::Assert; | ||
use assert_cmd::prelude::*; | ||
use dotenvy::dotenv; | ||
use curlz::data::HttpMethod; | ||
use predicates::prelude::*; | ||
use std::process::Command; | ||
use wiremock::matchers::{method, path}; | ||
use wiremock::{Mock, MockServer, ResponseTemplate}; | ||
|
||
fn binary() -> Result<Command, assert_cmd::cargo::CargoError> { | ||
Command::cargo_bin(env!("CARGO_PKG_NAME")) | ||
} | ||
use crate::testlib::{binary, CurlzTestSuite}; | ||
|
||
mod testlib; | ||
|
||
#[test] | ||
fn should_show_usage_when_no_args_passed() { | ||
binary() | ||
.unwrap() | ||
.assert() | ||
.failure() | ||
.stderr(predicate::str::contains("USAGE:")); | ||
} | ||
|
||
#[tokio::test] | ||
async fn should_request_a_url() { | ||
CurlzTest::new() | ||
async fn should_send_as_get() { | ||
CurlzTestSuite::new() | ||
.await | ||
.with_url("/gitignore/templates/Rust") | ||
.request() | ||
.send_to_path("/gitignore/templates/Rust") | ||
.issue_request() | ||
.await; | ||
} | ||
|
||
struct CurlzTest { | ||
mock_server: MockServer, | ||
url_part: String, | ||
payload: String, | ||
} | ||
|
||
impl CurlzTest { | ||
pub async fn new() -> Self { | ||
dotenv().ok(); | ||
Self { | ||
mock_server: MockServer::start().await, | ||
url_part: "".to_string(), | ||
payload: "# curlz rocks".to_string(), | ||
} | ||
} | ||
|
||
/// sets the target url to be requested | ||
pub fn with_url(mut self, url_part: &str) -> Self { | ||
self.url_part = url_part.to_string(); | ||
self | ||
} | ||
|
||
/// runs curlz and requests the given url | ||
pub async fn request(self) -> Assert { | ||
Mock::given(method("GET")) | ||
.and(path(self.url_part.as_str())) | ||
.respond_with(ResponseTemplate::new(200).set_body_string(&self.payload)) | ||
.mount(&self.mock_server) | ||
.await; | ||
|
||
let url = format!("{}{}", &self.mock_server.uri(), self.url_part); | ||
binary() | ||
.unwrap() | ||
.args(["r", url.as_str()]) | ||
.assert() | ||
.success() | ||
.stdout(predicate::str::contains(&self.payload)) | ||
} | ||
#[tokio::test] | ||
async fn should_send_payload_as_post() { | ||
CurlzTestSuite::new() | ||
.await | ||
.send_to_path("/anything") | ||
.send_with_method(HttpMethod::Post) | ||
.issue_request() | ||
.await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
### this is a POST request with a body | ||
POST https://httpbin.org/anything | ||
Accept: application/json | ||
Content-Type: application/json | ||
|
||
{ | ||
"foo": "Bar", | ||
"bool": true | ||
} |
Oops, something went wrong.