Skip to content

Commit

Permalink
test for Scope::route(); prep release
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed May 7, 2018
1 parent c755d71 commit 72908d9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Changes

## 0.6.0 (...)
## 0.6.0 (2018-05-08)

* Add route scopes #202

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "actix-web"
version = "0.6.0-dev"
version = "0.6.0"
authors = ["Nikolay Kim <[email protected]>"]
description = "Actix web is a simple, pragmatic and extremely fast web framework for Rust."
readme = "README.md"
Expand Down
36 changes: 27 additions & 9 deletions src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// 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.
///
/// ```rust
Expand All @@ -50,6 +50,7 @@ type NestedInfo<S> = (Resource, Route<S>, Vec<Box<Predicate<S>>>);
/// * /{project_id}/path2 - `GET` requests
/// * /{project_id}/path3 - `HEAD` requests
///
#[derive(Default)]
pub struct Scope<S: 'static> {
filters: Vec<Box<Predicate<S>>>,
nested: Vec<NestedInfo<S>>,
Expand All @@ -58,12 +59,7 @@ pub struct Scope<S: 'static> {
resources: ScopeResources<S>,
}

impl<S: 'static> Default for Scope<S> {
fn default() -> Scope<S> {
Scope::new()
}
}

#[cfg_attr(feature = "cargo-clippy", allow(new_without_default_derive))]
impl<S: 'static> Scope<S> {
pub fn new() -> Scope<S> {
Scope {
Expand Down Expand Up @@ -319,7 +315,7 @@ impl<S: 'static> Scope<S> {
/// middlewares get invoked on scope level.
///
/// *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> {
Rc::get_mut(&mut self.middlewares)
.expect("Can not use after configuration")
Expand All @@ -333,7 +329,7 @@ impl<S: 'static> RouteHandler<S> for Scope<S> {
let path = unsafe { &*(&req.match_info()["tail"] as *const _) };
let path = if path == "" { "/" } else { path };

// recognize paths
// recognize resources
for &(ref pattern, ref resource) in self.resources.iter() {
if pattern.match_with_params(path, req.match_info_mut()) {
let default = unsafe { &mut *self.default.as_ref().get() };
Expand Down Expand Up @@ -777,6 +773,7 @@ mod tests {
use application::App;
use body::Body;
use http::{Method, StatusCode};
use httprequest::HttpRequest;
use httpresponse::HttpResponse;
use pred;
use test::TestRequest;
Expand All @@ -794,6 +791,27 @@ mod tests {
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]
fn test_scope_filter() {
let mut app = App::new()
Expand Down

0 comments on commit 72908d9

Please sign in to comment.