Skip to content

Commit

Permalink
feat(http-language): body variable substitution #21
Browse files Browse the repository at this point in the history
- introduce echo server behaviour to the testlib, to verify the variable substitution on the body

Signed-off-by: Sven Kanoldt <[email protected]>
  • Loading branch information
sassman committed Jan 11, 2023
1 parent a89d9c2 commit 4e930dd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions tests/basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async fn should_send_as_get() {
async fn should_send_as_post() {
CurlzTestSuite::new()
.await
.with_path("/anything")
.with_path("/post")
.with_method(HttpMethod::Post)
.send_request()
.await;
Expand All @@ -37,7 +37,7 @@ async fn should_send_as_post() {
async fn should_send_text_as_put() {
CurlzTestSuite::new()
.await
.with_path("/anything")
.with_path("/put")
.with_method(HttpMethod::Put)
.with_payload(HttpBody::InlineText("Howdy Pal!".to_string()))
.send_request()
Expand Down
20 changes: 12 additions & 8 deletions tests/testlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use assert_cmd::assert::Assert;
use dotenvy::dotenv;
use std::process::Command;
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
use wiremock::{Mock, MockServer, Request, Respond, ResponseTemplate};

use curlz::data::{HttpBody, HttpMethod};

Expand Down Expand Up @@ -46,11 +46,10 @@ impl CurlzTestSuite {
/// sets the http body payload that is send
pub fn with_payload(mut self, body: HttpBody) -> Self {
self.payload = body;

self
}

/// runs curlz and requests the given url
/// runs curlz and requests the given url from a local echo http server
pub async fn send_request(&mut self) -> Assert {
self.prepare_mock_server().await;

Expand Down Expand Up @@ -78,12 +77,17 @@ impl CurlzTestSuite {
async fn prepare_mock_server(&mut self) {
Mock::given(method(self.http_method.as_str()))
.and(path(self.url_part.as_str()))
.respond_with(
ResponseTemplate::new(200)
// todo: this should be out commented when `--data` is eventually submitted
.set_body_bytes(self.payload.as_bytes().unwrap()),
)
.respond_with(EchoResponder::default())
.mount(&self.mock_server)
.await;
}
}

#[derive(Default)]
struct EchoResponder;

impl Respond for EchoResponder {
fn respond(&self, request: &Request) -> ResponseTemplate {
ResponseTemplate::new(200).set_body_bytes(request.body.as_slice())
}
}

0 comments on commit 4e930dd

Please sign in to comment.