Skip to content

Commit

Permalink
Revert "Fix hyper usage"
Browse files Browse the repository at this point in the history
This reverts commit 213b4e2.
  • Loading branch information
alexliesenfeld committed Feb 29, 2024
1 parent 213b4e2 commit 167034b
Show file tree
Hide file tree
Showing 14 changed files with 339 additions and 832 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Expand Up @@ -28,7 +28,7 @@ similar = "2.4"
levenshtein = "1.0"
form_urlencoded = "1.2"

hyper = { version = "1.1", features = ["server", "http1", "client"] }
hyper = { version = "1.1", features = ["server", "http1"] }
hyper-util = { version = "0.1", features = ["tokio"] }
http-body-util = "0.1"
tokio = { version = "1.35", features = ["sync", "macros", "rt-multi-thread", "signal"] }
Expand All @@ -55,12 +55,11 @@ syn = { version = "2.0", features = ["full"] }
reqwest = "0.11"

[features]
default = ["cookies", "proxy"]
default = ["cookies"]
standalone = ["clap", "env_logger", "serde_yaml", "remote"]
color = ["colored"]
cookies = ["basic-cookies"]
remote = ["isahc"]
proxy = ["hyper-util/client"]

[[bin]]
name = "httpmock"
Expand Down
29 changes: 4 additions & 25 deletions src/api/adapter/local.rs
Expand Up @@ -12,8 +12,10 @@ use async_std::prelude::*;

use crate::api::adapter::MockServerAdapter;

use crate::common::data::{ActiveMock, ClosestMatch, MockDefinition, MockRef, ProxyMatcherRef, RecordingMatcherRef, RequestRequirements};
use crate::server::web::handlers::{add_new_mock, add_proxy_matcher, add_recording_matcher, delete_all_mocks, delete_all_proxy_matchers, delete_all_recording_matchers, delete_history, delete_one_mock, read_one_mock, reset, verify};
use crate::common::data::{ActiveMock, ClosestMatch, MockDefinition, MockRef, RequestRequirements};
use crate::server::web::handlers::{
add_new_mock, delete_all_mocks, delete_history, delete_one_mock, read_one_mock, verify,
};
use crate::server::MockServerState;

pub struct LocalMockServerAdapter {
Expand Down Expand Up @@ -109,27 +111,4 @@ impl MockServerAdapter for LocalMockServerAdapter {

Ok(())
}

async fn reset(&self) -> Result<(), String> {
reset(&self.local_state);
Ok(())
}

async fn create_proxy_matcher(&self, req: RequestRequirements) -> Result<ProxyMatcherRef, String> {
add_proxy_matcher(&self.local_state, req)
}

async fn delete_all_proxy_matchers(&self) -> Result<(), String> {
delete_all_proxy_matchers(&self.local_state);
Ok(())
}

async fn create_record_matcher(&self, req: RequestRequirements) -> Result<RecordingMatcherRef, String> {
add_recording_matcher(&self.local_state, req)
}

async fn delete_all_record_matchers(&self) -> Result<(), String> {
delete_all_recording_matchers(&self.local_state);
Ok(())
}
}
7 changes: 1 addition & 6 deletions src/api/adapter/mod.rs
Expand Up @@ -7,7 +7,7 @@ use async_trait::async_trait;

use serde::{Deserialize, Serialize};

use crate::common::data::{ActiveMock, ClosestMatch, MockDefinition, MockRef, ProxyMatcherRef, RecordingMatcherRef, RequestRequirements};
use crate::common::data::{ActiveMock, ClosestMatch, MockDefinition, MockRef, RequestRequirements};
use crate::server::web::handlers::{
add_new_mock, delete_all_mocks, delete_history, delete_one_mock, read_one_mock, verify,
};
Expand Down Expand Up @@ -77,9 +77,4 @@ pub trait MockServerAdapter {
async fn verify(&self, rr: &RequestRequirements) -> Result<Option<ClosestMatch>, String>;
async fn delete_history(&self) -> Result<(), String>;
async fn ping(&self) -> Result<(), String>;
async fn create_proxy_matcher(&self, req: RequestRequirements) -> Result<(ProxyMatcherRef), String>;
async fn delete_all_proxy_matchers(&self) -> Result<(), String>;
async fn create_record_matcher(&self, req: RequestRequirements) -> Result<(RecordingMatcherRef), String>;
async fn delete_all_record_matchers(&self) -> Result<(), String>;
async fn reset(&self) -> Result<(), String>;
}
9 changes: 0 additions & 9 deletions src/api/adapter/standalone.rs
Expand Up @@ -239,18 +239,9 @@ impl MockServerAdapter for RemoteMockServerAdapter {
Ok(())
}

async fn proxy(&self, req: RequestRequirements) -> Result<(), String> {
todo!()
}

async fn record(&self, req: RequestRequirements) -> Result<(), String> {
todo!()
}

async fn ping(&self) -> Result<(), String> {
http_ping(&self.addr, self.http_client.borrow()).await
}

}

