Skip to content

Commit 62c91a7

Browse files
committed
tests/util: Replace Response::assert_status() with Response::status()
`Response::status()` is easier to use, slightly less verbose in most cases, and does not hide the implementation details as much.
1 parent 2d81921 commit 62c91a7

File tree

5 files changed

+26
-28
lines changed

5 files changed

+26
-28
lines changed

src/tests/krate/downloads.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ fn download() {
3737

3838
let download = |name_and_version: &str| {
3939
let url = format!("/api/v1/crates/{}/download", name_and_version);
40-
anon.get::<()>(&url).assert_status(StatusCode::FOUND);
40+
let response = anon.get::<()>(&url);
41+
assert_eq!(response.status(), StatusCode::FOUND);
4142
// TODO: test the with_json code path
4243
};
4344

src/tests/krate/publish.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -910,13 +910,12 @@ fn publish_new_crate_rate_limited() {
910910

911911
// Uploading a second crate is limited
912912
let crate_to_publish = PublishBuilder::new("rate_limited2");
913-
token
914-
.enqueue_publish(crate_to_publish)
915-
.assert_status(StatusCode::TOO_MANY_REQUESTS);
913+
let response = token.enqueue_publish(crate_to_publish);
914+
assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
916915
app.run_pending_background_jobs();
917916

918-
anon.get::<()>("/api/v1/crates/rate_limited2")
919-
.assert_status(StatusCode::NOT_FOUND);
917+
let response = anon.get::<()>("/api/v1/crates/rate_limited2");
918+
assert_eq!(response.status(), StatusCode::NOT_FOUND);
920919

921920
// Wait for the limit to be up
922921
thread::sleep(Duration::from_millis(500));

src/tests/read_only_mode.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use diesel::prelude::*;
88
fn can_hit_read_only_endpoints_in_read_only_mode() {
99
let (app, anon) = TestApp::init().empty();
1010
app.db(set_read_only).unwrap();
11-
anon.get::<()>("/api/v1/crates")
12-
.assert_status(StatusCode::OK);
11+
let response = anon.get::<()>("/api/v1/crates");
12+
assert_eq!(response.status(), StatusCode::OK);
1313
}
1414

1515
#[test]
@@ -21,9 +21,9 @@ fn cannot_hit_endpoint_which_writes_db_in_read_only_mode() {
2121
.expect_build(conn);
2222
set_read_only(conn).unwrap();
2323
});
24-
token
25-
.delete::<()>("/api/v1/crates/foo_yank_read_only/1.0.0/yank")
26-
.assert_status(StatusCode::SERVICE_UNAVAILABLE);
24+
25+
let response = token.delete::<()>("/api/v1/crates/foo_yank_read_only/1.0.0/yank");
26+
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
2727

2828
// Restore the transaction so `TestApp::drop` can still access the transaction
2929
app.db(|conn| {
@@ -44,8 +44,8 @@ fn can_download_crate_in_read_only_mode() {
4444
set_read_only(conn).unwrap();
4545
});
4646

47-
anon.get::<()>("/api/v1/crates/foo_download_read_only/1.0.0/download")
48-
.assert_status(StatusCode::FOUND);
47+
let response = anon.get::<()>("/api/v1/crates/foo_download_read_only/1.0.0/download");
48+
assert_eq!(response.status(), StatusCode::FOUND);
4949

5050
// We're in read only mode so the download should not have been counted
5151
app.db(|conn| {

src/tests/server.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn user_agent_is_required() {
1010
let mut req = anon.request_builder(Method::GET, "/api/v1/crates");
1111
req.header(header::USER_AGENT, "");
1212
let resp = anon.run::<()>(req);
13-
resp.assert_status(StatusCode::FORBIDDEN);
13+
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
1414
}
1515

1616
#[test]
@@ -24,7 +24,7 @@ fn user_agent_is_not_required_for_download() {
2424
let mut req = anon.request_builder(Method::GET, "/api/v1/crates/dl_no_ua/0.99.0/download");
2525
req.header(header::USER_AGENT, "");
2626
let resp = anon.run::<()>(req);
27-
resp.assert_status(StatusCode::FOUND);
27+
assert_eq!(resp.status(), StatusCode::FOUND);
2828
}
2929

3030
#[test]
@@ -42,7 +42,7 @@ fn blocked_traffic_doesnt_panic_if_checked_header_is_not_present() {
4242
let mut req = anon.request_builder(Method::GET, "/api/v1/crates/dl_no_ua/0.99.0/download");
4343
req.header(header::USER_AGENT, "");
4444
let resp = anon.run::<()>(req);
45-
resp.assert_status(StatusCode::FOUND);
45+
assert_eq!(resp.status(), StatusCode::FOUND);
4646
}
4747

4848
#[test]
@@ -67,7 +67,7 @@ fn block_traffic_via_arbitrary_header_and_value() {
6767
"abcd",
6868
);
6969
let resp = anon.run::<()>(req);
70-
resp.assert_status(StatusCode::FORBIDDEN);
70+
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
7171

7272
let mut req = anon.request_builder(Method::GET, "/api/v1/crates/dl_no_ua/0.99.0/download");
7373
// A request with a header value we don't want to block is allowed, even though there might
@@ -77,5 +77,5 @@ fn block_traffic_via_arbitrary_header_and_value() {
7777
"1value-must-match-exactly-this-is-allowed",
7878
);
7979
let resp = anon.run::<()>(req);
80-
resp.assert_status(StatusCode::FOUND);
80+
assert_eq!(resp.status(), StatusCode::FOUND);
8181
}

src/tests/util.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,8 @@ where
590590
/// Assert that the response is good and deserialize the message
591591
#[track_caller]
592592
pub fn good(mut self) -> T {
593-
if !self.response.status().is_success() {
594-
panic!("bad response: {:?}", self.response.status());
593+
if !self.status().is_success() {
594+
panic!("bad response: {:?}", self.status());
595595
}
596596
crate::json(&mut self.response)
597597
}
@@ -601,17 +601,15 @@ where
601601
/// Cargo endpoints return a status 200 on error instead of 400.
602602
#[track_caller]
603603
pub fn bad_with_status(&mut self, expected: StatusCode) -> Bad {
604-
assert_eq!(self.response.status(), expected);
604+
assert_eq!(self.status(), expected);
605605
match crate::bad_resp(&mut self.response) {
606-
None => panic!("ok response: {:?}", self.response.status()),
606+
None => panic!("ok response: {:?}", self.status()),
607607
Some(b) => b,
608608
}
609609
}
610610

611-
#[track_caller]
612-
pub fn assert_status(&self, status: StatusCode) -> &Self {
613-
assert_eq!(status, self.response.status());
614-
self
611+
pub fn status(&self) -> StatusCode {
612+
self.response.status()
615613
}
616614

617615
#[track_caller]
@@ -632,12 +630,12 @@ impl Response<()> {
632630
/// Assert that the status code is 404
633631
#[track_caller]
634632
pub fn assert_not_found(&self) {
635-
assert_eq!(StatusCode::NOT_FOUND, self.response.status());
633+
assert_eq!(StatusCode::NOT_FOUND, self.status());
636634
}
637635

638636
/// Assert that the status code is 403
639637
#[track_caller]
640638
pub fn assert_forbidden(&self) {
641-
assert_eq!(StatusCode::FORBIDDEN, self.response.status());
639+
assert_eq!(StatusCode::FORBIDDEN, self.status());
642640
}
643641
}

0 commit comments

Comments
 (0)