add assertion test for responder impls

This commit is contained in:
Rob Ede 2022-01-05 04:41:29 +00:00
parent e7b72a2e90
commit 9fddb8703a
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
5 changed files with 36 additions and 24 deletions

View File

@ -1,6 +1,10 @@
# Changes
## Unreleased - 2021-xx-xx
### Changed
- `HttpResponse` can now be used as a `Responder` with any body type.
[#2501]: https://github.com/actix/actix-web/pull/2501
## 4.0.0-beta.19 - 2022-01-04

View File

@ -118,6 +118,7 @@ futures-util = { version = "0.3.7", default-features = false, features = ["std"]
rand = "0.8"
rcgen = "0.8"
rustls-pemfile = "0.2"
static_assertions = "1"
tls-openssl = { package = "openssl", version = "0.10.9" }
tls-rustls = { package = "rustls", version = "0.20.0" }
zstd = "0.9"

View File

@ -23,7 +23,7 @@ use cookie::{Cookie, CookieJar};
use crate::{
error::{Error, JsonPayloadError},
BoxError, HttpResponse,
BoxError, HttpRequest, HttpResponse, Responder,
};
/// An HTTP response builder.
@ -424,6 +424,15 @@ impl Future for HttpResponseBuilder {
}
}
impl Responder for HttpResponseBuilder {
type Body = BoxBody;
#[inline]
fn respond_to(mut self, _: &HttpRequest) -> HttpResponse<Self::Body> {
self.finish()
}
}
#[cfg(test)]
mod tests {
use actix_http::body;

View File

@ -7,7 +7,7 @@ use actix_http::{
};
use bytes::{Bytes, BytesMut};
use crate::{Error, HttpRequest, HttpResponse, HttpResponseBuilder};
use crate::{Error, HttpRequest, HttpResponse};
use super::CustomizeResponder;
@ -57,18 +57,6 @@ pub trait Responder {
}
}
impl<B> Responder for HttpResponse<B>
where
B: MessageBody + 'static,
{
type Body = B;
#[inline]
fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body> {
self
}
}
impl Responder for actix_http::Response<BoxBody> {
type Body = BoxBody;
@ -78,15 +66,6 @@ impl Responder for actix_http::Response<BoxBody> {
}
}
impl Responder for HttpResponseBuilder {
type Body = BoxBody;
#[inline]
fn respond_to(mut self, _: &HttpRequest) -> HttpResponse<Self::Body> {
self.finish()
}
}
impl Responder for actix_http::ResponseBuilder {
type Body = BoxBody;

View File

@ -22,7 +22,7 @@ use {
cookie::Cookie,
};
use crate::{error::Error, HttpResponseBuilder};
use crate::{error::Error, HttpRequest, HttpResponseBuilder, Responder};
/// An outgoing response.
pub struct HttpResponse<B = BoxBody> {
@ -311,6 +311,18 @@ impl Future for HttpResponse<BoxBody> {
}
}
impl<B> Responder for HttpResponse<B>
where
B: MessageBody + 'static,
{
type Body = B;
#[inline]
fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body> {
self
}
}
#[cfg(feature = "cookies")]
pub struct CookieIter<'a> {
iter: std::slice::Iter<'a, HeaderValue>,
@ -333,9 +345,16 @@ impl<'a> Iterator for CookieIter<'a> {
#[cfg(test)]
mod tests {
use static_assertions::assert_impl_all;
use super::*;
use crate::http::header::{HeaderValue, COOKIE};
assert_impl_all!(HttpResponse: Responder);
assert_impl_all!(HttpResponse<String>: Responder);
assert_impl_all!(HttpResponse<&'static str>: Responder);
assert_impl_all!(HttpResponse<crate::body::None>: Responder);
#[test]
fn test_debug() {
let resp = HttpResponse::Ok()