async fn http_ping(
Expand Down
10 changes: 0 additions & 10 deletions src/api/mock.rs
Expand Up @@ -442,16 +442,6 @@ impl<'a> MockExt<'a> for Mock<'a> {
}
}

pub struct ProxyMatcher<'a> {
pub id: usize,
pub(crate) server: &'a MockServer,
}

pub struct RecordingMatcher<'a> {
pub id: usize,
pub(crate) server: &'a MockServer,
}

fn create_reason_output(reason: &Reason) -> String {
let mut output = String::new();
let offsets = match reason.best_match {
Expand Down
50 changes: 0 additions & 50 deletions src/api/server.rs
Expand Up @@ -14,7 +14,6 @@ use std::rc::Rc;
use std::sync::Arc;
use std::thread;
use tokio::task::LocalSet;
use crate::api::mock::{ProxyMatcher, RecordingMatcher};

/// A mock server that is able to receive and respond to HTTP requests.
pub struct MockServer {
Expand Down Expand Up @@ -327,55 +326,6 @@ impl MockServer {
.expect("Cannot reset mock server (task: delete request history).");
}
}


pub fn proxy<F>(&self, host: F) -> ProxyMatcher where F: FnOnce(When) {
self.proxy_async(host).join()
}

pub async fn proxy_async<F>(&self, spec_fn: F) -> ProxyMatcher where F: FnOnce(When) {
let mut req = Rc::new(Cell::new(RequestRequirements::new()));

spec_fn(When { expectations: req.clone() });

let response = self
.server_adapter
.as_ref()
.unwrap()
.create_proxy_matcher(req.take())
.await
.expect("Cannot deserialize mock server response");

ProxyMatcher {
id: response.id,
server: self,
}
}


pub fn record<F>(&self, host: F) -> RecordingMatcher where F: FnOnce(When) {
self.record_async(host).join()
}

pub async fn record_async<F>(&self, spec_fn: F) -> RecordingMatcher where F: FnOnce(When){
let mut req = Rc::new(Cell::new(RequestRequirements::new()));

spec_fn(When { expectations: req.clone() });

let response = self
.server_adapter
.as_ref()
.unwrap()
.create_record_matcher(req.take())
.await
.expect("Cannot deserialize mock server response");

RecordingMatcher {
id: response.id,
server: self,
}
}

}

impl Drop for MockServer {
Expand Down
14 changes: 0 additions & 14 deletions src/api/spec.rs
Expand Up @@ -51,20 +51,6 @@ impl When {
self
}

/// Sets the host to match requests for.
/// * `host` - The host.
///
/// # Example
/// ```
/// // TODO
/// ```
pub fn host<S: Into<String>>(mut self, host: S) -> Self {
update_cell(&self.expectations, |e| {
e.host = Some(host.into());
});
self
}

/// Sets the expected HTTP method.
///
/// * `method` - The HTTP method (a [Method](enum.Method.html) or a `String`).
Expand Down
59 changes: 0 additions & 59 deletions src/common/data.rs
Expand Up @@ -173,7 +173,6 @@ pub type MockMatcherFunction = fn(&HttpMockRequest) -> bool;
/// A general abstraction of an HTTP request for all handlers.
#[derive(Serialize, Deserialize, Clone)]
pub struct RequestRequirements {
pub host: Option<String>,
pub path: Option<String>,
pub path_contains: Option<Vec<String>>,
pub path_matches: Option<Vec<Pattern>>,
Expand Down Expand Up @@ -205,7 +204,6 @@ impl Default for RequestRequirements {
impl RequestRequirements {
pub fn new() -> Self {
Self {
host: None,
path: None,
path_contains: None,
path_matches: None,
Expand Down Expand Up @@ -349,63 +347,6 @@ impl ActiveMock {
}
}

#[derive(Serialize, Deserialize)]
pub struct ProxyMatcherRef {
pub id: usize,
}

impl ProxyMatcherRef {
pub fn new(id: usize) -> Self {
Self { id }
}
}

#[derive(Serialize, Deserialize)]
pub struct RecordingMatcherRef {
pub id: usize,
}

impl RecordingMatcherRef {
pub fn new(id: usize) -> Self {
Self { id }
}
}


#[derive(Serialize, Deserialize, Clone)]
pub struct ActiveProxyMatcher {
pub id: usize,
pub call_counter: usize,
pub requirements: RequestRequirements,
}

impl ActiveProxyMatcher {
pub fn new(id: usize, requirements: RequestRequirements) -> Self {
ActiveProxyMatcher {
id,
requirements,
call_counter: 0,
}
}
}

#[derive(Serialize, Deserialize, Clone)]
pub struct ActiveRecordingMatcher {
pub id: usize,
pub call_counter: usize,
pub requirements: RequestRequirements,
}

impl ActiveRecordingMatcher {
pub fn new(id: usize, requirements: RequestRequirements) -> Self {
ActiveRecordingMatcher {
id,
requirements,
call_counter: 0,
}
}
}

#[derive(Serialize, Deserialize)]
pub struct ClosestMatch {
pub request: HttpMockRequest,
Expand Down

0 comments on commit 167034b

Please sign in to comment.