2017-10-07 04:48:14 +00:00
|
|
|
//! Basic http responses
|
2018-04-30 04:05:10 +00:00
|
|
|
#![allow(non_upper_case_globals)]
|
2018-03-22 03:15:52 +00:00
|
|
|
use http::StatusCode;
|
2018-12-06 22:32:52 +00:00
|
|
|
|
|
|
|
use crate::response::{Response, ResponseBuilder};
|
2017-10-07 04:48:14 +00:00
|
|
|
|
2017-11-28 22:23:42 +00:00
|
|
|
macro_rules! STATIC_RESP {
|
|
|
|
($name:ident, $status:expr) => {
|
2018-06-02 13:51:58 +00:00
|
|
|
#[allow(non_snake_case, missing_docs)]
|
2018-10-05 18:04:59 +00:00
|
|
|
pub fn $name() -> ResponseBuilder {
|
|
|
|
Response::build($status)
|
2017-11-28 22:23:42 +00:00
|
|
|
}
|
2018-04-13 23:02:01 +00:00
|
|
|
};
|
2017-11-28 22:23:42 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 18:04:59 +00:00
|
|
|
impl Response {
|
2017-11-28 22:23:42 +00:00
|
|
|
STATIC_RESP!(Ok, StatusCode::OK);
|
|
|
|
STATIC_RESP!(Created, StatusCode::CREATED);
|
2018-03-31 06:07:33 +00:00
|
|
|
STATIC_RESP!(Accepted, StatusCode::ACCEPTED);
|
2018-04-29 16:09:08 +00:00
|
|
|
STATIC_RESP!(
|
|
|
|
NonAuthoritativeInformation,
|
|
|
|
StatusCode::NON_AUTHORITATIVE_INFORMATION
|
|
|
|
);
|
2018-03-31 06:07:33 +00:00
|
|
|
|
2017-11-28 22:23:42 +00:00
|
|
|
STATIC_RESP!(NoContent, StatusCode::NO_CONTENT);
|
2018-03-31 06:07:33 +00:00
|
|
|
STATIC_RESP!(ResetContent, StatusCode::RESET_CONTENT);
|
|
|
|
STATIC_RESP!(PartialContent, StatusCode::PARTIAL_CONTENT);
|
|
|
|
STATIC_RESP!(MultiStatus, StatusCode::MULTI_STATUS);
|
|
|
|
STATIC_RESP!(AlreadyReported, StatusCode::ALREADY_REPORTED);
|
2017-11-28 22:23:42 +00:00
|
|
|
|
|
|
|
STATIC_RESP!(MultipleChoices, StatusCode::MULTIPLE_CHOICES);
|
|
|
|
STATIC_RESP!(MovedPermanenty, StatusCode::MOVED_PERMANENTLY);
|
2018-04-13 21:36:07 +00:00
|
|
|
STATIC_RESP!(MovedPermanently, StatusCode::MOVED_PERMANENTLY);
|
2017-11-28 22:23:42 +00:00
|
|
|
STATIC_RESP!(Found, StatusCode::FOUND);
|
|
|
|
STATIC_RESP!(SeeOther, StatusCode::SEE_OTHER);
|
|
|
|
STATIC_RESP!(NotModified, StatusCode::NOT_MODIFIED);
|
|
|
|
STATIC_RESP!(UseProxy, StatusCode::USE_PROXY);
|
|
|
|
STATIC_RESP!(TemporaryRedirect, StatusCode::TEMPORARY_REDIRECT);
|
|
|
|
STATIC_RESP!(PermanentRedirect, StatusCode::PERMANENT_REDIRECT);
|
|
|
|
|
|
|
|
STATIC_RESP!(BadRequest, StatusCode::BAD_REQUEST);
|
|
|
|
STATIC_RESP!(NotFound, StatusCode::NOT_FOUND);
|
|
|
|
STATIC_RESP!(Unauthorized, StatusCode::UNAUTHORIZED);
|
|
|
|
STATIC_RESP!(PaymentRequired, StatusCode::PAYMENT_REQUIRED);
|
|
|
|
STATIC_RESP!(Forbidden, StatusCode::FORBIDDEN);
|
|
|
|
STATIC_RESP!(MethodNotAllowed, StatusCode::METHOD_NOT_ALLOWED);
|
|
|
|
STATIC_RESP!(NotAcceptable, StatusCode::NOT_ACCEPTABLE);
|
2018-04-29 16:09:08 +00:00
|
|
|
STATIC_RESP!(
|
|
|
|
ProxyAuthenticationRequired,
|
|
|
|
StatusCode::PROXY_AUTHENTICATION_REQUIRED
|
|
|
|
);
|
2017-11-28 22:23:42 +00:00
|
|
|
STATIC_RESP!(RequestTimeout, StatusCode::REQUEST_TIMEOUT);
|
|
|
|
STATIC_RESP!(Conflict, StatusCode::CONFLICT);
|
|
|
|
STATIC_RESP!(Gone, StatusCode::GONE);
|
|
|
|
STATIC_RESP!(LengthRequired, StatusCode::LENGTH_REQUIRED);
|
|
|
|
STATIC_RESP!(PreconditionFailed, StatusCode::PRECONDITION_FAILED);
|
|
|
|
STATIC_RESP!(PayloadTooLarge, StatusCode::PAYLOAD_TOO_LARGE);
|
|
|
|
STATIC_RESP!(UriTooLong, StatusCode::URI_TOO_LONG);
|
2018-05-17 19:20:20 +00:00
|
|
|
STATIC_RESP!(UnsupportedMediaType, StatusCode::UNSUPPORTED_MEDIA_TYPE);
|
2018-03-31 06:07:33 +00:00
|
|
|
STATIC_RESP!(RangeNotSatisfiable, StatusCode::RANGE_NOT_SATISFIABLE);
|
2017-11-28 22:23:42 +00:00
|
|
|
STATIC_RESP!(ExpectationFailed, StatusCode::EXPECTATION_FAILED);
|
|
|
|
|
|
|
|
STATIC_RESP!(InternalServerError, StatusCode::INTERNAL_SERVER_ERROR);
|
2018-03-31 06:07:33 +00:00
|
|
|
STATIC_RESP!(NotImplemented, StatusCode::NOT_IMPLEMENTED);
|
|
|
|
STATIC_RESP!(BadGateway, StatusCode::BAD_GATEWAY);
|
|
|
|
STATIC_RESP!(ServiceUnavailable, StatusCode::SERVICE_UNAVAILABLE);
|
|
|
|
STATIC_RESP!(GatewayTimeout, StatusCode::GATEWAY_TIMEOUT);
|
2018-05-17 19:20:20 +00:00
|
|
|
STATIC_RESP!(VersionNotSupported, StatusCode::HTTP_VERSION_NOT_SUPPORTED);
|
|
|
|
STATIC_RESP!(VariantAlsoNegotiates, StatusCode::VARIANT_ALSO_NEGOTIATES);
|
2018-03-31 06:07:33 +00:00
|
|
|
STATIC_RESP!(InsufficientStorage, StatusCode::INSUFFICIENT_STORAGE);
|
|
|
|
STATIC_RESP!(LoopDetected, StatusCode::LOOP_DETECTED);
|
2017-11-28 22:23:42 +00:00
|
|
|
}
|
2017-10-14 17:01:53 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2018-04-30 04:05:10 +00:00
|
|
|
use body::Body;
|
2017-10-14 17:01:53 +00:00
|
|
|
use http::StatusCode;
|
2018-10-05 18:04:59 +00:00
|
|
|
use response::Response;
|
2017-10-14 17:01:53 +00:00
|
|
|
|
|
|
|
#[test]
|
2017-11-27 06:31:29 +00:00
|
|
|
fn test_build() {
|
2018-10-05 18:04:59 +00:00
|
|
|
let resp = Response::Ok().body(Body::Empty);
|
2017-10-14 17:01:53 +00:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
}
|
|
|
|
}
|