Skip to content

Commit 341b940

Browse files
Merge pull request #3 from mocks-rs/0.3.3
0.3.3
2 parents af62629 + c6238f7 commit 341b940

22 files changed

+295
-141
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 = "mocks"
3-
version = "0.3.2"
3+
version = "0.3.3"
44
edition = "2021"
55
authors = ["codemountains <codemountains@gmail.com>"]
66
description = "Get a mock REST APIs with zero coding within seconds."

runn-e2e/runbooks/test.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@ steps:
1616
post:
1717
include:
1818
path: test_post.yml
19+
postError:
20+
include:
21+
path: test_post_error.yml
1922
comment:
2023
include:
2124
path: test_comment.yml
25+
commentError:
26+
include:
27+
path: test_comment_error.yml
2228
profile:
2329
include:
2430
path: test_profile.yml
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
desc: "comment endpoint error tests"
2+
runners:
3+
req:
4+
endpoint: http://localhost:3000
5+
vars:
6+
duplicateCommentId: 1
7+
if: included
8+
steps:
9+
postCommentDuplicateIdError:
10+
desc: "Failed due to duplicate ID"
11+
req:
12+
/comments:
13+
post:
14+
body:
15+
application/json:
16+
id: "{{ vars.duplicateCommentId }}"
17+
text: "new comment"
18+
post_id: "{{ vars.postId }}"
19+
test: |
20+
// Status code must be 409.
21+
current.res.status == 409
22+
getCommentNotFoundError:
23+
desc: "Post is not found"
24+
req:
25+
/comments/999999:
26+
get:
27+
body:
28+
application/json: null
29+
test: |
30+
// Status code must be 404.
31+
current.res.status == 404

runn-e2e/runbooks/test_post_error.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
desc: "post endpoint error tests"
2+
runners:
3+
req:
4+
endpoint: http://localhost:3000
5+
vars:
6+
duplicatePostId: "01J7BAKH37HPG116ZRRFKHBDGB"
7+
if: included
8+
steps:
9+
postPostDuplicateIdError:
10+
desc: "Failed due to duplicate ID"
11+
req:
12+
/posts:
13+
post:
14+
body:
15+
application/json:
16+
id: "{{ vars.duplicatePostId }}"
17+
title: "new post"
18+
views: 0
19+
test: |
20+
// Status code must be 409.
21+
current.res.status == 409
22+
getPostNotFoundError:
23+
desc: "Comment is not found"
24+
req:
25+
/posts/notfound:
26+
get:
27+
body:
28+
application/json: null
29+
test: |
30+
// Status code must be 404.
31+
current.res.status == 404

src/error.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::fmt;
66

77
pub const EXCEPTION_ERROR_MESSAGE: &str = "An unexpected error occurred.";
88

9-
#[derive(Debug)]
9+
#[derive(Debug, Eq, PartialEq)]
1010
pub enum MocksError {
1111
FailedReadFile(String),
1212
FailedWriteFile(String),
@@ -16,6 +16,7 @@ pub enum MocksError {
1616
ObjectNotFound,
1717
MethodNotAllowed,
1818
InvalidRequest,
19+
DuplicateId,
1920
}
2021

2122
impl std::error::Error for MocksError {
@@ -35,6 +36,7 @@ impl fmt::Display for MocksError {
3536
Self::ObjectNotFound => write!(fmt, "Object not found."),
3637
Self::MethodNotAllowed => write!(fmt, "Method not allowed."),
3738
Self::InvalidRequest => write!(fmt, "Invalid request."),
39+
Self::DuplicateId => write!(fmt, "Duplicate ID."),
3840
}
3941
}
4042
}
@@ -50,6 +52,7 @@ impl IntoResponse for MocksError {
5052
}
5153
MocksError::MethodNotAllowed => (StatusCode::METHOD_NOT_ALLOWED, self.to_string()),
5254
MocksError::InvalidRequest => (StatusCode::BAD_REQUEST, self.to_string()),
55+
MocksError::DuplicateId => (StatusCode::CONFLICT, self.to_string()),
5356
};
5457

5558
(status, Json(json!({ "error": message }))).into_response()

src/server.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
mod context;
22
mod handler;
3-
mod hc;
43
mod state;
54

