Skip to content

Commit 4f6edbd

Browse files
authored
Merge pull request #1 from pachecoio/feature/docs
Included cargo docs
2 parents 5674a76 + dbf2e03 commit 4f6edbd

File tree

5 files changed

+134
-5
lines changed

5 files changed

+134
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "openapi-mocker"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
edition = "2021"
55
repository = "https://github.com/pachecoio/openapi-mocker"
66
keywords = ["openapi", "mock", "mock-server"]

src/lib.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,39 @@
1-
use std::path::PathBuf;
2-
1+
//! # OpenAPI Mock Server
2+
//!
3+
//! `openapi-mocker` is a simple mock server for OpenAPI 3.0 specs.
4+
//! It can be used to quickly create a mock server for an OpenAPI spec.
5+
//!
6+
//! The server will respond with example responses defined in the spec.
7+
//! If no example is defined, it will respond with an empty JSON object.
8+
//! The server will respond with a 200 status code by default, but you can
9+
//! specify a different status code in the URL.
10+
//!
11+
//! ## Usage
12+
//! ```sh
13+
//! openapi-mocker <spec> [port]
14+
//! ```
15+
//! * `<spec>` - Path to the OpenAPI spec file
16+
//! * `[port]` - Port to bind the server to (default: 8080)
17+
//!
18+
//! ## Example
19+
//! ```sh
20+
//! openapi-mocker tests/testdata/petstore.yaml
21+
//! ```
22+
//! This will start a server on port 8080 with the Petstore spec.
23+
//! You can then make requests to the server to get example responses.
24+
//! For example, to get a list of pets:
25+
//! ```sh
26+
//! curl http://localhost:8080/200/pets
27+
//! ```
28+
//! This will return a list of pets from the example response in the spec.
29+
//! You can also specify a different status code in the URL:
30+
//! ```sh
31+
//! curl http://localhost:8080/404/pets
32+
//! ```
33+
//! This will return a 404 status code with the example response for a 404 error.
34+
//!
335
use clap::Parser;
4-
36+
use std::path::PathBuf;
537
pub mod server;
638
pub mod spec;
739

src/server.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ use actix_web::{
55

66
use crate::spec::{load_endpoint, load_example, load_response, Method};
77

8+
/// Application state for the Actix Web server.
89
pub struct AppState {
910
pub spec: oas3::OpenApiV3Spec,
1011
}
1112

13+
/// Returns a new Actix Web scope with all the routes for the server.
1214
pub fn get_scope() -> Scope {
1315
web::scope("")
1416
.route("/{status}/{tail:.*}", get().to(handle_all))

src/spec.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use oas3::spec::{Operation, PathItem, Response};
22

33
pub type SpecResult<T> = Result<T, Box<dyn std::error::Error>>;
44

5+
/// HTTP methods
56
pub enum Method {
67
Get,
78
Post,
@@ -29,10 +30,43 @@ impl From<&str> for Method {
2930
}
3031
}
3132

33+
/// Load an OpenAPI spec from a file
34+
///
35+
/// # Arguments
36+
/// * `path` - Path to the OpenAPI spec file
37+
///
38+
/// # Returns
39+
/// An OpenAPI spec object
40+
///
41+
/// # Example
42+
/// ```
43+
/// use openapi_mocker::spec::load_spec;
44+
///
45+
/// let spec = load_spec("tests/testdata/petstore.yaml");
46+
/// assert_eq!(spec.openapi, "3.0.0");
47+
/// ```
3248
pub fn load_spec(path: &str) -> oas3::OpenApiV3Spec {
3349
oas3::from_path(path).unwrap()
3450
}
3551

52+
/// Load an endpoint from an OpenAPI spec
53+
///
54+
/// # Arguments
55+
/// * `spec` - OpenAPI spec object
56+
/// * `path` - Path to the endpoint
57+
/// * `method` - HTTP method
58+
///
59+
/// # Returns
60+
/// An OpenAPI operation object
61+
///
62+
/// # Example
63+
/// ```
64+
/// use openapi_mocker::spec::{load_spec, load_endpoint, Method};
65+
///
66+
/// let spec = load_spec("tests/testdata/petstore.yaml");
67+
/// let op = load_endpoint(&spec, "/pets", Method::Get).unwrap();
68+
/// assert_eq!(op.operation_id, Some("listPets".to_string()));
69+
/// ```
3670
pub fn load_endpoint(
3771
spec: &oas3::OpenApiV3Spec,
3872
path: &str,
@@ -46,6 +80,14 @@ pub fn load_endpoint(
4680
Ok(op.clone())
4781
}
4882

83+
/// Load a method from a PathItem
84+
///
85+
/// # Arguments
86+
/// * `method` - HTTP method
87+
/// * `path_item` - PathItem object
88+
///
89+
/// # Returns
90+
/// An Option with the Operation object
4991
fn load_method<'a>(method: Method) -> impl Fn(&PathItem) -> Option<&Operation> + 'a {
5092
move |path_item: &PathItem| match method {
5193
Method::Get => path_item.get.as_ref(),
@@ -59,6 +101,25 @@ fn load_method<'a>(method: Method) -> impl Fn(&PathItem) -> Option<&Operation> +
59101
}
60102
}
61103

104+
/// Load a response from an OpenAPI operation
105+
///
106+
/// # Arguments
107+
/// * `spec` - OpenAPI spec object
108+
/// * `op` - OpenAPI operation object
109+
/// * `status` - HTTP status code
110+
///
111+
/// # Returns
112+
/// An OpenAPI response object
113+
///
114+
/// # Example
115+
/// ```
116+
/// use openapi_mocker::spec::{load_spec, load_endpoint, load_response, Method};
117+
///
118+
/// let spec = load_spec("tests/testdata/petstore.yaml");
119+
/// let op = load_endpoint(&spec, "/pets", Method::Get).unwrap();
120+
/// let response = load_response(&spec, &op, 200).unwrap();
121+
/// assert_eq!(response.description, Some("A paged array of pets".to_string()));
122+
/// ```
62123
pub fn load_response(
63124
spec: &oas3::OpenApiV3Spec,
64125
op: &Operation,
@@ -73,6 +134,40 @@ pub fn load_response(
73134
}
74135
}
75136

137+
/// Load an example from an OpenAPI response
138+
///
139+
/// # Arguments
140+
/// * `spec` - OpenAPI spec object
141+
/// * `response` - OpenAPI response object
142+
/// * `content_type` - Content type
143+
///
144+
/// # Returns
145+
/// A JSON value with the example
146+
///
147+
/// # Example
148+
/// ```
149+
/// use openapi_mocker::spec::{load_spec, load_endpoint, load_response, load_example, Method};
150+
/// use serde_json::json;
151+
///
152+
/// let spec = load_spec("tests/testdata/petstore.yaml");
153+
/// let op = load_endpoint(&spec, "/pets", Method::Get).unwrap();
154+
/// let response = load_response(&spec, &op, 200).unwrap();
155+
/// let content_type = "application/json";
156+
/// let example = load_example(&spec, &response, content_type).unwrap();
157+
/// let expected = json!([
158+
/// {
159+
/// "id": 1,
160+
/// "name": "doggie",
161+
/// "tag": "dog"
162+
/// },
163+
/// {
164+
/// "id": 2,
165+
/// "name": "kitty",
166+
/// "tag": "cat"
167+
/// }
168+
/// ]);
169+
/// assert_eq!(example, expected);
170+
/// ```
76171
pub fn load_example(
77172
spec: &oas3::OpenApiV3Spec,
78173
response: &Response,

0 commit comments

Comments
 (0)