logo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//! Status code based HTTP response builders.

use actix_http::StatusCode;

use crate::{HttpResponse, HttpResponseBuilder};

macro_rules! static_resp {
    ($name:ident, $status:expr) => {
        #[allow(non_snake_case, missing_docs)]
        pub fn $name() -> HttpResponseBuilder {
            HttpResponseBuilder::new($status)
        }
    };
}

impl HttpResponse {
    static_resp!(Continue, StatusCode::CONTINUE);
    static_resp!(SwitchingProtocols, StatusCode::SWITCHING_PROTOCOLS);
    static_resp!(Processing, StatusCode::PROCESSING);

    static_resp!(Ok, StatusCode::OK);
    static_resp!(Created, StatusCode::CREATED);
    static_resp!(Accepted, StatusCode::ACCEPTED);
    static_resp!(
        NonAuthoritativeInformation,
        StatusCode::NON_AUTHORITATIVE_INFORMATION
    );

    static_resp!(NoContent, StatusCode::NO_CONTENT);
    static_resp!(ResetContent, StatusCode::RESET_CONTENT);
    static_resp!(PartialContent, StatusCode::PARTIAL_CONTENT);
    static_resp!(MultiStatus, StatusCode::MULTI_STATUS);
    static_resp!(AlreadyReported, StatusCode::ALREADY_REPORTED);

    static_resp!(MultipleChoices, StatusCode::MULTIPLE_CHOICES);
    static_resp!(MovedPermanently, StatusCode::MOVED_PERMANENTLY);
    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);
    static_resp!(
        ProxyAuthenticationRequired,
        StatusCode::PROXY_AUTHENTICATION_REQUIRED
    );
    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!(PreconditionRequired, StatusCode::PRECONDITION_REQUIRED);
    static_resp!(PayloadTooLarge, StatusCode::PAYLOAD_TOO_LARGE);
    static_resp!(UriTooLong, StatusCode::URI_TOO_LONG);
    static_resp!(UnsupportedMediaType, StatusCode::UNSUPPORTED_MEDIA_TYPE);
    static_resp!(RangeNotSatisfiable, StatusCode::RANGE_NOT_SATISFIABLE);
    static_resp!(ExpectationFailed, StatusCode::EXPECTATION_FAILED);
    static_resp!(UnprocessableEntity, StatusCode::UNPROCESSABLE_ENTITY);
    static_resp!(TooManyRequests, StatusCode::TOO_MANY_REQUESTS);
    static_resp!(
        RequestHeaderFieldsTooLarge,
        StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE
    );
    static_resp!(
        UnavailableForLegalReasons,
        StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS
    );

    static_resp!(InternalServerError, StatusCode::INTERNAL_SERVER_ERROR);
    static_resp!(NotImplemented, StatusCode::NOT_IMPLEMENTED);
    static_resp!(BadGateway, StatusCode::BAD_GATEWAY);
    static_resp!(ServiceUnavailable, StatusCode::SERVICE_UNAVAILABLE);
    static_resp!(GatewayTimeout, StatusCode::GATEWAY_TIMEOUT);
    static_resp!(VersionNotSupported, StatusCode::HTTP_VERSION_NOT_SUPPORTED);
    static_resp!(VariantAlsoNegotiates, StatusCode::VARIANT_ALSO_NEGOTIATES);
    static_resp!(InsufficientStorage, StatusCode::INSUFFICIENT_STORAGE);
    static_resp!(LoopDetected, StatusCode::LOOP_DETECTED);
}

#[cfg(test)]
mod tests {
    use crate::http::StatusCode;
    use crate::HttpResponse;

    #[test]
    fn test_build() {
        let resp = HttpResponse::Ok().finish();
        assert_eq!(resp.status(), StatusCode::OK);
    }
}