65
use crate::error::MocksError;
7-
use crate::server::handler::{delete, get_all, get_one, patch, patch_one, post, put, put_one};
8-
use crate::server::hc::hc;
6+
use crate::server::handler::delete::delete;
7+
use crate::server::handler::get::{get_all, get_one};
8+
use crate::server::handler::hc::hc;
9+
use crate::server::handler::patch::{patch, patch_one};
10+
use crate::server::handler::post::post;
11+
use crate::server::handler::put::{put, put_one};
912
use crate::server::state::{AppState, SharedState};
1013
use crate::storage::Storage;
1114
use axum::routing::get;

src/server/handler.rs

Lines changed: 11 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,19 @@
1-
use crate::error::MocksError;
2-
use crate::server::context::{Payload, PayloadWithId};
3-
use crate::server::state::SharedState;
4-
use axum::extract::{Path, State};
5-
use axum::http::StatusCode;
6-
use axum::response::IntoResponse;
7-
use axum::Json;
8-
use serde_json::json;
9-
10-
pub async fn get_all(
11-
Path(resource): Path<String>,
12-
state: State<SharedState>,
13-
) -> Result<impl IntoResponse, MocksError> {
14-
let state = state
15-
.lock()
16-
.map_err(|e| MocksError::Exception(e.to_string()))?;
17-
18-
let value = state
19-
.storage
20-
.get_all(&resource)
21-
.ok_or(MocksError::ResourceNotFound)?;
22-
let response = json!({
23-
resource: value
24-
});
25-
26-
Ok((StatusCode::OK, Json(response)))
27-
}
28-
29-
pub async fn get_one(
30-
Path((resource, id)): Path<(String, String)>,
31-
state: State<SharedState>,
32-
) -> Result<impl IntoResponse, MocksError> {
33-
let state = state
34-
.lock()
35-
.map_err(|e| MocksError::Exception(e.to_string()))?;
36-
37-
let value = state
38-
.storage
39-
.get_one(&resource, &id)
40-
.ok_or(MocksError::ObjectNotFound)?;
41-
Ok((StatusCode::OK, Json(value)))
42-
}
43-
44-
pub async fn post(
45-
Path(resource): Path<String>,
46-
state: State<SharedState>,
47-
PayloadWithId(input): PayloadWithId,
48-
) -> Result<impl IntoResponse, MocksError> {
49-
let mut state = state
50-
.lock()
51-
.map_err(|e| MocksError::Exception(e.to_string()))?;
52-
53-
let value = state.storage.insert(&resource, &input)?;
54-
Ok((StatusCode::CREATED, Json(value)))
55-
}
56-
57-
pub async fn put(
58-
Path((resource, id)): Path<(String, String)>,
59-
state: State<SharedState>,
60-
PayloadWithId(input): PayloadWithId,
61-
) -> Result<impl IntoResponse, MocksError> {
62-
let mut state = state
63-
.lock()
64-
.map_err(|e| MocksError::Exception(e.to_string()))?;
65-
66-
let value = state.storage.replace(&resource, &id, &input)?;
67-
Ok((StatusCode::OK, Json(value)))
68-
}
69-
70-
pub async fn put_one(
71-
Path(resource): Path<String>,
72-
state: State<SharedState>,
73-
PayloadWithId(input): PayloadWithId,
74-
) -> Result<impl IntoResponse, MocksError> {
75-
let mut state = state
76-
.lock()
77-
.map_err(|e| MocksError::Exception(e.to_string()))?;
78-
79-
let value = state.storage.replace_one(&resource, &input)?;
80-
Ok((StatusCode::OK, Json(value)))
81-
}
82-
83-
pub async fn patch(
84-
Path((resource, id)): Path<(String, String)>,
85-
state: State<SharedState>,
86-
Payload(input): Payload,
87-
) -> Result<impl IntoResponse, MocksError> {
88-
let mut state = state
89-
.lock()
90-
.map_err(|e| MocksError::Exception(e.to_string()))?;
91-
92-
let value = state.storage.update(&resource, &id, &input)?;
93-
Ok((StatusCode::OK, Json(value)))
94-
}
95-
96-
pub async fn patch_one(
97-
Path(resource): Path<String>,
98-
state: State<SharedState>,
99-
Payload(input): Payload,
100-
) -> Result<impl IntoResponse, MocksError> {
101-
let mut state = state
102-
.lock()
103-
.map_err(|e| MocksError::Exception(e.to_string()))?;
104-
105-
let value = state.storage.update_one(&resource, &input)?;
106-
Ok((StatusCode::OK, Json(value)))
107-
}
108-
109-
pub async fn delete(
110-
Path((resource, id)): Path<(String, String)>,
111-
state: State<SharedState>,
112-
) -> Result<impl IntoResponse, MocksError> {
113-
let mut state = state
114-
.lock()
115-
.map_err(|e| MocksError::Exception(e.to_string()))?;
116-
117-
let value = state.storage.delete(&resource, &id)?;
118-
Ok((StatusCode::OK, Json(value)))
119-
}
1+
pub mod delete;
2+
pub mod get;
3+
pub mod hc;
4+
pub mod patch;
5+
pub mod post;
6+
pub mod put;
1207

