2018-04-14 01:02:01 +02:00
|
|
|
use std::rc::Rc;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2018-07-15 11:12:21 +02:00
|
|
|
use handler::{AsyncResult, FromRequest, Handler, Responder, WrapHandler};
|
2018-03-29 20:06:44 +02:00
|
|
|
use header::ContentEncoding;
|
2018-07-15 11:12:21 +02:00
|
|
|
use http::Method;
|
2017-10-15 07:52:38 +02:00
|
|
|
use httprequest::HttpRequest;
|
2018-05-02 02:19:15 +02:00
|
|
|
use httpresponse::HttpResponse;
|
2017-12-27 04:59:41 +01:00
|
|
|
use middleware::Middleware;
|
2018-07-15 11:12:21 +02:00
|
|
|
use pipeline::{Pipeline, PipelineHandler};
|
2018-05-07 23:40:04 +02:00
|
|
|
use pred::Predicate;
|
2018-07-12 11:30:01 +02:00
|
|
|
use resource::Resource;
|
2018-07-15 11:12:21 +02:00
|
|
|
use router::{ResourceDef, Router};
|
2018-04-30 04:35:50 +02:00
|
|
|
use scope::Scope;
|
2018-07-04 17:01:27 +02:00
|
|
|
use server::{HttpHandler, HttpHandlerTask, IntoHttpHandler, Request};
|
2018-08-17 05:29:06 +02:00
|
|
|
use with::WithFactory;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
|
|
|
/// Application
|
2018-04-14 01:02:01 +02:00
|
|
|
pub struct HttpApplication<S = ()> {
|
2017-10-22 03:54:24 +02:00
|
|
|
state: Rc<S>,
|
|
|
|
prefix: String,
|
2018-04-10 19:13:52 +02:00
|
|
|
prefix_len: usize,
|
2018-06-21 19:06:23 +02:00
|
|
|
inner: Rc<Inner<S>>,
|
2018-06-08 04:46:38 +02:00
|
|
|
filters: Option<Vec<Box<Predicate<S>>>>,
|
2018-06-21 19:06:23 +02:00
|
|
|
middlewares: Rc<Vec<Box<Middleware<S>>>>,
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2018-06-18 01:45:54 +02:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct Inner<S> {
|
2018-07-15 11:12:21 +02:00
|
|
|
router: Router<S>,
|
2018-02-19 07:23:17 +01:00
|
|
|
encoding: ContentEncoding,
|
2017-12-29 10:01:31 +01:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-12-29 10:01:31 +01:00
|
|
|
impl<S: 'static> PipelineHandler<S> for Inner<S> {
|
2018-06-21 19:06:23 +02:00
|
|
|
#[inline]
|
2018-02-19 07:23:17 +01:00
|
|
|
fn encoding(&self) -> ContentEncoding {
|
|
|
|
self.encoding
|
|
|
|
}
|
|
|
|
|
2018-07-15 11:12:21 +02:00
|
|
|
fn handle(&self, req: &HttpRequest<S>) -> AsyncResult<HttpResponse> {
|
|
|
|
self.router.handle(req)
|
2018-04-02 19:27:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: 'static> HttpApplication<S> {
|
2018-02-19 23:27:36 +01:00
|
|
|
#[cfg(test)]
|
2018-07-04 17:01:27 +02:00
|
|
|
pub(crate) fn run(&self, req: Request) -> AsyncResult<HttpResponse> {
|
2018-07-15 11:12:21 +02:00
|
|
|
let info = self
|
|
|
|
.inner
|
|
|
|
.router
|
|
|
|
.recognize(&req, &self.state, self.prefix_len);
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = HttpRequest::new(req, Rc::clone(&self.state), info);
|
2018-04-02 19:27:37 +02:00
|
|
|
|
2018-07-15 11:12:21 +02:00
|
|
|
self.inner.handle(&req)
|
2017-12-29 10:01:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 17:51:16 +01:00
|
|
|
impl<S: 'static> HttpHandler for HttpApplication<S> {
|
2018-06-18 01:45:54 +02:00
|
|
|
type Task = Pipeline<S, Inner<S>>;
|
|
|
|
|
2018-07-04 17:01:27 +02:00
|
|
|
fn handle(&self, msg: Request) -> Result<Pipeline<S, Inner<S>>, Request> {
|
2017-12-29 23:04:13 +01:00
|
|
|
let m = {
|
2018-07-15 11:50:56 +02:00
|
|
|
if self.prefix_len == 0 {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
let path = msg.path();
|
|
|
|
path.starts_with(&self.prefix)
|
|
|
|
&& (path.len() == self.prefix_len
|
|
|
|
|| path.split_at(self.prefix_len).1.starts_with('/'))
|
|
|
|
}
|
2017-12-29 23:04:13 +01:00
|
|
|
};
|
|
|
|
if m {
|
2018-06-08 04:46:38 +02:00
|
|
|
if let Some(ref filters) = self.filters {
|
|
|
|
for filter in filters {
|
2018-06-25 06:58:04 +02:00
|
|
|
if !filter.check(&msg, &self.state) {
|
|
|
|
return Err(msg);
|
2018-06-08 04:46:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-15 11:12:21 +02:00
|
|
|
let info = self
|
|
|
|
.inner
|
|
|
|
.router
|
|
|
|
.recognize(&msg, &self.state, self.prefix_len);
|
|
|
|
|
2017-12-29 10:01:31 +01:00
|
|
|
let inner = Rc::clone(&self.inner);
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = HttpRequest::new(msg, Rc::clone(&self.state), info);
|
2018-07-15 11:12:21 +02:00
|
|
|
Ok(Pipeline::new(req, Rc::clone(&self.middlewares), inner))
|
2017-11-29 22:53:52 +01:00
|
|
|
} else {
|
2018-06-25 06:58:04 +02:00
|
|
|
Err(msg)
|
2017-11-29 22:53:52 +01:00
|
|
|
}
|
2017-10-22 07:59:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 20:00:39 +01:00
|
|
|
struct ApplicationParts<S> {
|
|
|
|
state: S,
|
|
|
|
prefix: String,
|
2018-07-15 11:12:21 +02:00
|
|
|
router: Router<S>,
|
2018-02-19 07:23:17 +01:00
|
|
|
encoding: ContentEncoding,
|
2017-12-09 13:33:40 +01:00
|
|
|
middlewares: Vec<Box<Middleware<S>>>,
|
2018-06-08 04:46:38 +02:00
|
|
|
filters: Vec<Box<Predicate<S>>>,
|
2017-12-06 20:00:39 +01:00
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
/// Structure that follows the builder pattern for building application
|
|
|
|
/// instances.
|
|
|
|
pub struct App<S = ()> {
|
2017-12-06 20:00:39 +01:00
|
|
|
parts: Option<ApplicationParts<S>>,
|
|
|
|
}
|
|
|
|
|
2018-03-31 09:16:55 +02:00
|
|
|
impl App<()> {
|
2018-04-14 01:02:01 +02:00
|
|
|
/// Create application with empty state. Application can
|
2018-04-07 17:10:36 +02:00
|
|
|
/// be configured with a builder-like pattern.
|
2018-03-31 09:16:55 +02:00
|
|
|
pub fn new() -> App<()> {
|
2018-06-13 21:31:20 +02:00
|
|
|
App::with_state(())
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-31 09:16:55 +02:00
|
|
|
impl Default for App<()> {
|
2017-12-11 23:16:29 +01:00
|
|
|
fn default() -> Self {
|
2018-03-31 09:16:55 +02:00
|
|
|
App::new()
|
2017-12-11 23:16:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
impl<S> App<S>
|
|
|
|
where
|
|
|
|
S: 'static,
|
|
|
|
{
|
2018-04-07 17:10:36 +02:00
|
|
|
/// Create application with specified state. Application can be
|
|
|
|
/// configured with a builder-like pattern.
|
2017-12-06 20:00:39 +01:00
|
|
|
///
|
2018-04-07 17:10:36 +02:00
|
|
|
/// State is shared with all resources within same application and
|
|
|
|
/// could be accessed with `HttpRequest::state()` method.
|
2018-04-17 22:59:55 +02:00
|
|
|
///
|
|
|
|
/// **Note**: http server accepts an application factory rather than
|
|
|
|
/// an application instance. Http server constructs an application
|
2018-04-18 19:49:03 +02:00
|
|
|
/// instance for each thread, thus application state must be constructed
|
|
|
|
/// multiple times. If you want to share state between different
|
|
|
|
/// threads, a shared object should be used, e.g. `Arc`. Application
|
|
|
|
/// state does not need to be `Send` and `Sync`.
|
2018-03-31 09:16:55 +02:00
|
|
|
pub fn with_state(state: S) -> App<S> {
|
|
|
|
App {
|
2017-12-06 20:00:39 +01:00
|
|
|
parts: Some(ApplicationParts {
|
2018-02-26 23:33:56 +01:00
|
|
|
state,
|
2018-07-15 11:50:56 +02:00
|
|
|
prefix: "".to_owned(),
|
2018-08-01 00:40:52 +02:00
|
|
|
router: Router::new(ResourceDef::prefix("")),
|
2017-10-22 07:59:09 +02:00
|
|
|
middlewares: Vec::new(),
|
2018-06-08 04:46:38 +02:00
|
|
|
filters: Vec::new(),
|
2018-02-19 07:23:17 +01:00
|
|
|
encoding: ContentEncoding::Auto,
|
2018-04-14 01:02:01 +02:00
|
|
|
}),
|
2017-10-15 23:17:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-26 18:05:07 +02:00
|
|
|
/// Get reference to the application state
|
|
|
|
pub fn state(&self) -> &S {
|
|
|
|
let parts = self.parts.as_ref().expect("Use after finish");
|
|
|
|
&parts.state
|
|
|
|
}
|
|
|
|
|
2018-04-07 17:10:36 +02:00
|
|
|
/// Set application prefix.
|
2017-12-11 23:16:29 +01:00
|
|
|
///
|
2018-04-07 17:10:36 +02:00
|
|
|
/// Only requests that match the application's prefix get
|
|
|
|
/// processed by this application.
|
2017-12-11 23:16:29 +01:00
|
|
|
///
|
2018-04-07 17:10:36 +02:00
|
|
|
/// The application prefix always contains a leading slash (`/`).
|
|
|
|
/// If the supplied prefix does not contain leading slash, it is
|
|
|
|
/// inserted.
|
|
|
|
///
|
|
|
|
/// Prefix should consist of valid path segments. i.e for an
|
|
|
|
/// application with the prefix `/app` any request with the paths
|
|
|
|
/// `/app`, `/app/` or `/app/test` would match, but the path
|
|
|
|
/// `/application` would not.
|
|
|
|
///
|
|
|
|
/// In the following example only requests with an `/app/` path
|
|
|
|
/// prefix get handled. Requests with path `/app/test/` would be
|
|
|
|
/// handled, while requests with the paths `/application` or
|
2018-07-24 23:51:48 +02:00
|
|
|
/// `/other/...` would return `NOT FOUND`. It is also possible to
|
|
|
|
/// handle `/app` path, to do this you can register resource for
|
|
|
|
/// empty string `""`
|
2017-12-11 23:16:29 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
2018-03-31 09:16:55 +02:00
|
|
|
/// use actix_web::{http, App, HttpResponse};
|
2017-12-11 23:16:29 +01:00
|
|
|
///
|
|
|
|
/// fn main() {
|
2018-03-31 09:16:55 +02:00
|
|
|
/// let app = App::new()
|
2017-12-11 23:16:29 +01:00
|
|
|
/// .prefix("/app")
|
2018-07-24 23:51:48 +02:00
|
|
|
/// .resource("", |r| r.f(|_| HttpResponse::Ok())) // <- handle `/app` path
|
|
|
|
/// .resource("/", |r| r.f(|_| HttpResponse::Ok())) // <- handle `/app/` path
|
2017-12-11 23:16:29 +01:00
|
|
|
/// .resource("/test", |r| {
|
2018-06-01 18:37:14 +02:00
|
|
|
/// r.get().f(|_| HttpResponse::Ok());
|
|
|
|
/// r.head().f(|_| HttpResponse::MethodNotAllowed());
|
2017-12-11 23:16:29 +01:00
|
|
|
/// })
|
|
|
|
/// .finish();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-03-31 09:16:55 +02:00
|
|
|
pub fn prefix<P: Into<String>>(mut self, prefix: P) -> App<S> {
|
2017-12-11 23:16:29 +01:00
|
|
|
{
|
|
|
|
let parts = self.parts.as_mut().expect("Use after finish");
|
|
|
|
let mut prefix = prefix.into();
|
|
|
|
if !prefix.starts_with('/') {
|
|
|
|
prefix.insert(0, '/')
|
|
|
|
}
|
2018-08-01 00:40:52 +02:00
|
|
|
parts.router.set_prefix(&prefix);
|
2017-12-11 23:16:29 +01:00
|
|
|
parts.prefix = prefix;
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-06-08 04:46:38 +02:00
|
|
|
/// Add match predicate to application.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # fn main() {
|
|
|
|
/// App::new()
|
2018-06-08 05:22:23 +02:00
|
|
|
/// .filter(pred::Host("www.rust-lang.org"))
|
2018-06-08 04:46:38 +02:00
|
|
|
/// .resource("/path", |r| r.f(|_| HttpResponse::Ok()))
|
|
|
|
/// # .finish();
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
pub fn filter<T: Predicate<S> + 'static>(mut self, p: T) -> App<S> {
|
|
|
|
{
|
|
|
|
let parts = self.parts.as_mut().expect("Use after finish");
|
|
|
|
parts.filters.push(Box::new(p));
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-07 17:10:36 +02:00
|
|
|
/// Configure route for a specific path.
|
|
|
|
///
|
|
|
|
/// This is a simplified version of the `App::resource()` method.
|
|
|
|
/// Handler functions need to accept one request extractor
|
|
|
|
/// argument.
|
2018-04-07 06:18:42 +02:00
|
|
|
///
|
2018-04-07 17:10:36 +02:00
|
|
|
/// This method could be called multiple times, in that case
|
|
|
|
/// multiple routes would be registered for same resource path.
|
2018-04-07 06:18:42 +02:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// use actix_web::{http, App, HttpRequest, HttpResponse};
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = App::new()
|
2018-06-01 18:37:14 +02:00
|
|
|
/// .route("/test", http::Method::GET, |_: HttpRequest| {
|
|
|
|
/// HttpResponse::Ok()
|
|
|
|
/// })
|
|
|
|
/// .route("/test", http::Method::POST, |_: HttpRequest| {
|
|
|
|
/// HttpResponse::MethodNotAllowed()
|
|
|
|
/// });
|
2018-04-07 06:18:42 +02:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn route<T, F, R>(mut self, path: &str, method: Method, f: F) -> App<S>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-08-17 05:29:06 +02:00
|
|
|
F: WithFactory<T, S, R>,
|
2018-04-14 01:02:01 +02:00
|
|
|
R: Responder + 'static,
|
|
|
|
T: FromRequest<S> + 'static,
|
2018-04-07 06:18:42 +02:00
|
|
|
{
|
2018-07-15 11:12:21 +02:00
|
|
|
self.parts
|
|
|
|
.as_mut()
|
|
|
|
.expect("Use after finish")
|
|
|
|
.router
|
|
|
|
.register_route(path, method, f);
|
2018-06-20 10:50:56 +02:00
|
|
|
|
2018-04-07 06:18:42 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-30 04:35:50 +02:00
|
|
|
/// Configure scope for common root path.
|
|
|
|
///
|
|
|
|
/// Scopes collect multiple paths under a common path prefix.
|
2018-05-07 22:50:43 +02:00
|
|
|
/// Scope path can contain variable path segments as resources.
|
2018-04-30 04:35:50 +02:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// use actix_web::{http, App, HttpRequest, HttpResponse};
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2018-06-01 18:37:14 +02:00
|
|
|
/// let app = App::new().scope("/{project_id}", |scope| {
|
|
|
|
/// scope
|
|
|
|
/// .resource("/path1", |r| r.f(|_| HttpResponse::Ok()))
|
|
|
|
/// .resource("/path2", |r| r.f(|_| HttpResponse::Ok()))
|
|
|
|
/// .resource("/path3", |r| r.f(|_| HttpResponse::MethodNotAllowed()))
|
|
|
|
/// });
|
2018-04-30 04:35:50 +02:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2018-04-30 04:49:26 +02:00
|
|
|
/// In the above example, three routes get added:
|
2018-05-07 22:50:43 +02:00
|
|
|
/// * /{project_id}/path1
|
|
|
|
/// * /{project_id}/path2
|
|
|
|
/// * /{project_id}/path3
|
2018-04-30 04:35:50 +02:00
|
|
|
///
|
|
|
|
pub fn scope<F>(mut self, path: &str, f: F) -> App<S>
|
|
|
|
where
|
|
|
|
F: FnOnce(Scope<S>) -> Scope<S>,
|
|
|
|
{
|
2018-07-15 11:12:21 +02:00
|
|
|
let scope = f(Scope::new(path));
|
|
|
|
self.parts
|
|
|
|
.as_mut()
|
|
|
|
.expect("Use after finish")
|
|
|
|
.router
|
|
|
|
.register_scope(scope);
|
2018-04-30 04:35:50 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-07 17:10:36 +02:00
|
|
|
/// Configure resource for a specific path.
|
2017-10-15 23:17:41 +02:00
|
|
|
///
|
2018-04-07 17:10:36 +02:00
|
|
|
/// Resources may have variable path segments. For example, a
|
|
|
|
/// resource with the path `/a/{name}/c` would match all incoming
|
|
|
|
/// requests with paths such as `/a/b/c`, `/a/1/c`, or `/a/etc/c`.
|
2017-10-22 03:54:24 +02:00
|
|
|
///
|
2018-04-07 17:10:36 +02:00
|
|
|
/// A variable segment is specified in the form `{identifier}`,
|
|
|
|
/// where the identifier can be used later in a request handler to
|
|
|
|
/// access the matched value for that segment. This is done by
|
|
|
|
/// looking up the identifier in the `Params` object returned by
|
|
|
|
/// `HttpRequest.match_info()` method.
|
2017-10-22 03:54:24 +02:00
|
|
|
///
|
2018-04-07 17:10:36 +02:00
|
|
|
/// By default, each segment matches the regular expression `[^{}/]+`.
|
2017-10-22 03:54:24 +02:00
|
|
|
///
|
|
|
|
/// You can also specify a custom regex in the form `{identifier:regex}`:
|
|
|
|
///
|
2018-04-07 17:10:36 +02:00
|
|
|
/// For instance, to route `GET`-requests on any route matching
|
|
|
|
/// `/users/{userid}/{friend}` and store `userid` and `friend` in
|
|
|
|
/// the exposed `Params` object:
|
2017-10-22 03:54:24 +02:00
|
|
|
///
|
2017-10-15 23:17:41 +02:00
|
|
|
/// ```rust
|
2017-12-06 20:00:39 +01:00
|
|
|
/// # extern crate actix_web;
|
2018-03-31 09:16:55 +02:00
|
|
|
/// use actix_web::{http, App, HttpResponse};
|
2017-10-15 23:17:41 +02:00
|
|
|
///
|
|
|
|
/// fn main() {
|
2018-06-01 18:37:14 +02:00
|
|
|
/// let app = App::new().resource("/users/{userid}/{friend}", |r| {
|
|
|
|
/// r.get().f(|_| HttpResponse::Ok());
|
|
|
|
/// r.head().f(|_| HttpResponse::MethodNotAllowed());
|
|
|
|
/// });
|
2017-10-15 23:17:41 +02:00
|
|
|
/// }
|
|
|
|
/// ```
|
2018-04-04 07:06:18 +02:00
|
|
|
pub fn resource<F, R>(mut self, path: &str, f: F) -> App<S>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-07-12 11:30:01 +02:00
|
|
|
F: FnOnce(&mut Resource<S>) -> R + 'static,
|
2017-10-15 23:17:41 +02:00
|
|
|
{
|
|
|
|
{
|
|
|
|
let parts = self.parts.as_mut().expect("Use after finish");
|
|
|
|
|
2018-07-15 11:12:21 +02:00
|
|
|
// create resource
|
|
|
|
let mut resource = Resource::new(ResourceDef::new(path));
|
2017-12-08 01:22:26 +01:00
|
|
|
|
2018-07-15 11:12:21 +02:00
|
|
|
// configure
|
|
|
|
f(&mut resource);
|
|
|
|
|
|
|
|
parts.router.register_resource(resource);
|
2017-10-15 23:17:41 +02:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-10 06:11:15 +02:00
|
|
|
/// Configure resource for a specific path.
|
|
|
|
#[doc(hidden)]
|
2018-07-15 11:12:21 +02:00
|
|
|
pub fn register_resource(&mut self, resource: Resource<S>) {
|
2018-04-14 01:02:01 +02:00
|
|
|
self.parts
|
|
|
|
.as_mut()
|
|
|
|
.expect("Use after finish")
|
2018-07-15 11:12:21 +02:00
|
|
|
.router
|
|
|
|
.register_resource(resource);
|
2018-04-10 06:11:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Default resource to be used if no matching route could be found.
|
2018-04-04 07:06:18 +02:00
|
|
|
pub fn default_resource<F, R>(mut self, f: F) -> App<S>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2018-07-12 11:30:01 +02:00
|
|
|
F: FnOnce(&mut Resource<S>) -> R + 'static,
|
2017-10-15 23:17:41 +02:00
|
|
|
{
|
2018-07-15 11:12:21 +02:00
|
|
|
// create and configure default resource
|
|
|
|
let mut resource = Resource::new(ResourceDef::new(""));
|
|
|
|
f(&mut resource);
|
|
|
|
|
|
|
|
self.parts
|
|
|
|
.as_mut()
|
|
|
|
.expect("Use after finish")
|
|
|
|
.router
|
|
|
|
.register_default_resource(resource.into());
|
|
|
|
|
2017-10-15 23:17:41 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-02-19 07:23:17 +01:00
|
|
|
/// Set default content encoding. `ContentEncoding::Auto` is set by default.
|
2018-04-14 01:02:01 +02:00
|
|
|
pub fn default_encoding(mut self, encoding: ContentEncoding) -> App<S> {
|
2018-02-19 07:23:17 +01:00
|
|
|
{
|
|
|
|
let parts = self.parts.as_mut().expect("Use after finish");
|
|
|
|
parts.encoding = encoding;
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-07 17:10:36 +02:00
|
|
|
/// Register an external resource.
|
2017-12-08 01:22:26 +01:00
|
|
|
///
|
2018-04-07 17:10:36 +02:00
|
|
|
/// External resources are useful for URL generation purposes only
|
|
|
|
/// and are never considered for matching at request time. Calls to
|
|
|
|
/// `HttpRequest::url_for()` will work as expected.
|
2017-12-08 01:22:26 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
2018-03-31 09:16:55 +02:00
|
|
|
/// use actix_web::{App, HttpRequest, HttpResponse, Result};
|
2017-12-08 01:22:26 +01:00
|
|
|
///
|
2018-06-25 06:58:04 +02:00
|
|
|
/// fn index(req: &HttpRequest) -> Result<HttpResponse> {
|
2018-06-01 18:37:14 +02:00
|
|
|
/// let url = req.url_for("youtube", &["oHg5SJYRHA0"])?;
|
|
|
|
/// assert_eq!(url.as_str(), "https://youtube.com/watch/oHg5SJYRHA0");
|
|
|
|
/// Ok(HttpResponse::Ok().into())
|
2017-12-08 01:22:26 +01:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2018-03-31 09:16:55 +02:00
|
|
|
/// let app = App::new()
|
2018-04-10 19:57:53 +02:00
|
|
|
/// .resource("/index.html", |r| r.get().f(index))
|
2017-12-08 01:22:26 +01:00
|
|
|
/// .external_resource("youtube", "https://youtube.com/watch/{video_id}")
|
|
|
|
/// .finish();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-03-31 09:16:55 +02:00
|
|
|
pub fn external_resource<T, U>(mut self, name: T, url: U) -> App<S>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
T: AsRef<str>,
|
|
|
|
U: AsRef<str>,
|
2017-12-08 01:22:26 +01:00
|
|
|
{
|
2018-07-15 11:12:21 +02:00
|
|
|
self.parts
|
|
|
|
.as_mut()
|
|
|
|
.expect("Use after finish")
|
|
|
|
.router
|
|
|
|
.register_external(name.as_ref(), ResourceDef::external(url.as_ref()));
|
2017-12-08 01:22:26 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-01-02 22:09:02 +01:00
|
|
|
/// Configure handler for specific path prefix.
|
|
|
|
///
|
2018-04-07 17:10:36 +02:00
|
|
|
/// A path prefix consists of valid path segments, i.e for the
|
|
|
|
/// prefix `/app` any request with the paths `/app`, `/app/` or
|
|
|
|
/// `/app/test` would match, but the path `/application` would
|
|
|
|
/// not.
|
2018-01-02 22:09:02 +01:00
|
|
|
///
|
2018-04-17 21:55:13 +02:00
|
|
|
/// Path tail is available as `tail` parameter in request's match_dict.
|
|
|
|
///
|
2018-01-02 22:09:02 +01:00
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
2018-03-31 09:16:55 +02:00
|
|
|
/// use actix_web::{http, App, HttpRequest, HttpResponse};
|
2018-01-02 22:09:02 +01:00
|
|
|
///
|
|
|
|
/// fn main() {
|
2018-06-25 06:58:04 +02:00
|
|
|
/// let app = App::new().handler("/app", |req: &HttpRequest| match *req.method() {
|
2018-06-01 18:37:14 +02:00
|
|
|
/// http::Method::GET => HttpResponse::Ok(),
|
|
|
|
/// http::Method::POST => HttpResponse::MethodNotAllowed(),
|
|
|
|
/// _ => HttpResponse::NotFound(),
|
|
|
|
/// });
|
2018-01-02 22:09:02 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
2018-04-14 01:02:01 +02:00
|
|
|
pub fn handler<H: Handler<S>>(mut self, path: &str, handler: H) -> App<S> {
|
2018-01-02 22:09:02 +01:00
|
|
|
{
|
2018-04-10 19:13:52 +02:00
|
|
|
let mut path = path.trim().trim_right_matches('/').to_owned();
|
|
|
|
if !path.is_empty() && !path.starts_with('/') {
|
|
|
|
path.insert(0, '/')
|
|
|
|
}
|
2018-05-07 22:50:43 +02:00
|
|
|
if path.len() > 1 && path.ends_with('/') {
|
|
|
|
path.pop();
|
|
|
|
}
|
2018-07-15 11:12:21 +02:00
|
|
|
self.parts
|
|
|
|
.as_mut()
|
|
|
|
.expect("Use after finish")
|
|
|
|
.router
|
|
|
|
.register_handler(&path, Box::new(WrapHandler::new(handler)), None);
|
2018-01-02 22:09:02 +01:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-07 17:10:36 +02:00
|
|
|
/// Register a middleware.
|
2018-03-31 09:16:55 +02:00
|
|
|
pub fn middleware<M: Middleware<S>>(mut self, mw: M) -> App<S> {
|
2018-04-29 18:09:08 +02:00
|
|
|
self.parts
|
|
|
|
.as_mut()
|
|
|
|
.expect("Use after finish")
|
|
|
|
.middlewares
|
|
|
|
.push(Box::new(mw));
|
2017-10-22 07:59:09 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-07 17:10:36 +02:00
|
|
|
/// Run external configuration as part of the application building
|
|
|
|
/// process
|
2018-03-27 20:16:02 +02:00
|
|
|
///
|
2018-04-07 17:10:36 +02:00
|
|
|
/// This function is useful for moving parts of configuration to a
|
|
|
|
/// different module or event library. For example we can move
|
|
|
|
/// some of the resources' configuration to different module.
|
2018-03-27 20:16:02 +02:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
2018-06-01 18:37:14 +02:00
|
|
|
/// use actix_web::{fs, middleware, App, HttpResponse};
|
2018-03-27 20:16:02 +02:00
|
|
|
///
|
|
|
|
/// // this function could be located in different module
|
2018-03-31 09:16:55 +02:00
|
|
|
/// fn config(app: App) -> App {
|
2018-06-01 18:37:14 +02:00
|
|
|
/// app.resource("/test", |r| {
|
|
|
|
/// r.get().f(|_| HttpResponse::Ok());
|
|
|
|
/// r.head().f(|_| HttpResponse::MethodNotAllowed());
|
|
|
|
/// })
|
2018-03-27 20:16:02 +02:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2018-03-31 09:16:55 +02:00
|
|
|
/// let app = App::new()
|
2018-03-27 20:16:02 +02:00
|
|
|
/// .middleware(middleware::Logger::default())
|
|
|
|
/// .configure(config) // <- register resources
|
2018-07-05 11:34:13 +02:00
|
|
|
/// .handler("/static", fs::StaticFiles::new(".").unwrap());
|
2018-03-27 20:16:02 +02:00
|
|
|
/// }
|
|
|
|
/// ```
|
2018-03-31 09:16:55 +02:00
|
|
|
pub fn configure<F>(self, cfg: F) -> App<S>
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
F: Fn(App<S>) -> App<S>,
|
2018-03-27 20:16:02 +02:00
|
|
|
{
|
|
|
|
cfg(self)
|
|
|
|
}
|
|
|
|
|
2018-04-07 17:10:36 +02:00
|
|
|
/// Finish application configuration and create `HttpHandler` object.
|
2017-12-06 20:00:39 +01:00
|
|
|
pub fn finish(&mut self) -> HttpApplication<S> {
|
2018-06-21 10:17:27 +02:00
|
|
|
let mut parts = self.parts.take().expect("Use after finish");
|
2017-12-07 01:26:27 +01:00
|
|
|
let prefix = parts.prefix.trim().trim_right_matches('/');
|
2018-07-15 11:12:21 +02:00
|
|
|
parts.router.finish();
|
2017-12-29 10:01:31 +01:00
|
|
|
|
2018-06-21 19:06:23 +02:00
|
|
|
let inner = Rc::new(Inner {
|
2018-07-15 11:12:21 +02:00
|
|
|
router: parts.router,
|
2018-04-14 01:02:01 +02:00
|
|
|
encoding: parts.encoding,
|
2018-06-21 19:06:23 +02:00
|
|
|
});
|
2018-06-08 04:46:38 +02:00
|
|
|
let filters = if parts.filters.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(parts.filters)
|
|
|
|
};
|
2017-12-29 10:01:31 +01:00
|
|
|
|
2017-12-06 20:00:39 +01:00
|
|
|
HttpApplication {
|
2018-02-26 23:33:56 +01:00
|
|
|
inner,
|
2018-06-08 04:46:38 +02:00
|
|
|
filters,
|
2018-07-15 11:50:56 +02:00
|
|
|
state: Rc::new(parts.state),
|
|
|
|
middlewares: Rc::new(parts.middlewares),
|
|
|
|
prefix: prefix.to_owned(),
|
|
|
|
prefix_len: prefix.len(),
|
2017-10-22 07:59:09 +02:00
|
|
|
}
|
2017-10-15 23:17:41 +02:00
|
|
|
}
|
2018-01-14 03:58:17 +01:00
|
|
|
|
2018-04-07 17:10:36 +02:00
|
|
|
/// Convenience method for creating `Box<HttpHandler>` instances.
|
2018-01-14 03:58:17 +01:00
|
|
|
///
|
2018-04-07 17:10:36 +02:00
|
|
|
/// This method is useful if you need to register multiple
|
|
|
|
/// application instances with different state.
|
2018-01-14 03:58:17 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use std::thread;
|
|
|
|
/// # extern crate actix_web;
|
2018-04-06 18:45:10 +02:00
|
|
|
/// use actix_web::{server, App, HttpResponse};
|
2018-01-14 03:58:17 +01:00
|
|
|
///
|
|
|
|
/// struct State1;
|
|
|
|
///
|
|
|
|
/// struct State2;
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2018-06-01 19:27:23 +02:00
|
|
|
/// # thread::spawn(|| {
|
2018-06-01 18:37:14 +02:00
|
|
|
/// server::new(|| {
|
|
|
|
/// vec![
|
|
|
|
/// App::with_state(State1)
|
|
|
|
/// .prefix("/app1")
|
|
|
|
/// .resource("/", |r| r.f(|r| HttpResponse::Ok()))
|
|
|
|
/// .boxed(),
|
|
|
|
/// App::with_state(State2)
|
|
|
|
/// .prefix("/app2")
|
|
|
|
/// .resource("/", |r| r.f(|r| HttpResponse::Ok()))
|
|
|
|
/// .boxed(),
|
|
|
|
/// ]
|
|
|
|
/// }).bind("127.0.0.1:8080")
|
|
|
|
/// .unwrap()
|
2018-01-14 03:58:17 +01:00
|
|
|
/// .run()
|
2018-06-01 19:27:23 +02:00
|
|
|
/// # });
|
2018-01-14 03:58:17 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
2018-06-18 01:45:54 +02:00
|
|
|
pub fn boxed(mut self) -> Box<HttpHandler<Task = Box<HttpHandlerTask>>> {
|
|
|
|
Box::new(BoxedApplication { app: self.finish() })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct BoxedApplication<S> {
|
|
|
|
app: HttpApplication<S>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: 'static> HttpHandler for BoxedApplication<S> {
|
|
|
|
type Task = Box<HttpHandlerTask>;
|
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
fn handle(&self, req: Request) -> Result<Self::Task, Request> {
|
2018-06-18 01:45:54 +02:00
|
|
|
self.app.handle(req).map(|t| {
|
|
|
|
let task: Self::Task = Box::new(t);
|
|
|
|
task
|
|
|
|
})
|
2018-01-14 03:58:17 +01:00
|
|
|
}
|
2017-10-15 23:17:41 +02:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2018-03-31 09:16:55 +02:00
|
|
|
impl<S: 'static> IntoHttpHandler for App<S> {
|
2017-12-06 20:00:39 +01:00
|
|
|
type Handler = HttpApplication<S>;
|
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
fn into_handler(mut self) -> HttpApplication<S> {
|
2017-12-06 20:00:39 +01:00
|
|
|
self.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-31 09:16:55 +02:00
|
|
|
impl<'a, S: 'static> IntoHttpHandler for &'a mut App<S> {
|
2017-12-06 20:00:39 +01:00
|
|
|
type Handler = HttpApplication<S>;
|
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
fn into_handler(self) -> HttpApplication<S> {
|
2017-12-06 20:00:39 +01:00
|
|
|
self.finish()
|
2017-10-22 03:54:24 +02:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-12-06 20:00:39 +01:00
|
|
|
#[doc(hidden)]
|
2018-03-31 09:16:55 +02:00
|
|
|
impl<S: 'static> Iterator for App<S> {
|
2017-12-06 20:00:39 +01:00
|
|
|
type Item = HttpApplication<S>;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-10-22 03:54:24 +02:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.parts.is_some() {
|
|
|
|
Some(self.finish())
|
2017-10-07 06:48:14 +02:00
|
|
|
} else {
|
2017-10-22 03:54:24 +02:00
|
|
|
None
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-06 22:02:53 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-06-13 21:31:20 +02:00
|
|
|
use body::{Binary, Body};
|
2018-04-14 01:02:01 +02:00
|
|
|
use http::StatusCode;
|
2017-12-06 22:02:53 +01:00
|
|
|
use httprequest::HttpRequest;
|
2018-03-31 08:07:33 +02:00
|
|
|
use httpresponse::HttpResponse;
|
2018-06-08 04:46:38 +02:00
|
|
|
use pred;
|
|
|
|
use test::{TestRequest, TestServer};
|
2017-12-06 22:02:53 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_default_resource() {
|
2018-06-21 19:06:23 +02:00
|
|
|
let app = App::new()
|
2018-04-29 18:09:08 +02:00
|
|
|
.resource("/test", |r| r.f(|_| HttpResponse::Ok()))
|
|
|
|
.finish();
|
2017-12-06 22:02:53 +01:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/test").request();
|
2017-12-09 22:25:06 +01:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
2017-12-06 22:02:53 +01:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/blah").request();
|
2017-12-09 22:25:06 +01:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
|
2017-12-06 22:02:53 +01:00
|
|
|
|
2018-06-21 19:06:23 +02:00
|
|
|
let app = App::new()
|
2018-07-21 14:58:08 +02:00
|
|
|
.resource("/test", |r| r.f(|_| HttpResponse::Ok()))
|
2018-03-31 08:07:33 +02:00
|
|
|
.default_resource(|r| r.f(|_| HttpResponse::MethodNotAllowed()))
|
2017-12-06 22:02:53 +01:00
|
|
|
.finish();
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/blah").request();
|
2017-12-09 22:25:06 +01:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::METHOD_NOT_ALLOWED);
|
2017-12-06 22:02:53 +01:00
|
|
|
}
|
2017-12-07 03:39:13 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unhandled_prefix() {
|
2018-06-21 19:06:23 +02:00
|
|
|
let app = App::new()
|
2017-12-11 23:16:29 +01:00
|
|
|
.prefix("/test")
|
2018-03-31 08:07:33 +02:00
|
|
|
.resource("/test", |r| r.f(|_| HttpResponse::Ok()))
|
2017-12-07 03:39:13 +01:00
|
|
|
.finish();
|
2018-06-25 06:58:04 +02:00
|
|
|
let ctx = TestRequest::default().request();
|
|
|
|
assert!(app.handle(ctx).is_err());
|
2017-12-07 03:39:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_state() {
|
2018-06-21 19:06:23 +02:00
|
|
|
let app = App::with_state(10)
|
2018-04-29 18:09:08 +02:00
|
|
|
.resource("/", |r| r.f(|_| HttpResponse::Ok()))
|
|
|
|
.finish();
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_state(10).request();
|
2017-12-09 22:25:06 +01:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
2017-12-07 03:39:13 +01:00
|
|
|
}
|
2017-12-29 23:04:13 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_prefix() {
|
2018-06-21 19:06:23 +02:00
|
|
|
let app = App::new()
|
2017-12-29 23:04:13 +01:00
|
|
|
.prefix("/test")
|
2018-03-31 08:07:33 +02:00
|
|
|
.resource("/blah", |r| r.f(|_| HttpResponse::Ok()))
|
2017-12-29 23:04:13 +01:00
|
|
|
.finish();
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/test").request();
|
2017-12-29 23:04:13 +01:00
|
|
|
let resp = app.handle(req);
|
|
|
|
assert!(resp.is_ok());
|
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/test/").request();
|
2017-12-29 23:04:13 +01:00
|
|
|
let resp = app.handle(req);
|
|
|
|
assert!(resp.is_ok());
|
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/test/blah").request();
|
2018-01-02 22:09:02 +01:00
|
|
|
let resp = app.handle(req);
|
|
|
|
assert!(resp.is_ok());
|
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/testing").request();
|
2017-12-29 23:04:13 +01:00
|
|
|
let resp = app.handle(req);
|
|
|
|
assert!(resp.is_err());
|
|
|
|
}
|
2018-01-02 22:09:02 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_handler() {
|
2018-06-25 06:58:04 +02:00
|
|
|
let app = App::new()
|
|
|
|
.handler("/test", |_: &_| HttpResponse::Ok())
|
|
|
|
.finish();
|
2018-01-02 22:09:02 +01:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/test").request();
|
2018-01-02 22:09:02 +01:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
2018-01-02 22:09:02 +01:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/test/").request();
|
2018-01-02 22:09:02 +01:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
2018-01-02 22:09:02 +01:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/test/app").request();
|
2018-01-02 22:09:02 +01:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
2018-01-02 22:09:02 +01:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/testapp").request();
|
2018-01-02 22:09:02 +01:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
|
2018-01-02 22:09:02 +01:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/blah").request();
|
2018-01-02 22:09:02 +01:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
|
2018-01-03 00:23:31 +01:00
|
|
|
}
|
|
|
|
|
2018-04-10 19:13:52 +02:00
|
|
|
#[test]
|
|
|
|
fn test_handler2() {
|
2018-06-25 06:58:04 +02:00
|
|
|
let app = App::new()
|
|
|
|
.handler("test", |_: &_| HttpResponse::Ok())
|
|
|
|
.finish();
|
2018-04-10 19:13:52 +02:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/test").request();
|
2018-04-10 19:13:52 +02:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
2018-04-10 19:13:52 +02:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/test/").request();
|
2018-04-10 19:13:52 +02:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
2018-04-10 19:13:52 +02:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/test/app").request();
|
2018-04-10 19:13:52 +02:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
2018-04-10 19:13:52 +02:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/testapp").request();
|
2018-04-10 19:13:52 +02:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
|
2018-04-10 19:13:52 +02:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/blah").request();
|
2018-04-10 19:13:52 +02:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
|
2018-04-10 19:13:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_handler_with_prefix() {
|
2018-06-21 19:06:23 +02:00
|
|
|
let app = App::new()
|
2018-04-10 19:13:52 +02:00
|
|
|
.prefix("prefix")
|
2018-06-25 06:58:04 +02:00
|
|
|
.handler("/test", |_: &_| HttpResponse::Ok())
|
2018-04-10 19:13:52 +02:00
|
|
|
.finish();
|
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/prefix/test").request();
|
2018-04-10 19:13:52 +02:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
2018-04-10 19:13:52 +02:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/prefix/test/").request();
|
2018-04-10 19:13:52 +02:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
2018-04-10 19:13:52 +02:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/prefix/test/app").request();
|
2018-04-10 19:13:52 +02:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
2018-04-10 19:13:52 +02:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/prefix/testapp").request();
|
2018-04-10 19:13:52 +02:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
|
2018-04-10 19:13:52 +02:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/prefix/blah").request();
|
2018-04-10 19:13:52 +02:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
|
2018-04-10 19:13:52 +02:00
|
|
|
}
|
|
|
|
|
2018-04-07 06:18:42 +02:00
|
|
|
#[test]
|
|
|
|
fn test_route() {
|
2018-06-21 19:06:23 +02:00
|
|
|
let app = App::new()
|
2018-06-18 01:45:54 +02:00
|
|
|
.route("/test", Method::GET, |_: HttpRequest| HttpResponse::Ok())
|
2018-04-29 18:09:08 +02:00
|
|
|
.route("/test", Method::POST, |_: HttpRequest| {
|
|
|
|
HttpResponse::Created()
|
2018-08-23 18:48:01 +02:00
|
|
|
}).finish();
|
2018-04-07 06:18:42 +02:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/test").method(Method::GET).request();
|
2018-04-07 06:18:42 +02:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
2018-04-07 06:18:42 +02:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/test")
|
|
|
|
.method(Method::POST)
|
|
|
|
.request();
|
2018-04-07 06:18:42 +02:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::CREATED);
|
2018-04-07 06:18:42 +02:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/test")
|
|
|
|
.method(Method::HEAD)
|
|
|
|
.request();
|
2018-04-07 06:18:42 +02:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
|
2018-04-07 06:18:42 +02:00
|
|
|
}
|
|
|
|
|
2018-01-03 00:23:31 +01:00
|
|
|
#[test]
|
|
|
|
fn test_handler_prefix() {
|
2018-06-21 19:06:23 +02:00
|
|
|
let app = App::new()
|
2018-04-29 18:09:08 +02:00
|
|
|
.prefix("/app")
|
2018-06-25 06:58:04 +02:00
|
|
|
.handler("/test", |_: &_| HttpResponse::Ok())
|
2018-04-29 18:09:08 +02:00
|
|
|
.finish();
|
2018-01-03 00:23:31 +01:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/test").request();
|
2018-01-03 00:23:31 +01:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
|
2018-01-03 00:23:31 +01:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/app/test").request();
|
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
2018-01-03 00:23:31 +01:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/app/test/").request();
|
2018-01-03 00:23:31 +01:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
2018-01-03 00:23:31 +01:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/app/test/app").request();
|
2018-01-03 00:23:31 +01:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
2018-01-03 00:23:31 +01:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/app/testapp").request();
|
2018-01-03 00:23:31 +01:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
|
2018-01-03 00:23:31 +01:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::with_uri("/app/blah").request();
|
2018-01-03 00:23:31 +01:00
|
|
|
let resp = app.run(req);
|
2018-05-02 02:19:15 +02:00
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
|
2018-01-02 22:09:02 +01:00
|
|
|
}
|
2018-06-08 04:46:38 +02:00
|
|
|
|
2018-07-24 23:51:48 +02:00
|
|
|
#[test]
|
|
|
|
fn test_option_responder() {
|
|
|
|
let app = App::new()
|
|
|
|
.resource("/none", |r| r.f(|_| -> Option<&'static str> { None }))
|
|
|
|
.resource("/some", |r| r.f(|_| Some("some")))
|
|
|
|
.finish();
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/none").request();
|
|
|
|
let resp = app.run(req);
|
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
|
|
|
|
|
|
|
|
let req = TestRequest::with_uri("/some").request();
|
|
|
|
let resp = app.run(req);
|
|
|
|
assert_eq!(resp.as_msg().status(), StatusCode::OK);
|
|
|
|
assert_eq!(resp.as_msg().body(), &Body::Binary(Binary::Slice(b"some")));
|
|
|
|
}
|
|
|
|
|
2018-06-08 04:46:38 +02:00
|
|
|
#[test]
|
|
|
|
fn test_filter() {
|
|
|
|
let mut srv = TestServer::with_factory(|| {
|
|
|
|
App::new()
|
|
|
|
.filter(pred::Get())
|
2018-06-25 06:58:04 +02:00
|
|
|
.handler("/test", |_: &_| HttpResponse::Ok())
|
2018-06-08 04:46:38 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
let request = srv.get().uri(srv.url("/test")).finish().unwrap();
|
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let request = srv.post().uri(srv.url("/test")).finish().unwrap();
|
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::NOT_FOUND);
|
|
|
|
}
|
2018-06-11 13:05:41 +02:00
|
|
|
|
|
|
|
#[test]
|
2018-07-24 23:51:48 +02:00
|
|
|
fn test_prefix_root() {
|
|
|
|
let mut srv = TestServer::with_factory(|| {
|
|
|
|
App::new()
|
|
|
|
.prefix("/test")
|
|
|
|
.resource("/", |r| r.f(|_| HttpResponse::Ok()))
|
|
|
|
.resource("", |r| r.f(|_| HttpResponse::Created()))
|
|
|
|
});
|
2018-06-11 13:05:41 +02:00
|
|
|
|
2018-07-24 23:51:48 +02:00
|
|
|
let request = srv.get().uri(srv.url("/test/")).finish().unwrap();
|
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::OK);
|
2018-06-11 13:05:41 +02:00
|
|
|
|
2018-07-24 23:51:48 +02:00
|
|
|
let request = srv.get().uri(srv.url("/test")).finish().unwrap();
|
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::CREATED);
|
2018-06-11 13:05:41 +02:00
|
|
|
}
|
2018-07-24 23:51:48 +02:00
|
|
|
|
2017-12-06 22:02:53 +01:00
|
|
|
}
|