Skip to content

Commit e93178a

Browse files
committed
KnownErrorToJson: Replace unit test with acceptance test
We don't care about the exact implementation details (middleware implementation), but we do care that we get a 404 error with JSON payload back.
1 parent 8123309 commit e93178a

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

src/middleware/known_error_to_json.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,3 @@ impl Middleware for KnownErrorToJson {
2424
})
2525
}
2626
}
27-
28-
#[cfg(test)]
29-
mod tests {
30-
use super::KnownErrorToJson;
31-
32-
use conduit::{Body, Handler, Method, StatusCode};
33-
use conduit_middleware::MiddlewareBuilder;
34-
use conduit_router::RouteBuilder;
35-
use conduit_test::MockRequest;
36-
37-
#[test]
38-
fn router_errors_become_not_found_response() {
39-
let route_builder = RouteBuilder::new();
40-
let mut middleware = MiddlewareBuilder::new(route_builder);
41-
middleware.add(KnownErrorToJson);
42-
43-
let mut req = MockRequest::new(Method::GET, "/");
44-
let (parts, body) = middleware.call(&mut req).unwrap().into_parts();
45-
assert_eq!(parts.status, StatusCode::NOT_FOUND);
46-
assert!(matches!(
47-
body,
48-
Body::Owned(vec) if vec == br#"{"errors":[{"detail":"Not Found"}]}"#
49-
));
50-
}
51-
}

src/tests/all.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ mod git;
4040
mod keyword;
4141
mod krate;
4242
mod metrics;
43+
mod not_found_error;
4344
mod owners;
4445
mod read_only_mode;
4546
mod record;

src/tests/not_found_error.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use crate::{RequestHelper, TestApp};
2+
use http::StatusCode;
3+
4+
#[test]
5+
fn visiting_unknown_route_returns_404() {
6+
let (_, anon) = TestApp::init().empty();
7+
8+
let response = anon.get::<()>("/does-not-exist");
9+
assert_eq!(response.status(), StatusCode::NOT_FOUND);
10+
assert_eq!(
11+
response.into_json(),
12+
json!({ "errors": [{ "detail": "Not Found" }] })
13+
);
14+
}
15+
16+
#[test]
17+
fn visiting_unknown_api_route_returns_404() {
18+
let (_, anon) = TestApp::init().empty();
19+
20+
let response = anon.get::<()>("/api/v1/does-not-exist");
21+
assert_eq!(response.status(), StatusCode::NOT_FOUND);
22+
assert_eq!(
23+
response.into_json(),
24+
json!({ "errors": [{ "detail": "Not Found" }] })
25+
);
26+
}

0 commit comments

Comments
 (0)