2017-10-07 06:48:14 +02:00
|
|
|
//! Basic http responses
|
2018-04-30 06:05:10 +02:00
|
|
|
#![allow(non_upper_case_globals)]
|
2018-03-22 04:15:52 +01:00
|
|
|
use http::StatusCode;
|
2018-12-06 23:32:52 +01:00
|
|
|
|
|
|
|
use crate::response::{Response, ResponseBuilder};
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-11-28 23:23:42 +01:00
|
|
|
macro_rules! STATIC_RESP {
|
|
|
|
($name:ident, $status:expr) => {
|
2018-06-02 15:51:58 +02:00
|
|
|
#[allow(non_snake_case, missing_docs)]
|
2018-10-05 20:04:59 +02:00
|
|
|
pub fn $name() -> ResponseBuilder {
|
2019-04-08 08:06:21 +02:00
|
|
|
ResponseBuilder::new($status)
|
2017-11-28 23:23:42 +01:00
|
|
|
}
|
2018-04-14 01:02:01 +02:00
|
|
|
};
|
2017-11-28 23:23:42 +01:00
|
|
|
}
|
|
|
|
|
2018-10-05 20:04:59 +02:00
|
|
|
impl Response {
|
2017-11-28 23:23:42 +01:00
|
|
|
STATIC_RESP!(Ok, StatusCode::OK);
|
|
|
|
STATIC_RESP!(Created, StatusCode::CREATED);
|
2018-03-31 08:07:33 +02:00
|
|
|
STATIC_RESP!(Accepted, StatusCode::ACCEPTED);
|
2018-04-29 18:09:08 +02:00
|
|
|
STATIC_RESP!(
|
|
|
|
NonAuthoritativeInformation,
|
|
|
|
StatusCode::NON_AUTHORITATIVE_INFORMATION
|
|
|
|
);
|
2018-03-31 08:07:33 +02:00
|
|
|
|
2017-11-28 23:23:42 +01:00
|
|
|
STATIC_RESP!(NoContent, StatusCode::NO_CONTENT);
|
2018-03-31 08:07:33 +02: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 23:23:42 +01:00
|
|
|
|
|
|
|
STATIC_RESP!(MultipleChoices, StatusCode::MULTIPLE_CHOICES);
|
|
|
|
STATIC_RESP!(MovedPermanenty, StatusCode::MOVED_PERMANENTLY);
|
2018-04-13 23:36:07 +02:00
|
|
|
STATIC_RESP!(MovedPermanently, StatusCode::MOVED_PERMANENTLY);
|
2017-11-28 23:23:42 +01: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 18:09:08 +02:00
|
|
|
STATIC_RESP!(
|
|
|
|
ProxyAuthenticationRequired,
|
|
|
|
StatusCode::PROXY_AUTHENTICATION_REQUIRED
|
|
|
|
);
|
2017-11-28 23:23:42 +01: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);
|
2019-02-07 20:06:05 +01:00
|
|
|
STATIC_RESP!(PreconditionRequired, StatusCode::PRECONDITION_REQUIRED);
|
2017-11-28 23:23:42 +01:00
|
|
|
STATIC_RESP!(PayloadTooLarge, StatusCode::PAYLOAD_TOO_LARGE);
|
|
|
|
STATIC_RESP!(UriTooLong, StatusCode::URI_TOO_LONG);
|
2018-05-17 21:20:20 +02:00
|
|
|
STATIC_RESP!(UnsupportedMediaType, StatusCode::UNSUPPORTED_MEDIA_TYPE);
|
2018-03-31 08:07:33 +02:00
|
|
|
STATIC_RESP!(RangeNotSatisfiable, StatusCode::RANGE_NOT_SATISFIABLE);
|
2017-11-28 23:23:42 +01:00
|
|
|
STATIC_RESP!(ExpectationFailed, StatusCode::EXPECTATION_FAILED);
|
2019-04-16 01:55:06 +02:00
|
|
|
STATIC_RESP!(UnprocessableEntity, StatusCode::UNPROCESSABLE_ENTITY);
|
2017-11-28 23:23:42 +01:00
|
|
|
|
|
|
|
STATIC_RESP!(InternalServerError, StatusCode::INTERNAL_SERVER_ERROR);
|
2018-03-31 08:07:33 +02: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 21:20:20 +02:00
|
|
|
STATIC_RESP!(VersionNotSupported, StatusCode::HTTP_VERSION_NOT_SUPPORTED);
|
|
|
|
STATIC_RESP!(VariantAlsoNegotiates, StatusCode::VARIANT_ALSO_NEGOTIATES);
|
2018-03-31 08:07:33 +02:00
|
|
|
STATIC_RESP!(InsufficientStorage, StatusCode::INSUFFICIENT_STORAGE);
|
|
|
|
STATIC_RESP!(LoopDetected, StatusCode::LOOP_DETECTED);
|
2017-11-28 23:23:42 +01:00
|
|
|
}
|
2017-10-14 19:01:53 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2018-12-07 00:03:01 +01:00
|
|
|
use crate::body::Body;
|
|
|
|
use crate::response::Response;
|
2017-10-14 19:01:53 +02:00
|
|
|
use http::StatusCode;
|
|
|
|
|
|
|
|
#[test]
|
2017-11-27 07:31:29 +01:00
|
|
|
fn test_build() {
|
2018-10-05 20:04:59 +02:00
|
|
|
let resp = Response::Ok().body(Body::Empty);
|
2017-10-14 19:01:53 +02:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
}
|
|
|
|
}
|