Skip to main content

rejection_from

Function rejection_from 

Source
pub fn rejection_from<Body>(response: HttpResponse) -> Result<Response<Body>>
Expand description

Build the crux_http::Result a feature receives for a rejection, from a full protocol response.

Use this over rejection when the rejection’s headers are what the feature acts on — Retry-After, WWW-Authenticate, Content-Type — since those are readable from the error via HttpError::header and HttpError::content_type:

let result = rejection_from::<Vec<u8>>(
    HttpResponse::status(503)
        .header("retry-after", "120")
        .body(b"maintenance".to_vec())
        .build(),
);

let error = result.expect_err("a 503 is never Ok");
assert_eq!(error.header("retry-after").unwrap(), "120");
assert_eq!(error.body(), Some(&b"maintenance"[..]));

It takes the same HttpResponse you would resolve a request with in an end-to-end test, so the two styles of test describe a rejection the same way.

§Errors

Always Err, for the reason given on rejection.

§Panics

Panics if the response’s status is outside the valid HTTP range (100–999), or if it is not a client (4xx) or server (5xx) error.