Skip to content

Commit

Permalink
feat(mock): return a request if no handler is installed
Browse files Browse the repository at this point in the history
Closes #89, #91
  • Loading branch information
loyd committed Jul 16, 2024
1 parent 034a125 commit 65829d3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- inserter: improve performance of time measurements by using `quanta`.
- inserter: improve performance if the time limit isn't used.
- derive: move to syn v2.
- mock: return a request if no handler is installed ([#89], [#91]).

### Fixed
- watch: support a new syntax.
Expand All @@ -37,8 +38,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[#103]: https://github.com/loyd/clickhouse.rs/issues/103
[#102]: https://github.com/loyd/clickhouse.rs/pull/102
[#83]: https://github.com/loyd/clickhouse.rs/pull/83
[#91]: https://github.com/loyd/clickhouse.rs/pull/91
[#90]: https://github.com/loyd/clickhouse.rs/pull/90
[#89]: https://github.com/loyd/clickhouse.rs/issues/89
[#83]: https://github.com/loyd/clickhouse.rs/pull/83

## [0.11.6] - 2023-09-27
### Fixed
Expand Down
31 changes: 24 additions & 7 deletions src/test/mock.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
collections::VecDeque,
convert::Infallible,
error::Error,
net::SocketAddr,
sync::{Arc, Mutex},
Expand All @@ -8,7 +9,7 @@ use std::{

use bytes::Bytes;
use http_body_util::{BodyExt as _, Full};
use hyper::{body::Incoming, server::conn, service, Request, Response};
use hyper::{body::Incoming, server::conn, service, Request, Response, StatusCode};
use hyper_util::rt::{TokioIo, TokioTimer};
use tokio::{net::TcpListener, task::AbortHandle};

Expand Down Expand Up @@ -134,11 +135,7 @@ async fn server(listener: TcpListener, shared: Arc<Mutex<Shared>>) {
);

if let Err(err) = serving.await {
break if let Some(source) = err.source() {
source.to_string().into()
} else {
err.into()
};
break err.into();
}
};

Expand All @@ -148,9 +145,29 @@ async fn server(listener: TcpListener, shared: Arc<Mutex<Shared>>) {
async fn handle(
request: Request<Incoming>,
shared: &Mutex<Shared>,
) -> Result<Response<Full<Bytes>>, Infallible> {
let response = do_handle(request, shared).await.unwrap_or_else(|err| {
let bytes = Bytes::from(err.to_string());

// Prevents further usage of the mock.
shared.lock().unwrap().error.get_or_insert(err);

Response::builder()
.status(StatusCode::BAD_GATEWAY)
.body(Full::new(bytes))
.unwrap()
});

Ok(response)
}

async fn do_handle(
request: Request<Incoming>,
shared: &Mutex<Shared>,
) -> Result<Response<Full<Bytes>>, Box<dyn Error + Send + Sync>> {
let Some(handler) = shared.lock().unwrap().handlers.pop_front() else {
return Err("no installed handler for an incoming request".into());
// TODO: provide better error, e.g. some part of parsed body.
return Err(format!("no installed handler for an incoming request: {request:?}").into());
};

let (parts, body) = request.into_parts();
Expand Down

0 comments on commit 65829d3

Please sign in to comment.