Skip to content

Commit 1ab8772

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 09f49e1 commit 1ab8772

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" }] })
@@ -232,7 +232,7 @@ fn reject_new_crate_with_alternative_registry_dependency() {
232232

233233
let crate_to_publish = PublishBuilder::new("depends-on-alt-registry").dependency(dependency);
234234
let response = token.enqueue_publish(crate_to_publish);
235-
response.assert_status(StatusCode::OK);
235+
assert_eq!(response.status(), StatusCode::OK);
236236
assert_eq!(
237237
response.json(),
238238
json!({ "errors": [{ "detail": "Dependency `dep` is hosted on another registry. Cross-registry dependencies are not permitted on crates.io." }] })
@@ -255,7 +255,7 @@ fn new_krate_with_wildcard_dependency() {
255255
.dependency(dependency);
256256

257257
let response = token.enqueue_publish(crate_to_publish);
258-
response.assert_status(StatusCode::OK);
258+
assert_eq!(response.status(), StatusCode::OK);
259259
assert_eq!(
260260
response.json(),
261261
json!({ "errors": [{ "detail": WILDCARD_ERROR_MESSAGE }] })
@@ -294,7 +294,7 @@ fn new_krate_wrong_user() {
294294
let crate_to_publish = PublishBuilder::new("foo_wrong").version("2.0.0");
295295

296296
let response = another_user.enqueue_publish(crate_to_publish);
297-
response.assert_status(StatusCode::OK);
297+
assert_eq!(response.status(), StatusCode::OK);
298298
assert_eq!(
299299
response.json(),
300300
json!({ "errors": [{ "detail": MISSING_RIGHTS_ERROR_MESSAGE }] })
@@ -309,7 +309,7 @@ fn new_krate_too_big() {
309309
let builder = PublishBuilder::new("foo_big").files(&files);
310310

311311
let response = user.enqueue_publish(builder);
312-
response.assert_status(StatusCode::OK);
312+
assert_eq!(response.status(), StatusCode::OK);
313313
assert_eq!(
314314
response.json(),
315315
json!({ "errors": [{ "detail": "uploaded tarball is malformed or too large when decompressed" }] })
@@ -342,7 +342,7 @@ fn new_krate_wrong_files() {
342342
let builder = PublishBuilder::new("foo").files(&files);
343343

344344
let response = user.enqueue_publish(builder);
345-
response.assert_status(StatusCode::OK);
345+
assert_eq!(response.status(), StatusCode::OK);
346346
assert_eq!(
347347
response.json(),
348348
json!({ "errors": [{ "detail": "invalid tarball uploaded" }] })
@@ -361,7 +361,7 @@ fn new_krate_gzip_bomb() {
361361
.files_with_io(&mut [("foo-1.1.0/a", &mut body, len)]);
362362

363363
let response = token.enqueue_publish(crate_to_publish);
364-
response.assert_status(StatusCode::OK);
364+
assert_eq!(response.status(), StatusCode::OK);
365365
assert_eq!(
366366
response.json(),
367367
json!({ "errors": [{ "detail": "uploaded tarball is malformed or too large when decompressed" }] })
@@ -381,7 +381,7 @@ fn new_krate_duplicate_version() {
381381

382382
let crate_to_publish = PublishBuilder::new("foo_dupe").version("1.0.0");
383383
let response = token.enqueue_publish(crate_to_publish);
384-
response.assert_status(StatusCode::OK);
384+
assert_eq!(response.status(), StatusCode::OK);
385385
assert_eq!(
386386
response.json(),
387387
json!({ "errors": [{ "detail": "crate version `1.0.0` is already uploaded" }] })
@@ -400,7 +400,7 @@ fn new_crate_similar_name() {
400400

401401
let crate_to_publish = PublishBuilder::new("foo_similar").version("1.1.0");
402402
let response = token.enqueue_publish(crate_to_publish);
403-
response.assert_status(StatusCode::OK);
403+
assert_eq!(response.status(), StatusCode::OK);
404404
assert_eq!(
405405
response.json(),
406406
json!({ "errors": [{ "detail": "crate was previously named `Foo_similar`" }] })
@@ -419,7 +419,7 @@ fn new_crate_similar_name_hyphen() {
419419

420420
let crate_to_publish = PublishBuilder::new("foo-bar-hyphen").version("1.1.0");
421421
let response = token.enqueue_publish(crate_to_publish);
422-
response.assert_status(StatusCode::OK);
422+
assert_eq!(response.status(), StatusCode::OK);
423423
assert_eq!(
424424
response.json(),
425425
json!({ "errors": [{ "detail": "crate was previously named `foo_bar_hyphen`" }] })
@@ -438,7 +438,7 @@ fn new_crate_similar_name_underscore() {
438438

439439
let crate_to_publish = PublishBuilder::new("foo_bar_underscore").version("1.1.0");
440440
let response = token.enqueue_publish(crate_to_publish);
441-
response.assert_status(StatusCode::OK);
441+
assert_eq!(response.status(), StatusCode::OK);
442442
assert_eq!(
443443
response.json(),
444444
json!({ "errors": [{ "detail": "crate was previously named `foo-bar-underscore`" }] })
@@ -511,7 +511,7 @@ fn new_krate_dependency_missing() {
511511
let crate_to_publish = PublishBuilder::new("foo_missing").dependency(dependency);
512512

513513
let response = token.enqueue_publish(crate_to_publish);
514-
response.assert_status(StatusCode::OK);
514+
assert_eq!(response.status(), StatusCode::OK);
515515
assert_eq!(
516516
response.json(),
517517
json!({ "errors": [{ "detail": "no known crate named `bar_missing`" }] })
@@ -540,7 +540,7 @@ fn new_krate_without_any_email_fails() {
540540
let crate_to_publish = PublishBuilder::new("foo_no_email");
541541

542542
let response = token.enqueue_publish(crate_to_publish);
543-
response.assert_status(StatusCode::OK);
543+
assert_eq!(response.status(), StatusCode::OK);
544544
assert_eq!(
545545
response.json(),
546546
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." }] })
@@ -561,7 +561,7 @@ fn new_krate_with_unverified_email_fails() {
561561
let crate_to_publish = PublishBuilder::new("foo_unverified_email");
562562

563563
let response = token.enqueue_publish(crate_to_publish);
564-
response.assert_status(StatusCode::OK);
564+
assert_eq!(response.status(), StatusCode::OK);
565565
assert_eq!(
566566
response.json(),
567567
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." }] })
@@ -770,7 +770,7 @@ fn author_license_and_description_required() {
770770
.unset_authors();
771771

772772
let response = token.enqueue_publish(crate_to_publish);
773-
response.assert_status(StatusCode::OK);
773+
assert_eq!(response.status(), StatusCode::OK);
774774
assert_eq!(
775775
response.json(),
776776
json!({ "errors": [{ "detail": missing_metadata_error_message(&["description", "license", "authors"]) }] })
@@ -783,7 +783,7 @@ fn author_license_and_description_required() {
783783
.author("");
784784

785785
let response = token.enqueue_publish(crate_to_publish);
786-
response.assert_status(StatusCode::OK);
786+
assert_eq!(response.status(), StatusCode::OK);
787787
assert_eq!(
788788
response.json(),
789789
json!({ "errors": [{ "detail": missing_metadata_error_message(&["description", "authors"]) }] })
@@ -796,7 +796,7 @@ fn author_license_and_description_required() {
796796
.unset_description();
797797

798798
let response = token.enqueue_publish(crate_to_publish);
799-
response.assert_status(StatusCode::OK);
799+
assert_eq!(response.status(), StatusCode::OK);
800800
assert_eq!(
801801
response.json(),
802802
json!({ "errors": [{ "detail": missing_metadata_error_message(&["description"]) }] })
@@ -823,7 +823,7 @@ fn new_krate_tarball_with_hard_links() {
823823
let crate_to_publish = PublishBuilder::new("foo").version("1.1.0").tarball(tarball);
824824

825825
let response = token.enqueue_publish(crate_to_publish);
826-
response.assert_status(StatusCode::OK);
826+
assert_eq!(response.status(), StatusCode::OK);
827827
assert_eq!(
828828
response.json(),
829829
json!({ "errors": [{ "detail": "uploaded tarball is malformed or too large when decompressed" }] })
@@ -842,13 +842,12 @@ fn publish_new_crate_rate_limited() {
842842

843843
// Uploading a second crate is limited
844844
let crate_to_publish = PublishBuilder::new("rate_limited2");
845-
token
846-
.enqueue_publish(crate_to_publish)
847-
.assert_status(StatusCode::TOO_MANY_REQUESTS);
845+
let response = token.enqueue_publish(crate_to_publish);
846+
assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
848847
app.run_pending_background_jobs();
849848

850-
anon.get::<()>("/api/v1/crates/rate_limited2")
851-
.assert_status(StatusCode::NOT_FOUND);
849+
let response = anon.get::<()>("/api/v1/crates/rate_limited2");
850+
assert_eq!(response.status(), StatusCode::NOT_FOUND);
852851

853852
// Wait for the limit to be up
854853
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)