2021-04-14 02:12:47 +01:00
|
|
|
//! HTTP response.
|
2021-01-15 02:11:10 +00:00
|
|
|
|
|
|
|
use std::{
|
|
|
|
cell::{Ref, RefMut},
|
|
|
|
fmt,
|
|
|
|
future::Future,
|
|
|
|
pin::Pin,
|
|
|
|
str,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
2017-10-06 21:48:14 -07:00
|
|
|
|
2019-12-05 23:35:43 +06:00
|
|
|
use bytes::{Bytes, BytesMut};
|
2017-12-07 16:40:29 -08:00
|
|
|
|
2021-02-13 15:08:43 +00:00
|
|
|
use crate::{
|
2021-04-14 02:12:47 +01:00
|
|
|
body::{Body, MessageBody, ResponseBody},
|
2021-04-09 18:07:10 +01:00
|
|
|
error::Error,
|
|
|
|
extensions::Extensions,
|
2021-04-14 02:12:47 +01:00
|
|
|
http::{HeaderMap, StatusCode},
|
|
|
|
message::{BoxedResponseHead, ResponseHead},
|
|
|
|
ResponseBuilder,
|
2021-02-13 15:08:43 +00:00
|
|
|
};
|
2018-03-09 10:00:15 -08:00
|
|
|
|
2021-04-14 02:12:47 +01:00
|
|
|
/// An HTTP response.
|
2021-04-13 11:16:12 +01:00
|
|
|
pub struct Response<B> {
|
2021-04-14 02:12:47 +01:00
|
|
|
pub(crate) head: BoxedResponseHead,
|
|
|
|
pub(crate) body: ResponseBody<B>,
|
|
|
|
pub(crate) error: Option<Error>,
|
2019-02-07 21:16:46 -08:00
|
|
|
}
|
2017-12-15 16:24:15 -08:00
|
|
|
|
2018-11-18 13:48:42 -08:00
|
|
|
impl Response<Body> {
|
2021-04-14 02:00:14 +01:00
|
|
|
/// Constructs a response
|
|
|
|
#[inline]
|
|
|
|
pub fn new(status: StatusCode) -> Response<Body> {
|
|
|
|
Response {
|
|
|
|
head: BoxedResponseHead::new(status),
|
|
|
|
body: ResponseBody::Body(Body::Empty),
|
|
|
|
error: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 22:39:54 +00:00
|
|
|
/// Create HTTP response builder with specific status.
|
2017-10-10 16:03:32 -07:00
|
|
|
#[inline]
|
2018-10-05 11:04:59 -07:00
|
|
|
pub fn build(status: StatusCode) -> ResponseBuilder {
|
2019-02-07 21:16:46 -08:00
|
|
|
ResponseBuilder::new(status)
|
2017-10-10 16:03:32 -07:00
|
|
|
}
|
|
|
|
|
2021-04-14 02:00:14 +01:00
|
|
|
// just a couple frequently used shortcuts
|
|
|
|
// this list should not grow larger than a few
|
|
|
|
|
|
|
|
/// Creates a new response with status 200 OK.
|
2018-03-21 19:54:21 -07:00
|
|
|
#[inline]
|
2021-04-14 02:00:14 +01:00
|
|
|
pub fn ok() -> Response<Body> {
|
|
|
|
Response::new(StatusCode::OK)
|
2018-03-21 19:54:21 -07:00
|
|
|
}
|
|
|
|
|
2021-04-14 02:00:14 +01:00
|
|
|
/// Creates a new response with status 400 Bad Request.
|
2017-10-06 21:48:14 -07:00
|
|
|
#[inline]
|
2021-04-14 02:00:14 +01:00
|
|
|
pub fn bad_request() -> Response<Body> {
|
|
|
|
Response::new(StatusCode::BAD_REQUEST)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new response with status 404 Not Found.
|
|
|
|
#[inline]
|
|
|
|
pub fn not_found() -> Response<Body> {
|
|
|
|
Response::new(StatusCode::NOT_FOUND)
|
2017-10-06 21:48:14 -07:00
|
|
|
}
|
|
|
|
|
2021-04-14 02:00:14 +01:00
|
|
|
/// Creates a new response with status 500 Internal Server Error.
|
|
|
|
#[inline]
|
|
|
|
pub fn internal_server_error() -> Response<Body> {
|
|
|
|
Response::new(StatusCode::INTERNAL_SERVER_ERROR)
|
|
|
|
}
|
|
|
|
|
|
|
|
// end shortcuts
|
|
|
|
|
2018-05-29 18:11:10 +02:00
|
|
|
/// Constructs an error response
|
2017-11-25 09:03:44 -08:00
|
|
|
#[inline]
|
2021-04-13 11:16:12 +01:00
|
|
|
pub fn from_error(error: Error) -> Response<Body> {
|
2019-11-26 16:07:39 +06:00
|
|
|
let mut resp = error.as_response_error().error_response();
|
2019-07-18 11:31:18 +03:00
|
|
|
if resp.head.status == StatusCode::INTERNAL_SERVER_ERROR {
|
2021-05-03 00:58:14 +01:00
|
|
|
debug!("Internal Server Error: {:?}", error);
|
2019-07-18 11:31:18 +03:00
|
|
|
}
|
2019-02-07 21:16:46 -08:00
|
|
|
resp.error = Some(error);
|
2019-04-10 12:43:31 -07:00
|
|
|
resp
|
2017-11-25 09:03:44 -08:00
|
|
|
}
|
|
|
|
|
2018-11-21 07:49:24 -08:00
|
|
|
/// Convert response to response with body
|
2019-03-05 17:24:24 -08:00
|
|
|
pub fn into_body<B>(self) -> Response<B> {
|
2019-02-07 21:16:46 -08:00
|
|
|
let b = match self.body {
|
2018-11-21 07:49:24 -08:00
|
|
|
ResponseBody::Body(b) => b,
|
|
|
|
ResponseBody::Other(b) => b,
|
|
|
|
};
|
2019-02-07 21:16:46 -08:00
|
|
|
Response {
|
|
|
|
head: self.head,
|
|
|
|
error: self.error,
|
|
|
|
body: ResponseBody::Other(b),
|
|
|
|
}
|
2018-11-21 07:49:24 -08:00
|
|
|
}
|
2018-11-17 20:21:28 -08:00
|
|
|
}
|
|
|
|
|
2019-02-20 21:05:37 -08:00
|
|
|
impl<B> Response<B> {
|
2019-04-07 23:06:21 -07:00
|
|
|
/// Constructs a response with body
|
|
|
|
#[inline]
|
|
|
|
pub fn with_body(status: StatusCode, body: B) -> Response<B> {
|
|
|
|
Response {
|
|
|
|
head: BoxedResponseHead::new(status),
|
|
|
|
body: ResponseBody::Body(body),
|
|
|
|
error: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-17 20:21:28 -08:00
|
|
|
#[inline]
|
2018-11-19 14:57:12 -08:00
|
|
|
/// Http message part of the response
|
|
|
|
pub fn head(&self) -> &ResponseHead {
|
2019-02-07 21:16:46 -08:00
|
|
|
&*self.head
|
2018-11-19 14:57:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-02-11 22:39:54 +00:00
|
|
|
/// Mutable reference to a HTTP message part of the response
|
2018-11-19 14:57:12 -08:00
|
|
|
pub fn head_mut(&mut self) -> &mut ResponseHead {
|
2019-02-07 21:16:46 -08:00
|
|
|
&mut *self.head
|
2018-11-17 20:21:28 -08:00
|
|
|
}
|
|
|
|
|
2017-11-25 09:03:44 -08:00
|
|
|
/// The source `error` for this response
|
|
|
|
#[inline]
|
|
|
|
pub fn error(&self) -> Option<&Error> {
|
2019-02-07 21:16:46 -08:00
|
|
|
self.error.as_ref()
|
2017-11-25 09:03:44 -08:00
|
|
|
}
|
|
|
|
|
2018-11-17 20:21:28 -08:00
|
|
|
/// Get the response status code
|
|
|
|
#[inline]
|
|
|
|
pub fn status(&self) -> StatusCode {
|
2019-02-07 21:16:46 -08:00
|
|
|
self.head.status
|
2018-11-17 20:21:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the `StatusCode` for this response
|
|
|
|
#[inline]
|
|
|
|
pub fn status_mut(&mut self) -> &mut StatusCode {
|
2019-02-07 21:16:46 -08:00
|
|
|
&mut self.head.status
|
2018-11-17 20:21:28 -08:00
|
|
|
}
|
|
|
|
|
2018-01-29 11:39:26 -08:00
|
|
|
/// Get the headers from the response
|
2017-10-06 21:48:14 -07:00
|
|
|
#[inline]
|
2017-10-09 23:07:32 -07:00
|
|
|
pub fn headers(&self) -> &HeaderMap {
|
2019-02-07 21:16:46 -08:00
|
|
|
&self.head.headers
|
2017-10-06 21:48:14 -07:00
|
|
|
}
|
|
|
|
|
2018-01-29 11:39:26 -08:00
|
|
|
/// Get a mutable reference to the headers
|
2017-10-06 21:48:14 -07:00
|
|
|
#[inline]
|
2017-10-09 23:07:32 -07:00
|
|
|
pub fn headers_mut(&mut self) -> &mut HeaderMap {
|
2019-02-07 21:16:46 -08:00
|
|
|
&mut self.head.headers
|
2017-10-06 21:48:14 -07:00
|
|
|
}
|
|
|
|
|
2017-10-07 21:48:00 -07:00
|
|
|
/// Connection upgrade status
|
2017-12-31 17:26:32 -08:00
|
|
|
#[inline]
|
2017-10-07 21:48:00 -07:00
|
|
|
pub fn upgrade(&self) -> bool {
|
2019-02-07 21:16:46 -08:00
|
|
|
self.head.upgrade()
|
2017-10-07 21:48:00 -07:00
|
|
|
}
|
|
|
|
|
2017-10-06 21:48:14 -07:00
|
|
|
/// Keep-alive status for this connection
|
2018-11-18 17:52:56 -08:00
|
|
|
pub fn keep_alive(&self) -> bool {
|
2019-02-07 21:16:46 -08:00
|
|
|
self.head.keep_alive()
|
2017-10-06 21:48:14 -07:00
|
|
|
}
|
2017-10-07 21:48:00 -07:00
|
|
|
|
2019-03-30 02:29:11 +03:00
|
|
|
/// Responses extensions
|
|
|
|
#[inline]
|
2019-12-08 00:46:51 +06:00
|
|
|
pub fn extensions(&self) -> Ref<'_, Extensions> {
|
2019-03-30 02:29:11 +03:00
|
|
|
self.head.extensions.borrow()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mutable reference to a the response's extensions
|
|
|
|
#[inline]
|
2019-12-08 00:46:51 +06:00
|
|
|
pub fn extensions_mut(&mut self) -> RefMut<'_, Extensions> {
|
2019-03-30 02:29:11 +03:00
|
|
|
self.head.extensions.borrow_mut()
|
|
|
|
}
|
|
|
|
|
2019-10-09 09:11:55 -04:00
|
|
|
/// Get body of this response
|
2017-12-31 17:26:32 -08:00
|
|
|
#[inline]
|
2019-02-07 21:16:46 -08:00
|
|
|
pub fn body(&self) -> &ResponseBody<B> {
|
|
|
|
&self.body
|
2017-10-06 21:48:14 -07:00
|
|
|
}
|
|
|
|
|
2017-10-07 21:48:00 -07:00
|
|
|
/// Set a body
|
2019-04-05 11:29:42 -07:00
|
|
|
pub fn set_body<B2>(self, body: B2) -> Response<B2> {
|
2019-02-07 21:16:46 -08:00
|
|
|
Response {
|
|
|
|
head: self.head,
|
|
|
|
body: ResponseBody::Body(body),
|
|
|
|
error: None,
|
|
|
|
}
|
2017-10-07 21:48:00 -07:00
|
|
|
}
|
|
|
|
|
2019-04-10 12:24:17 -07:00
|
|
|
/// Split response and body
|
|
|
|
pub fn into_parts(self) -> (Response<()>, ResponseBody<B>) {
|
|
|
|
(
|
|
|
|
Response {
|
|
|
|
head: self.head,
|
|
|
|
body: ResponseBody::Body(()),
|
|
|
|
error: self.error,
|
|
|
|
},
|
|
|
|
self.body,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-11-18 13:48:42 -08:00
|
|
|
/// Drop request's body
|
2019-04-05 11:29:42 -07:00
|
|
|
pub fn drop_body(self) -> Response<()> {
|
2019-02-07 21:16:46 -08:00
|
|
|
Response {
|
|
|
|
head: self.head,
|
|
|
|
body: ResponseBody::Body(()),
|
|
|
|
error: None,
|
|
|
|
}
|
2018-11-18 13:48:42 -08:00
|
|
|
}
|
|
|
|
|
2017-10-06 23:14:13 -07:00
|
|
|
/// Set a body and return previous body value
|
2019-03-05 18:14:30 -08:00
|
|
|
pub(crate) fn replace_body<B2>(self, body: B2) -> (Response<B2>, ResponseBody<B>) {
|
2019-02-07 21:16:46 -08:00
|
|
|
(
|
|
|
|
Response {
|
|
|
|
head: self.head,
|
|
|
|
body: ResponseBody::Body(body),
|
|
|
|
error: self.error,
|
|
|
|
},
|
|
|
|
self.body,
|
|
|
|
)
|
2018-06-25 10:10:02 +06:00
|
|
|
}
|
2019-02-18 17:01:35 -08:00
|
|
|
|
|
|
|
/// Set a body and return previous body value
|
2019-03-05 18:14:30 -08:00
|
|
|
pub fn map_body<F, B2>(mut self, f: F) -> Response<B2>
|
2019-02-18 17:01:35 -08:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut ResponseHead, ResponseBody<B>) -> ResponseBody<B2>,
|
|
|
|
{
|
|
|
|
let body = f(&mut self.head, self.body);
|
|
|
|
|
|
|
|
Response {
|
2019-03-29 21:13:39 -07:00
|
|
|
body,
|
2019-02-18 17:01:35 -08:00
|
|
|
head: self.head,
|
|
|
|
error: self.error,
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 21:15:18 -08:00
|
|
|
|
|
|
|
/// Extract response body
|
|
|
|
pub fn take_body(&mut self) -> ResponseBody<B> {
|
|
|
|
self.body.take_body()
|
|
|
|
}
|
2017-10-06 21:48:14 -07:00
|
|
|
}
|
2017-10-10 16:03:32 -07:00
|
|
|
|
2021-05-05 18:36:02 +01:00
|
|
|
impl<B> fmt::Debug for Response<B>
|
|
|
|
where
|
|
|
|
B: MessageBody,
|
|
|
|
B::Error: Into<Error>,
|
|
|
|
{
|
2019-12-08 00:46:51 +06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-04-13 16:02:01 -07:00
|
|
|
let res = writeln!(
|
|
|
|
f,
|
2018-10-05 11:04:59 -07:00
|
|
|
"\nResponse {:?} {}{}",
|
2019-02-07 21:16:46 -08:00
|
|
|
self.head.version,
|
|
|
|
self.head.status,
|
|
|
|
self.head.reason.unwrap_or(""),
|
2018-04-13 16:02:01 -07:00
|
|
|
);
|
2018-04-09 14:25:30 -07:00
|
|
|
let _ = writeln!(f, " headers:");
|
2019-02-07 21:16:46 -08:00
|
|
|
for (key, val) in self.head.headers.iter() {
|
2018-04-09 14:25:30 -07:00
|
|
|
let _ = writeln!(f, " {:?}: {:?}", key, val);
|
2017-11-08 19:31:25 -08:00
|
|
|
}
|
2019-04-10 12:24:17 -07:00
|
|
|
let _ = writeln!(f, " body: {:?}", self.body.size());
|
2017-11-08 19:31:25 -08:00
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:16:12 +01:00
|
|
|
impl<B: Unpin> Future for Response<B> {
|
|
|
|
type Output = Result<Response<B>, Error>;
|
2019-03-02 21:22:01 -08:00
|
|
|
|
2019-12-08 00:46:51 +06:00
|
|
|
fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
|
2019-11-21 21:34:04 +06:00
|
|
|
Poll::Ready(Ok(Response {
|
2019-11-22 11:49:35 +06:00
|
|
|
head: self.head.take(),
|
2019-11-21 21:34:04 +06:00
|
|
|
body: self.body.take_body(),
|
|
|
|
error: self.error.take(),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
2019-03-02 21:22:01 -08:00
|
|
|
|
2017-11-28 13:52:53 -08:00
|
|
|
/// Helper converters
|
2021-04-13 11:16:12 +01:00
|
|
|
impl<I: Into<Response<Body>>, E: Into<Error>> From<Result<I, E>> for Response<Body> {
|
2017-11-28 13:52:53 -08:00
|
|
|
fn from(res: Result<I, E>) -> Self {
|
|
|
|
match res {
|
|
|
|
Ok(val) => val.into(),
|
|
|
|
Err(err) => err.into().into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:16:12 +01:00
|
|
|
impl From<ResponseBuilder> for Response<Body> {
|
2018-10-05 11:04:59 -07:00
|
|
|
fn from(mut builder: ResponseBuilder) -> Self {
|
2018-03-30 23:07:33 -07:00
|
|
|
builder.finish()
|
2017-11-29 09:17:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:16:12 +01:00
|
|
|
impl From<&'static str> for Response<Body> {
|
2017-11-29 09:17:00 -08:00
|
|
|
fn from(val: &'static str) -> Self {
|
2021-04-14 02:00:14 +01:00
|
|
|
Response::build(StatusCode::OK)
|
2021-01-09 13:17:19 +00:00
|
|
|
.content_type(mime::TEXT_PLAIN_UTF_8)
|
2018-04-29 09:09:08 -07:00
|
|
|
.body(val)
|
2017-11-28 13:52:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:16:12 +01:00
|
|
|
impl From<&'static [u8]> for Response<Body> {
|
2017-11-29 09:17:00 -08:00
|
|
|
fn from(val: &'static [u8]) -> Self {
|
2021-04-14 02:00:14 +01:00
|
|
|
Response::build(StatusCode::OK)
|
2021-01-09 13:17:19 +00:00
|
|
|
.content_type(mime::APPLICATION_OCTET_STREAM)
|
2018-04-29 09:09:08 -07:00
|
|
|
.body(val)
|
2017-11-28 13:52:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:16:12 +01:00
|
|
|
impl From<String> for Response<Body> {
|
2017-11-29 09:17:00 -08:00
|
|
|
fn from(val: String) -> Self {
|
2021-04-14 02:00:14 +01:00
|
|
|
Response::build(StatusCode::OK)
|
2021-01-09 13:17:19 +00:00
|
|
|
.content_type(mime::TEXT_PLAIN_UTF_8)
|
2018-04-29 09:09:08 -07:00
|
|
|
.body(val)
|
2017-11-28 13:52:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:16:12 +01:00
|
|
|
impl<'a> From<&'a String> for Response<Body> {
|
2018-11-18 13:48:42 -08:00
|
|
|
fn from(val: &'a String) -> Self {
|
2021-04-14 02:00:14 +01:00
|
|
|
Response::build(StatusCode::OK)
|
2021-01-09 13:17:19 +00:00
|
|
|
.content_type(mime::TEXT_PLAIN_UTF_8)
|
2018-11-18 13:48:42 -08:00
|
|
|
.body(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:16:12 +01:00
|
|
|
impl From<Bytes> for Response<Body> {
|
2017-11-29 09:17:00 -08:00
|
|
|
fn from(val: Bytes) -> Self {
|
2021-04-14 02:00:14 +01:00
|
|
|
Response::build(StatusCode::OK)
|
2021-01-09 13:17:19 +00:00
|
|
|
.content_type(mime::APPLICATION_OCTET_STREAM)
|
2018-04-29 09:09:08 -07:00
|
|
|
.body(val)
|
2017-11-28 13:52:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:16:12 +01:00
|
|
|
impl From<BytesMut> for Response<Body> {
|
2017-11-29 09:17:00 -08:00
|
|
|
fn from(val: BytesMut) -> Self {
|
2021-04-14 02:00:14 +01:00
|
|
|
Response::build(StatusCode::OK)
|
2021-01-09 13:17:19 +00:00
|
|
|
.content_type(mime::APPLICATION_OCTET_STREAM)
|
2018-04-29 09:09:08 -07:00
|
|
|
.body(val)
|
2017-11-28 13:52:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-22 22:54:11 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-12-06 15:03:01 -08:00
|
|
|
use crate::body::Body;
|
2021-04-14 02:12:47 +01:00
|
|
|
use crate::http::header::{HeaderValue, CONTENT_TYPE, COOKIE};
|
2018-06-25 10:58:04 +06:00
|
|
|
|
2017-12-13 11:16:26 -08:00
|
|
|
#[test]
|
|
|
|
fn test_debug() {
|
2021-04-14 02:00:14 +01:00
|
|
|
let resp = Response::build(StatusCode::OK)
|
2021-01-15 02:11:10 +00:00
|
|
|
.append_header((COOKIE, HeaderValue::from_static("cookie1=value1; ")))
|
|
|
|
.append_header((COOKIE, HeaderValue::from_static("cookie2=value2; ")))
|
2018-03-30 23:07:33 -07:00
|
|
|
.finish();
|
2017-12-13 11:16:26 -08:00
|
|
|
let dbg = format!("{:?}", resp);
|
2018-10-05 11:04:59 -07:00
|
|
|
assert!(dbg.contains("Response"));
|
2017-12-13 11:16:26 -08:00
|
|
|
}
|
|
|
|
|
2017-11-28 13:52:53 -08:00
|
|
|
#[test]
|
|
|
|
fn test_into_response() {
|
2021-04-13 11:16:12 +01:00
|
|
|
let resp: Response<Body> = "test".into();
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-13 16:02:01 -07:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("text/plain; charset=utf-8")
|
|
|
|
);
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-11-18 13:48:42 -08:00
|
|
|
assert_eq!(resp.body().get_ref(), b"test");
|
2017-11-28 13:52:53 -08:00
|
|
|
|
2021-04-13 11:16:12 +01:00
|
|
|
let resp: Response<Body> = b"test".as_ref().into();
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-13 16:02:01 -07:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("application/octet-stream")
|
|
|
|
);
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-11-18 13:48:42 -08:00
|
|
|
assert_eq!(resp.body().get_ref(), b"test");
|
2017-11-28 13:52:53 -08:00
|
|
|
|
2021-04-13 11:16:12 +01:00
|
|
|
let resp: Response<Body> = "test".to_owned().into();
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-13 16:02:01 -07:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("text/plain; charset=utf-8")
|
|
|
|
);
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-11-18 13:48:42 -08:00
|
|
|
assert_eq!(resp.body().get_ref(), b"test");
|
2018-04-13 16:02:01 -07:00
|
|
|
|
2021-04-13 11:16:12 +01:00
|
|
|
let resp: Response<Body> = (&"test".to_owned()).into();
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-13 16:02:01 -07:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("text/plain; charset=utf-8")
|
|
|
|
);
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-11-18 13:48:42 -08:00
|
|
|
assert_eq!(resp.body().get_ref(), b"test");
|
2018-04-13 16:02:01 -07:00
|
|
|
|
2017-11-28 13:52:53 -08:00
|
|
|
let b = Bytes::from_static(b"test");
|
2021-04-13 11:16:12 +01:00
|
|
|
let resp: Response<Body> = b.into();
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-13 16:02:01 -07:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("application/octet-stream")
|
|
|
|
);
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-11-18 13:48:42 -08:00
|
|
|
assert_eq!(resp.body().get_ref(), b"test");
|
2017-11-28 13:52:53 -08:00
|
|
|
|
2017-12-03 14:22:04 -08:00
|
|
|
let b = Bytes::from_static(b"test");
|
2021-04-13 11:16:12 +01:00
|
|
|
let resp: Response<Body> = b.into();
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-13 16:02:01 -07:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("application/octet-stream")
|
|
|
|
);
|
2017-11-28 13:52:53 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-11-18 13:48:42 -08:00
|
|
|
assert_eq!(resp.body().get_ref(), b"test");
|
2017-12-03 14:22:04 -08:00
|
|
|
|
|
|
|
let b = BytesMut::from("test");
|
2021-04-13 11:16:12 +01:00
|
|
|
let resp: Response<Body> = b.into();
|
2017-12-03 14:22:04 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-04-13 16:02:01 -07:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(CONTENT_TYPE).unwrap(),
|
|
|
|
HeaderValue::from_static("application/octet-stream")
|
|
|
|
);
|
2019-04-06 15:02:02 -07:00
|
|
|
|
2017-12-03 14:22:04 -08:00
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2018-11-18 13:48:42 -08:00
|
|
|
assert_eq!(resp.body().get_ref(), b"test");
|
2017-11-28 13:52:53 -08:00
|
|
|
}
|
2017-10-22 22:54:11 -07:00
|
|
|
}
|