Skip to content

Commit 7ef8a01

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 75563da commit 7ef8a01

File tree

5 files changed

+45
-47
lines changed

5 files changed

+45
-47
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: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn new_wrong_token() {
5757
// Try to publish without a token
5858
let crate_to_publish = PublishBuilder::new("foo");
5959
let response = anon.enqueue_publish(crate_to_publish);
60-
response.assert_status(StatusCode::FORBIDDEN);
60+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
6161
assert_eq!(
6262
response.json(),
6363
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
@@ -73,7 +73,7 @@ fn new_wrong_token() {
7373

7474
let crate_to_publish = PublishBuilder::new("foo");
7575
let response = token.enqueue_publish(crate_to_publish);
76-
response.assert_status(StatusCode::FORBIDDEN);
76+
assert_eq!(response.status(), StatusCode::FORBIDDEN);
7777
assert_eq!(
7878
response.json(),
7979
json!({ "errors": [{ "detail": "must be logged in to perform that action" }] })
@@ -278,7 +278,7 @@ fn reject_new_crate_with_alternative_registry_dependency() {
278278

279279
let crate_to_publish = PublishBuilder::new("depends-on-alt-registry").dependency(dependency);
280280
let response = token.enqueue_publish(crate_to_publish);
281-
response.assert_status(StatusCode::OK);
281+
assert_eq!(response.status(), StatusCode::OK);
282282
assert_eq!(
283283
response.json(),
284284
json!({ "errors": [{ "detail": "Dependency `dep` is hosted on another registry. Cross-registry dependencies are not permitted on crates.io." }] })
@@ -301,7 +301,7 @@ fn new_krate_with_wildcard_dependency() {
301301
.dependency(dependency);
302302

303303
let response = token.enqueue_publish(crate_to_publish);
304-
response.assert_status(StatusCode::OK);
304+
assert_eq!(response.status(), StatusCode::OK);
305305
assert_eq!(
306306
response.json(),
307307
json!({ "errors": [{ "detail": WILDCARD_ERROR_MESSAGE }] })
@@ -340,7 +340,7 @@ fn new_krate_wrong_user() {
340340
let crate_to_publish = PublishBuilder::new("foo_wrong").version("2.0.0");
341341

342342
let response = another_user.enqueue_publish(crate_to_publish);
343-
response.assert_status(StatusCode::OK);
343+
assert_eq!(response.status(), StatusCode::OK);
344344
assert_eq!(
345345
response.json(),
346346
json!({ "errors": [{ "detail": MISSING_RIGHTS_ERROR_MESSAGE }] })
@@ -355,7 +355,7 @@ fn new_krate_too_big() {
355355
let builder = PublishBuilder::new("foo_big").files(&files);
356356

357357
let response = user.enqueue_publish(builder);
358-
response.assert_status(StatusCode::OK);
358+
assert_eq!(response.status(), StatusCode::OK);
359359
assert_eq!(
360360
response.json(),
361361
json!({ "errors": [{ "detail": "uploaded tarball is malformed or too large when decompressed" }] })
@@ -388,7 +388,7 @@ fn new_krate_wrong_files() {
388388
let builder = PublishBuilder::new("foo").files(&files);
389389

390390
let response = user.enqueue_publish(builder);
391-
response.assert_status(StatusCode::OK);
391+
assert_eq!(response.status(), StatusCode::OK);
392392
assert_eq!(
393393
response.json(),
394394
json!({ "errors": [{ "detail": "invalid tarball uploaded" }] })
@@ -407,7 +407,7 @@ fn new_krate_gzip_bomb() {
407407
.files_with_io(&mut [("foo-1.1.0/a", &mut body, len)]);
408408

409409
let response = token.enqueue_publish(crate_to_publish);
410-
response.assert_status(StatusCode::OK);
410+
assert_eq!(response.status(), StatusCode::OK);
411411
assert_eq!(
412412
response.json(),
413413
json!({ "errors": [{ "detail": "uploaded tarball is malformed or too large when decompressed" }] })
@@ -427,7 +427,7 @@ fn new_krate_duplicate_version() {
427427

428428
let crate_to_publish = PublishBuilder::new("foo_dupe").version("1.0.0");
429429
let response = token.enqueue_publish(crate_to_publish);
430-
response.assert_status(StatusCode::OK);
430+
assert_eq!(response.status(), StatusCode::OK);
431431
assert_eq!(
432432
response.json(),
433433
json!({ "errors": [{ "detail": "crate version `1.0.0` is already uploaded" }] })
@@ -446,7 +446,7 @@ fn new_crate_similar_name() {
446446

447447
let crate_to_publish = PublishBuilder::new("foo_similar").version("1.1.0");
448448
let response = token.enqueue_publish(crate_to_publish);
449-
response.assert_status(StatusCode::OK);
449+
assert_eq!(response.status(), StatusCode::OK);
450450
assert_eq!(
451451
response.json(),
452452
json!({ "errors": [{ "detail": "crate was previously named `Foo_similar`" }] })
@@ -465,7 +465,7 @@ fn new_crate_similar_name_hyphen() {
465465

466466
let crate_to_publish = PublishBuilder::new("foo-bar-hyphen").version("1.1.0");
467467
let response = token.enqueue_publish(crate_to_publish);
468-
response.assert_status(StatusCode::OK);
468+
assert_eq!(response.status(), StatusCode::OK);
469469
assert_eq!(
470470
response.json(),
471471
json!({ "errors": [{ "detail": "crate was previously named `foo_bar_hyphen`" }] })
@@ -484,7 +484,7 @@ fn new_crate_similar_name_underscore() {
484484

485485
let crate_to_publish = PublishBuilder::new("foo_bar_underscore").version("1.1.0");
486486
let response = token.enqueue_publish(crate_to_publish);
487-
response.assert_status(StatusCode::OK);
487+
assert_eq!(response.status(), StatusCode::OK);
488488
assert_eq!(
489489
response.json(),
490490
json!({ "errors": [{ "detail": "crate was previously named `foo-bar-underscore`" }] })
@@ -557,7 +557,7 @@ fn new_krate_dependency_missing() {
557557
let crate_to_publish = PublishBuilder::new("foo_missing").dependency(dependency);
558558

559559
let response = token.enqueue_publish(crate_to_publish);
560-
response.assert_status(StatusCode::OK);
560+
assert_eq!(response.status(), StatusCode::OK);
561561
assert_eq!(
562562
response.json(),
563563
json!({ "errors": [{ "detail": "no known crate named `bar_missing`" }] })
@@ -586,7 +586,7 @@ fn new_krate_without_any_email_fails() {
586586
let crate_to_publish = PublishBuilder::new("foo_no_email");
587587

588588
let response = token.enqueue_publish(crate_to_publish);
589-
response.assert_status(StatusCode::OK);
589+
assert_eq!(response.status(), StatusCode::OK);
590590
assert_eq!(
591591
response.json(),
592592
json!({ "errors": [{ "detail": "A verified email address is required to publish crates to crates.io. Visit https://crates.io/me to set and verify your email address." }] })
@@ -607,7 +607,7 @@ fn new_krate_with_unverified_email_fails() {
607607
let crate_to_publish = PublishBuilder::new("foo_unverified_email");
608608

609609
let response = token.enqueue_publish(crate_to_publish);
610-
response.assert_status(StatusCode::OK);
610+
assert_eq!(response.status(), StatusCode::OK);
611611
assert_eq!(
612612
response.json(),
613613
json!({ "errors": [{ "detail": "A verified email address is required to publish crates to crates.io. Visit https://crates.io/me to set and verify your email address." }] })
@@ -816,7 +816,7 @@ fn author_license_and_description_required() {
816816
.unset_authors();
817817

818818
let response = token.enqueue_publish(crate_to_publish);
819-
response.assert_status(StatusCode::OK);
819+
assert_eq!(response.status(), StatusCode::OK);
820820
assert_eq!(
821821
response.json(),
822822
json!({ "errors": [{ "detail": missing_metadata_error_message(&["description", "license", "authors"]) }] })
@@ -829,7 +829,7 @@ fn author_license_and_description_required() {
829829
.author("");
830830

831831
let response = token.enqueue_publish(crate_to_publish);
832-
response.assert_status(StatusCode::OK);
832+
assert_eq!(response.status(), StatusCode::OK);
833833
assert_eq!(
834834
response.json(),
835835
json!({ "errors": [{ "detail": missing_metadata_error_message(&["description", "authors"]) }] })
@@ -842,7 +842,7 @@ fn author_license_and_description_required() {
842842
.unset_description();
843843

844844
let response = token.enqueue_publish(crate_to_publish);
845-
response.assert_status(StatusCode::OK);
845+
assert_eq!(response.status(), StatusCode::OK);
846846
assert_eq!(
847847
response.json(),
848848
json!({ "errors": [{ "detail": missing_metadata_error_message(&["description"]) }] })
@@ -869,7 +869,7 @@ fn new_krate_tarball_with_hard_links() {
869869
let crate_to_publish = PublishBuilder::new("foo").version("1.1.0").tarball(tarball);
870870

871871
let response = token.enqueue_publish(crate_to_publish);
872-
response.assert_status(StatusCode::OK);
872+
assert_eq!(response.status(), StatusCode::OK);
873873
assert_eq!(
874874
response.json(),
875875
json!({ "errors": [{ "detail": "uploaded tarball is malformed or too large when decompressed" }] })
@@ -888,13 +888,12 @@ fn publish_new_crate_rate_limited() {
888888

889889
// Uploading a second crate is limited
890890
let crate_to_publish = PublishBuilder::new("rate_limited2");
891-
token
892-
.enqueue_publish(crate_to_publish)
893-
.assert_status(StatusCode::TOO_MANY_REQUESTS);
891+
let response = token.enqueue_publish(crate_to_publish);
892+
assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
894893
app.run_pending_background_jobs();
895894

896-
anon.get::<()>("/api/v1/crates/rate_limited2")
897-
.assert_status(StatusCode::NOT_FOUND);
895+
let response = anon.get::<()>("/api/v1/crates/rate_limited2");
896+
assert_eq!(response.status(), StatusCode::NOT_FOUND);
898897

899898
// Wait for the limit to be up
900899
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
@@ -596,8 +596,8 @@ where
596596
/// Assert that the response is good and deserialize the message
597597
#[track_caller]
598598
pub fn good(mut self) -> T {
599-
if !self.response.status().is_success() {
600-
panic!("bad response: {:?}", self.response.status());
599+
if !self.status().is_success() {
600+
panic!("bad response: {:?}", self.status());
601601
}
602602
crate::json(&mut self.response)
603603
}
@@ -607,17 +607,15 @@ where
607607
/// Cargo endpoints return a status 200 on error instead of 400.
608608
#[track_caller]
609609
pub fn bad_with_status(&mut self, expected: StatusCode) -> Bad {
610-
assert_eq!(self.response.status(), expected);
610+
assert_eq!(self.status(), expected);
611611
match crate::bad_resp(&mut self.response) {
612-
None => panic!("ok response: {:?}", self.response.status()),
612+
None => panic!("ok response: {:?}", self.status()),
613613
Some(b) => b,
614614
}
615615
}
616616

617-
#[track_caller]
618-
pub fn assert_status(&self, status: StatusCode) -> &Self {
619-
assert_eq!(status, self.response.status());
620-
self
617+
pub fn status(&self) -> StatusCode {
618+
self.response.status()
621619
}
622620

623621
#[track_caller]
@@ -638,12 +636,12 @@ impl Response<()> {
638636
/// Assert that the status code is 404
639637
#[track_caller]
640638
pub fn assert_not_found(&self) {
641-
assert_eq!(StatusCode::NOT_FOUND, self.response.status());
639+
assert_eq!(StatusCode::NOT_FOUND, self.status());
642640
}
643641

644642
/// Assert that the status code is 403
645643
#[track_caller]
646644
pub fn assert_forbidden(&self) {
647-
assert_eq!(StatusCode::FORBIDDEN, self.response.status());
645+
assert_eq!(StatusCode::FORBIDDEN, self.status());
648646
}
649647
}

0 commit comments

Comments
 (0)