1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 16:02:59 +01:00

no need for mut ref

This commit is contained in:
Nikolay Kim 2018-01-03 10:57:57 -08:00
parent ae084d1146
commit 8348c830e2
3 changed files with 72 additions and 60 deletions

View File

@ -132,6 +132,12 @@ impl HttpRequest<()> {
pub fn with_state<S>(self, state: Rc<S>, router: Router) -> HttpRequest<S> { pub fn with_state<S>(self, state: Rc<S>, router: Router) -> HttpRequest<S> {
HttpRequest(self.0, Some(state), Some(router)) HttpRequest(self.0, Some(state), Some(router))
} }
#[cfg(test)]
/// Construct new http request with state.
pub(crate) fn with_state_no_router<S>(self, state: Rc<S>) -> HttpRequest<S> {
HttpRequest(self.0, Some(state), None)
}
} }
impl<S> HttpRequest<S> { impl<S> HttpRequest<S> {
@ -451,7 +457,7 @@ impl<S> HttpRequest<S> {
/// } /// }
/// # fn main() {} /// # fn main() {}
/// ``` /// ```
pub fn body(&mut self) -> RequestBody { pub fn body(&self) -> RequestBody {
RequestBody::from_request(self) RequestBody::from_request(self)
} }
@ -520,7 +526,7 @@ impl<S> HttpRequest<S> {
/// } /// }
/// # fn main() {} /// # fn main() {}
/// ``` /// ```
pub fn urlencoded(&mut self) -> UrlEncoded { pub fn urlencoded(&self) -> UrlEncoded {
UrlEncoded::from_request(self) UrlEncoded::from_request(self)
} }
@ -554,7 +560,7 @@ impl<S> HttpRequest<S> {
/// } /// }
/// # fn main() {} /// # fn main() {}
/// ``` /// ```
pub fn json<T: DeserializeOwned>(&mut self) -> JsonBody<S, T> { pub fn json<T: DeserializeOwned>(&self) -> JsonBody<S, T> {
JsonBody::from_request(self) JsonBody::from_request(self)
} }
} }
@ -604,9 +610,9 @@ pub struct UrlEncoded {
} }
impl UrlEncoded { impl UrlEncoded {
pub fn from_request<S>(req: &mut HttpRequest<S>) -> UrlEncoded { pub fn from_request<S>(req: &HttpRequest<S>) -> UrlEncoded {
let mut encoded = UrlEncoded { let mut encoded = UrlEncoded {
pl: req.payload_mut().clone(), pl: req.payload().clone(),
body: BytesMut::new(), body: BytesMut::new(),
error: None error: None
}; };
@ -684,7 +690,7 @@ pub struct RequestBody {
impl RequestBody { impl RequestBody {
/// Create `RequestBody` for request. /// Create `RequestBody` for request.
pub fn from_request<S>(req: &mut HttpRequest<S>) -> RequestBody { pub fn from_request<S>(req: &HttpRequest<S>) -> RequestBody {
let mut body = RequestBody { let mut body = RequestBody {
pl: req.payload().readany(), pl: req.payload().readany(),
body: BytesMut::new(), body: BytesMut::new(),
@ -793,20 +799,14 @@ mod tests {
#[test] #[test]
fn test_no_request_range_header() { fn test_no_request_range_header() {
let req = HttpRequest::new(Method::GET, Uri::from_str("/").unwrap(), let req = HttpRequest::default();
Version::HTTP_11, HeaderMap::new(), None);
let ranges = req.range(100).unwrap(); let ranges = req.range(100).unwrap();
assert!(ranges.is_empty()); assert!(ranges.is_empty());
} }
#[test] #[test]
fn test_request_range_header() { fn test_request_range_header() {
let mut headers = HeaderMap::new(); let req = TestRequest::with_header(header::RANGE, "bytes=0-4").finish();
headers.insert(header::RANGE,
header::HeaderValue::from_static("bytes=0-4"));
let req = HttpRequest::new(Method::GET, Uri::from_str("/").unwrap(),
Version::HTTP_11, headers, None);
let ranges = req.range(100).unwrap(); let ranges = req.range(100).unwrap();
assert_eq!(ranges.len(), 1); assert_eq!(ranges.len(), 1);
assert_eq!(ranges[0].start, 0); assert_eq!(ranges[0].start, 0);
@ -815,8 +815,7 @@ mod tests {
#[test] #[test]
fn test_request_query() { fn test_request_query() {
let req = HttpRequest::new(Method::GET, Uri::from_str("/?id=test").unwrap(), let req = TestRequest::with_uri("/?id=test").finish();
Version::HTTP_11, HeaderMap::new(), None);
assert_eq!(req.query_string(), "id=test"); assert_eq!(req.query_string(), "id=test");
let query = req.query(); let query = req.query();
assert_eq!(&query["id"], "test"); assert_eq!(&query["id"], "test");
@ -881,52 +880,61 @@ mod tests {
#[test] #[test]
fn test_urlencoded_error() { fn test_urlencoded_error() {
let mut headers = HeaderMap::new(); let req = TestRequest::with_header(header::TRANSFER_ENCODING, "chunked").finish();
headers.insert(header::TRANSFER_ENCODING,
header::HeaderValue::from_static("chunked"));
let mut req = HttpRequest::new(
Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, headers, None);
assert_eq!(req.urlencoded().poll().err().unwrap(), UrlencodedError::Chunked); assert_eq!(req.urlencoded().poll().err().unwrap(), UrlencodedError::Chunked);
let mut headers = HeaderMap::new(); let req = TestRequest::with_header(
headers.insert(header::CONTENT_TYPE, header::CONTENT_TYPE, "application/x-www-form-urlencoded")
header::HeaderValue::from_static("application/x-www-form-urlencoded")); .header(header::CONTENT_LENGTH, "xxxx")
headers.insert(header::CONTENT_LENGTH, .finish();
header::HeaderValue::from_static("xxxx"));
let mut req = HttpRequest::new(
Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, headers, None);
assert_eq!(req.urlencoded().poll().err().unwrap(), UrlencodedError::UnknownLength); assert_eq!(req.urlencoded().poll().err().unwrap(), UrlencodedError::UnknownLength);
let mut headers = HeaderMap::new(); let req = TestRequest::with_header(
headers.insert(header::CONTENT_TYPE, header::CONTENT_TYPE, "application/x-www-form-urlencoded")
header::HeaderValue::from_static("application/x-www-form-urlencoded")); .header(header::CONTENT_LENGTH, "1000000")
headers.insert(header::CONTENT_LENGTH, .finish();
header::HeaderValue::from_static("1000000"));
let mut req = HttpRequest::new(
Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, headers, None);
assert_eq!(req.urlencoded().poll().err().unwrap(), UrlencodedError::Overflow); assert_eq!(req.urlencoded().poll().err().unwrap(), UrlencodedError::Overflow);
let mut headers = HeaderMap::new(); let req = TestRequest::with_header(
headers.insert(header::CONTENT_TYPE, header::CONTENT_TYPE, "text/plain")
header::HeaderValue::from_static("text/plain")); .header(header::CONTENT_LENGTH, "10")
headers.insert(header::CONTENT_LENGTH, .finish();
header::HeaderValue::from_static("10"));
let mut req = HttpRequest::new(
Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, headers, None);
assert_eq!(req.urlencoded().poll().err().unwrap(), UrlencodedError::ContentType); assert_eq!(req.urlencoded().poll().err().unwrap(), UrlencodedError::ContentType);
} }
#[test]
fn test_request_body() {
let req = TestRequest::with_header(header::CONTENT_LENGTH, "xxxx").finish();
match req.body().poll().err().unwrap() {
PayloadError::UnknownLength => (),
_ => panic!("error"),
}
let req = TestRequest::with_header(header::CONTENT_LENGTH, "1000000").finish();
match req.body().poll().err().unwrap() {
PayloadError::Overflow => (),
_ => panic!("error"),
}
let mut req = HttpRequest::default();
req.payload_mut().unread_data(Bytes::from_static(b"test"));
match req.body().poll().ok().unwrap() {
Async::Ready(bytes) => assert_eq!(bytes, Bytes::from_static(b"test")),
_ => panic!("error"),
}
let mut req = HttpRequest::default();
req.payload_mut().unread_data(Bytes::from_static(b"11111111111111"));
match req.body().limit(5).poll().err().unwrap() {
PayloadError::Overflow => (),
_ => panic!("error"),
}
}
#[test] #[test]
fn test_url_for() { fn test_url_for() {
let mut headers = HeaderMap::new(); let req = TestRequest::with_header(header::HOST, "www.rust-lang.org")
headers.insert(header::HOST, .finish_no_router();
header::HeaderValue::from_static("www.rust-lang.org"));
let req = HttpRequest::new(
Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, headers, None);
let mut resource = Resource::<()>::default(); let mut resource = Resource::<()>::default();
resource.name("index"); resource.name("index");
@ -951,11 +959,7 @@ mod tests {
#[test] #[test]
fn test_url_for_with_prefix() { fn test_url_for_with_prefix() {
let mut headers = HeaderMap::new(); let req = TestRequest::with_header(header::HOST, "www.rust-lang.org").finish();
headers.insert(header::HOST,
header::HeaderValue::from_static("www.rust-lang.org"));
let req = HttpRequest::new(
Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11, headers, None);
let mut resource = Resource::<()>::default(); let mut resource = Resource::<()>::default();
resource.name("index"); resource.name("index");
@ -972,9 +976,7 @@ mod tests {
#[test] #[test]
fn test_url_for_external() { fn test_url_for_external() {
let req = HttpRequest::new( let req = HttpRequest::default();
Method::GET, Uri::from_str("/").unwrap(),
Version::HTTP_11, HeaderMap::new(), None);
let mut resource = Resource::<()>::default(); let mut resource = Resource::<()>::default();
resource.name("index"); resource.name("index");

View File

@ -86,7 +86,7 @@ pub struct JsonBody<S, T: DeserializeOwned>{
impl<S, T: DeserializeOwned> JsonBody<S, T> { impl<S, T: DeserializeOwned> JsonBody<S, T> {
/// Create `JsonBody` for request. /// Create `JsonBody` for request.
pub fn from_request(req: &mut HttpRequest<S>) -> Self { pub fn from_request(req: &HttpRequest<S>) -> Self {
JsonBody{ JsonBody{
limit: 262_144, limit: 262_144,
req: Some(req.clone()), req: Some(req.clone()),

View File

@ -351,6 +351,16 @@ impl<S> TestRequest<S> {
req.with_state(Rc::new(state), router) req.with_state(Rc::new(state), router)
} }
#[cfg(test)]
/// Complete request creation and generate `HttpRequest` instance
pub(crate) fn finish_no_router(self) -> HttpRequest<S> {
let TestRequest { state, method, uri, version, headers, params, cookies, payload } = self;
let req = HttpRequest::new(method, uri, version, headers, payload);
req.as_mut().cookies = cookies;
req.as_mut().params = params;
req.with_state_no_router(Rc::new(state))
}
/// This method generates `HttpRequest` instance and runs handler /// This method generates `HttpRequest` instance and runs handler
/// with generated request. /// with generated request.
/// ///