1218
#[cfg(test)]
1229
mod tests {
123-
use super::*;
10+
use super::super::*;
11+
use crate::server::context::{Payload, PayloadWithId};
12412
use crate::server::state::AppState;
13+
use crate::server::state::SharedState;
12514
use crate::storage::Storage;
15+
use axum::extract::{Path, State};
16+
use serde_json::json;
12617

12718
fn init_state() -> SharedState {
12819
let storage = Storage::new("storage.json", false)

src/server/handler/delete.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use crate::error::MocksError;
2+
use crate::server::state::SharedState;
3+
use axum::extract::{Path, State};
4+
use axum::http::StatusCode;
5+
use axum::response::IntoResponse;
6+
use axum::Json;
7+
8+
pub async fn delete(
9+
Path((resource, id)): Path<(String, String)>,
10+
state: State<SharedState>,
11+
) -> Result<impl IntoResponse, MocksError> {
12+
let mut state = state
13+
.lock()
14+
.map_err(|e| MocksError::Exception(e.to_string()))?;
15+
16+
let value = state.storage.delete(&resource, &id)?;
17+
Ok((StatusCode::OK, Json(value)))
18+
}

src/server/handler/get.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use crate::error::MocksError;
2+
use crate::server::state::SharedState;
3+
use axum::extract::{Path, State};
4+
use axum::http::StatusCode;
5+
use axum::response::IntoResponse;
6+
use axum::Json;
7+
use serde_json::json;
8+
9+
pub async fn get_all(
10+
Path(resource): Path<String>,
11+
state: State<SharedState>,
12+
) -> Result<impl IntoResponse, MocksError> {
13+
let state = state
14+
.lock()
15+
.map_err(|e| MocksError::Exception(e.to_string()))?;
16+
17+
let value = state
18+
.storage
19+
.get_all(&resource)
20+
.ok_or(MocksError::ResourceNotFound)?;
21+
let response = json!({
22+
resource: value
23+
});
24+
25+
Ok((StatusCode::OK, Json(response)))
26+
}
27+
28+
pub async fn get_one(
29+
Path((resource, id)): Path<(String, String)>,
30+
state: State<SharedState>,
31+
) -> Result<impl IntoResponse, MocksError> {
32+
let state = state
33+
.lock()
34+
.map_err(|e| MocksError::Exception(e.to_string()))?;
35+
36+
let value = state
37+
.storage
38+
.get_one(&resource, &id)
39+
.ok_or(MocksError::ObjectNotFound)?;
40+
Ok((StatusCode::OK, Json(value)))
41+
}
File renamed without changes.

src/server/handler/patch.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use crate::error::MocksError;
2+
use crate::server::context::Payload;
3+
use crate::server::state::SharedState;
4+
use axum::extract::{Path, State};
5+
use axum::http::StatusCode;
6+
use axum::response::IntoResponse;
7+
use axum::Json;
8+
9+
pub async fn patch(
10+
Path((resource, id)): Path<(String, String)>,
11+
state: State<SharedState>,
12+
Payload(input): Payload,
13+
) -> Result<impl IntoResponse, MocksError> {
14+
let mut state = state
15+
.lock()
16+
.map_err(|e| MocksError::Exception(e.to_string()))?;
17+
18+
let value = state.storage.update(&resource, &id, &input)?;
19+
Ok((StatusCode::OK, Json(value)))
20+
}
21+
22+
pub async fn patch_one(
23+
Path(resource): Path<String>,
24+
state: State<SharedState>,
25+
Payload(input): Payload,
26+
) -> Result<impl IntoResponse, MocksError> {
27+
let mut state = state
28+
.lock()
29+
.map_err(|e| MocksError::Exception(e.to_string()))?;
30+
31+
let value = state.storage.update_one(&resource, &input)?;
32+
Ok((StatusCode::OK, Json(value)))
33+
}

0 commit comments

Comments
 (0)