Skip to content

Commit

Permalink
WIP:Fix hyper usage
Browse files Browse the repository at this point in the history
  • Loading branch information
alexliesenfeld committed Feb 29, 2024
1 parent 82d4603 commit f5756a0
Show file tree
Hide file tree
Showing 10 changed files with 459 additions and 395 deletions.
23 changes: 18 additions & 5 deletions src/api/adapter/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ 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, 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::server::MockServerState;

pub struct LocalMockServerAdapter {
Expand Down Expand Up @@ -115,16 +122,22 @@ impl MockServerAdapter for LocalMockServerAdapter {
Ok(())
}

async fn create_proxy_matcher(&self, req: RequestRequirements) -> Result<ProxyMatcherRef, String> {
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);
delete_all_proxy_matchers(&self.local_state);
Ok(())
}

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

Expand Down
15 changes: 12 additions & 3 deletions src/api/adapter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ 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, ProxyMatcherRef, RecordingMatcherRef,
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 +80,15 @@ 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 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 create_record_matcher(
&self,
req: RequestRequirements,
) -> Result<(RecordingMatcherRef), String>;
async fn delete_all_record_matchers(&self) -> Result<(), String>;
async fn reset(&self) -> Result<(), String>;
}
1 change: 0 additions & 1 deletion src/api/adapter/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ impl MockServerAdapter for RemoteMockServerAdapter {
async fn ping(&self) -> Result<(), String> {
http_ping(&self.addr, self.http_client.borrow()).await
}

}

async fn http_ping(
Expand Down
33 changes: 23 additions & 10 deletions src/api/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::api::mock::{ProxyMatcher, RecordingMatcher};
use crate::api::spec::{Then, When};
#[cfg(feature = "remote")]
use crate::api::RemoteMockServerAdapter;
Expand All @@ -14,7 +15,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 @@ -328,15 +328,22 @@ impl MockServer {
}
}


pub fn proxy<F>(&self, host: F) -> ProxyMatcher where F: FnOnce(When) {
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) {
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() });
spec_fn(When {
expectations: req.clone(),
});

let response = self
.server_adapter
Expand All @@ -352,15 +359,22 @@ impl MockServer {
}
}


pub fn record<F>(&self, host: F) -> RecordingMatcher where F: FnOnce(When) {
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){
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() });
spec_fn(When {
expectations: req.clone(),
});

let response = self
.server_adapter
Expand All @@ -375,7 +389,6 @@ impl MockServer {
server: self,
}
}

}

impl Drop for MockServer {
Expand Down
1 change: 0 additions & 1 deletion src/common/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@ impl RecordingMatcherRef {
}
}


#[derive(Serialize, Deserialize, Clone)]
pub struct ActiveProxyMatcher {
pub id: usize,
Expand Down
Loading

0 comments on commit f5756a0

Please sign in to comment.