1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-30 18:34:36 +01:00

test for Scope::route(); prep release

This commit is contained in:
Nikolay Kim 2018-05-07 15:19:03 -07:00
parent c755d71a8b
commit 72908d974c
3 changed files with 29 additions and 11 deletions

View File

@ -1,6 +1,6 @@
# Changes # Changes
## 0.6.0 (...) ## 0.6.0 (2018-05-08)
* Add route scopes #202 * Add route scopes #202

View File

@ -1,6 +1,6 @@
[package] [package]
name = "actix-web" name = "actix-web"
version = "0.6.0-dev" version = "0.6.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix web is a simple, pragmatic and extremely fast web framework for Rust." description = "Actix web is a simple, pragmatic and extremely fast web framework for Rust."
readme = "README.md" readme = "README.md"

View File

@ -28,7 +28,7 @@ type NestedInfo<S> = (Resource, Route<S>, Vec<Box<Predicate<S>>>);
/// Scope prefix is always complete path segment, i.e `/app` would /// Scope prefix is always complete path segment, i.e `/app` would
/// be converted to a `/app/` and it would not match `/app` path. /// be converted to a `/app/` and it would not match `/app` path.
/// ///
/// You can get variable path segments from HttpRequest::match_info()`. /// You can get variable path segments from `HttpRequest::match_info()`.
/// `Path` extractor also is able to extract scope level variable segments. /// `Path` extractor also is able to extract scope level variable segments.
/// ///
/// ```rust /// ```rust
@ -50,6 +50,7 @@ type NestedInfo<S> = (Resource, Route<S>, Vec<Box<Predicate<S>>>);
/// * /{project_id}/path2 - `GET` requests /// * /{project_id}/path2 - `GET` requests
/// * /{project_id}/path3 - `HEAD` requests /// * /{project_id}/path3 - `HEAD` requests
/// ///
#[derive(Default)]
pub struct Scope<S: 'static> { pub struct Scope<S: 'static> {
filters: Vec<Box<Predicate<S>>>, filters: Vec<Box<Predicate<S>>>,
nested: Vec<NestedInfo<S>>, nested: Vec<NestedInfo<S>>,
@ -58,12 +59,7 @@ pub struct Scope<S: 'static> {
resources: ScopeResources<S>, resources: ScopeResources<S>,
} }
impl<S: 'static> Default for Scope<S> { #[cfg_attr(feature = "cargo-clippy", allow(new_without_default_derive))]
fn default() -> Scope<S> {
Scope::new()
}
}
impl<S: 'static> Scope<S> { impl<S: 'static> Scope<S> {
pub fn new() -> Scope<S> { pub fn new() -> Scope<S> {
Scope { Scope {
@ -319,7 +315,7 @@ impl<S: 'static> Scope<S> {
/// middlewares get invoked on scope level. /// middlewares get invoked on scope level.
/// ///
/// *Note* `Middleware::finish()` fires right after response get /// *Note* `Middleware::finish()` fires right after response get
/// prepared. It does not wait until body get sent to peer. /// prepared. It does not wait until body get sent to the peer.
pub fn middleware<M: Middleware<S>>(mut self, mw: M) -> Scope<S> { pub fn middleware<M: Middleware<S>>(mut self, mw: M) -> Scope<S> {
Rc::get_mut(&mut self.middlewares) Rc::get_mut(&mut self.middlewares)
.expect("Can not use after configuration") .expect("Can not use after configuration")
@ -333,7 +329,7 @@ impl<S: 'static> RouteHandler<S> for Scope<S> {
let path = unsafe { &*(&req.match_info()["tail"] as *const _) }; let path = unsafe { &*(&req.match_info()["tail"] as *const _) };
let path = if path == "" { "/" } else { path }; let path = if path == "" { "/" } else { path };
// recognize paths // recognize resources
for &(ref pattern, ref resource) in self.resources.iter() { for &(ref pattern, ref resource) in self.resources.iter() {
if pattern.match_with_params(path, req.match_info_mut()) { if pattern.match_with_params(path, req.match_info_mut()) {
let default = unsafe { &mut *self.default.as_ref().get() }; let default = unsafe { &mut *self.default.as_ref().get() };
@ -777,6 +773,7 @@ mod tests {
use application::App; use application::App;
use body::Body; use body::Body;
use http::{Method, StatusCode}; use http::{Method, StatusCode};
use httprequest::HttpRequest;
use httpresponse::HttpResponse; use httpresponse::HttpResponse;
use pred; use pred;
use test::TestRequest; use test::TestRequest;
@ -794,6 +791,27 @@ mod tests {
assert_eq!(resp.as_msg().status(), StatusCode::OK); assert_eq!(resp.as_msg().status(), StatusCode::OK);
} }
#[test]
fn test_scope_route() {
let mut app = App::new()
.scope("app", |scope| {
scope.route("/path1", Method::GET, |r: HttpRequest<_>| {
HttpResponse::Ok()
})
})
.finish();
let req = TestRequest::with_uri("/app/path1").finish();
let resp = app.run(req);
assert_eq!(resp.as_msg().status(), StatusCode::OK);
let req = TestRequest::with_uri("/app/path1")
.method(Method::POST)
.finish();
let resp = app.run(req);
assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
}
#[test] #[test]
fn test_scope_filter() { fn test_scope_filter() {
let mut app = App::new() let mut app = App::new()