Skip to content

Commit 0bc6f30

Browse files
committed
Improve module structure, reorganize exports, add doc comments, update README.md
Signed-off-by: declark1 <[email protected]>
1 parent 70bae2a commit 0bc6f30

20 files changed

+89
-45
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
11
# mocktail
2-
3-
mocktail is a minimal framework for mocking HTTP and gRPC services in Rust with support for streaming.
4-
5-
<!-- [![Crates.io](https://img.shields.io/crates/v/mocktail)](https://crates.io/crates/mocktail)
2+
[![Crates.io](https://img.shields.io/crates/v/mocktail)](https://crates.io/crates/mocktail)
63
[![Documentation](https://docs.rs/mocktail/badge.svg)](https://docs.rs/mocktail)
7-
[![Crates.io](https://img.shields.io/crates/l/mocktail)](LICENSE) -->
4+
[![Crates.io](https://img.shields.io/crates/l/mocktail)](LICENSE)
5+
6+
mocktail is a **minimal** crate for mocking HTTP and gRPC servers in Rust with support for streaming.
87

98
# Table of contents
109
* [Features](#features)
11-
* [Usage](#usage)
10+
* [Getting Started](#getting-started)
1211
* [Examples](#examples)
1312

1413
# Features
1514
- Mocks HTTP and gRPC servers
1615
- Mocks defined in Rust using a simple, ergonomic API
1716
- Supports HTTP streaming
1817
- Supports gRPC unary, client-streaming, server-streaming, and bidirectional-streaming methods
19-
- Performs matching using built-in matchers or custom matchers
18+
- Match requests to mock responses using built-in matchers or custom matchers
2019

21-
# Usage
20+
# Getting Started
2221
1. Add `mocktail` to `Cargo.toml` as a development dependency:
2322
```toml
2423
[dev-dependencies]
25-
mocktail = { git = "https://github.com/IBM/mocktail.git", version = "0.2.0-alpha" }
24+
mocktail = { git = "https://github.com/IBM/mocktail.git", version = "0.2.1-alpha" }
2625
```
2726

28-
2. See [mocktail-tests](/mocktail-tests/) crate for usage examples.
27+
2. For now, see [examples](/mocktail-tests/tests/examples) in the `mocktail-tests` crate. Additional documentation coming soon.
2928

3029
# Examples
31-
See [mocktail-tests](/mocktail-tests/) crate.
30+
See [examples](/mocktail-tests/tests/examples) in the `mocktail-tests` crate.
31+
32+
# Related projects
33+
This crate takes inspiration from other great mocking libraries including:
34+
- [wiremock](https://github.com/wiremock/wiremock)
35+
- [wiremock-rs](https://github.com/LukeMathWalker/wiremock-rs)
36+
- [httpmock](https://github.com/alexliesenfeld/httpmock)

mocktail/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mocktail"
3-
version = "0.2.0-alpha"
3+
version = "0.2.1-alpha"
44
edition = "2021"
55
authors = ["Dan Clark"]
66
description = "HTTP & gRPC server mocking for Rust"

mocktail/src/body.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
//! Mock body
12
use std::{collections::vec_deque, convert::Infallible, pin::Pin, task::Poll};
23

34
use bytes::{Buf, Bytes};
45
use futures::Stream;
56
use http_body::Frame;
67

7-
use crate::{buf_list::BufList, ext::MessageExt};
8+
use crate::ext::MessageExt;
89

9-
/// A mock body.
10+
mod buf_list;
11+
use buf_list::BufList;
12+
13+
/// The body of a mock request or response.
1014
#[derive(Default, Debug, Clone)]
1115
pub struct Body {
1216
bufs: BufList,
File renamed without changes.

mocktail/src/ext.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Extension traits
12
use bytes::{BufMut, Bytes, BytesMut};
23
use http::status::InvalidStatusCode;
34
use prost::Message;

mocktail/src/headers.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/// A representation of HTTP headers.
1+
//! Headers
2+
/// Represents HTTP headers.
23
#[derive(Default, Debug, Clone, PartialEq)]
34
pub struct Headers(Vec<(HeaderName, HeaderValue)>);
45

@@ -145,6 +146,7 @@ impl From<Headers> for tonic::metadata::MetadataMap {
145146
}
146147
}
147148

149+
/// Represents a HTTP header name.
148150
#[derive(Default, Debug, Clone, PartialEq)]
149151
pub struct HeaderName(String);
150152

@@ -210,6 +212,7 @@ impl From<&http::HeaderName> for HeaderName {
210212
}
211213
}
212214

215+
/// Represents a HTTP header value.
213216
#[derive(Default, Debug, Clone, PartialEq)]
214217
pub struct HeaderValue(String);
215218

mocktail/src/lib.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
#![doc = include_str!("../README.md")]
2-
mod body;
3-
pub use body::Body;
2+
pub mod body;
43
mod headers;
5-
pub use headers::*;
6-
mod matchers;
7-
pub use matchers::*;
4+
pub use headers::{HeaderName, HeaderValue, Headers};
5+
pub mod matchers;
86
mod mock;
97
pub use mock::Mock;
8+
pub mod mock_builder;
109
mod mock_set;
1110
pub use mock_set::MockSet;
1211
mod request;
13-
pub use request::Request;
12+
pub use request::{Method, Request};
1413
mod response;
1514
pub use response::{Response, StatusCode};
16-
mod when;
17-
pub use when::When;
18-
mod then;
19-
pub use then::Then;
20-
mod server;
21-
pub use server::MockServer;
22-
mod buf_list;
23-
mod ext;
24-
mod service;
15+
pub mod server;
2516
pub mod prelude {
2617
pub use crate::{
27-
matchers::*, Body, HeaderName, HeaderValue, Headers, Mock, MockServer, MockSet, Request,
28-
Response, StatusCode, Then, When,
18+
body::Body,
19+
headers::{HeaderName, HeaderValue, Headers},
20+
matchers::*,
21+
mock::Mock,
22+
mock_set::MockSet,
23+
request::{Method, Request},
24+
response::{Response, StatusCode},
25+
server::MockServer,
2926
};
3027
}
28+
mod ext;
29+
mod service;
3130

31+
/// Represents errors that can occur while serving mocks.
3232
#[derive(thiserror::Error, Debug)]
3333
pub enum Error {
3434
#[error("invalid: {0}")]

mocktail/src/matchers.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Mock request matchers
12
use super::{body::Body, headers::Headers, request::Request};
23
use crate::request::Method;
34
use std::cmp::Ordering;

mocktail/src/mock.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
use crate::{Matcher, Request, Response, Then, When};
1+
//! Mock
2+
use crate::{
3+
matchers::Matcher,
4+
mock_builder::{Then, When},
5+
request::Request,
6+
response::Response,
7+
};
28

39
const DEFAULT_PRIORITY: u8 = 5;
410

mocktail/src/mock_builder.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//! Mock builder types
2+
mod then;
3+
pub use then::Then;
4+
mod when;
5+
pub use when::When;

mocktail/src/then.rs mocktail/src/mock_builder/then.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Then
12
use std::{cell::Cell, rc::Rc};
23

34
use bytes::Bytes;

mocktail/src/when.rs mocktail/src/mock_builder/when.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
//! When
12
use std::{cell::Cell, rc::Rc};
23

34
use bytes::Bytes;
45

56
use crate::{
6-
body::Body, headers::Headers, matchers, request::Method, HeaderName, HeaderValue, Matcher,
7+
body::Body, headers::HeaderName, headers::HeaderValue, headers::Headers, matchers,
8+
matchers::Matcher, request::Method,
79
};
810

911
/// A request match conditions builder.

mocktail/src/mock_set.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
use super::{request::Request, response::Response, Mock};
2-
use crate::{Then, When};
1+
//! Mock set
2+
use crate::{
3+
mock::Mock,
4+
mock_builder::{Then, When},
5+
request::Request,
6+
response::Response,
7+
};
38

49
/// A set of mocks.
510
#[derive(Default, Debug)]

mocktail/src/request.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use crate::{Body, Headers};
1+
//! Mock request
2+
use crate::{body::Body, headers::Headers};
23

3-
/// A representation of a HTTP request.
4+
/// Represents a HTTP request.
45
#[derive(Debug, Clone, PartialEq)]
56
pub struct Request {
67
pub method: Method,
@@ -63,7 +64,7 @@ impl Request {
6364
}
6465
}
6566

66-
/// A representation of an HTTP method.
67+
/// Represents a HTTP method.
6768
#[allow(clippy::upper_case_acronyms)]
6869
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
6970
pub enum Method {

mocktail/src/response.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
//! Mock response
12
use std::num::NonZeroU16;
23

34
use super::{body::Body, headers::Headers};
45
use crate::{ext::CodeExt, Error};
56

6-
/// A representation of a HTTP response.
7+
/// Represents a HTTP response.
78
#[derive(Debug, Clone, PartialEq)]
89
pub struct Response {
910
pub status: StatusCode,
@@ -73,7 +74,7 @@ impl Default for Response {
7374
}
7475
}
7576

76-
/// A representation of a HTTP status code.
77+
/// Represents a HTTP status code.
7778
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
7879
pub struct StatusCode(NonZeroU16);
7980

mocktail/src/server.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Mock server
12
use http_body::Body;
23
use hyper::{body::Incoming, service::Service};
34
use hyper_util::{
@@ -16,8 +17,11 @@ use tracing::{debug, error, info, warn};
1617
use url::Url;
1718

1819
use crate::{
20+
mock::Mock,
21+
mock_builder::{Then, When},
22+
mock_set::MockSet,
1923
service::{GrpcMockService, HttpMockService},
20-
Error, Mock, MockSet, Then, When,
24+
Error,
2125
};
2226

2327
/// A mock server.

mocktail/src/service.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Mock services
12
pub mod grpc;
23
pub use grpc::GrpcMockService;
34
pub mod http;

mocktail/src/service/grpc.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Mock gRPC service
12
use std::{
23
convert::Infallible,
34
sync::{Arc, RwLock},
@@ -14,8 +15,9 @@ use tokio_stream::wrappers::ReceiverStream;
1415
use tonic::body::BoxBody;
1516
use tracing::debug;
1617

17-
use crate::{Headers, MockSet, Request};
18+
use crate::{headers::Headers, mock_set::MockSet, request::Request};
1819

20+
/// Mock gRPC service.
1921
#[derive(Debug, Clone)]
2022
pub struct GrpcMockService {
2123
pub mocks: Arc<RwLock<MockSet>>,

mocktail/src/service/http.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Mock HTTP service
12
use std::{
23
convert::Infallible,
34
sync::{Arc, RwLock},
@@ -13,10 +14,11 @@ use tokio::sync::mpsc;
1314
use tokio_stream::wrappers::ReceiverStream;
1415
use tracing::debug;
1516

16-
use crate::{MockSet, Request};
17+
use crate::{mock_set::MockSet, request::Request};
1718

1819
type BoxBody = http_body_util::combinators::BoxBody<Bytes, hyper::Error>;
1920

21+
/// Mock HTTP service.
2022
#[derive(Debug, Clone)]
2123
pub struct HttpMockService {
2224
pub mocks: Arc<RwLock<MockSet>>,

0 commit comments

Comments
 (0)