mirror of
https://github.com/fafhrd91/actix-web
synced 2025-09-02 09:36:39 +02:00
simplify http response construction; deprecate httpcodes
This commit is contained in:
@@ -178,14 +178,14 @@ impl<S> Application<S> where S: 'static {
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{Application, http, httpcodes};
|
||||
/// use actix_web::{http, Application, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new()
|
||||
/// .prefix("/app")
|
||||
/// .resource("/test", |r| {
|
||||
/// r.method(http::Method::GET).f(|_| httpcodes::HttpOk);
|
||||
/// r.method(http::Method::HEAD).f(|_| httpcodes::HttpMethodNotAllowed);
|
||||
/// r.method(http::Method::GET).f(|_| HttpResponse::Ok());
|
||||
/// r.method(http::Method::HEAD).f(|_| HttpResponse::MethodNotAllowed());
|
||||
/// })
|
||||
/// .finish();
|
||||
/// }
|
||||
@@ -222,13 +222,13 @@ impl<S> Application<S> where S: 'static {
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{Application, http, httpcodes};
|
||||
/// use actix_web::{http, Application, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new()
|
||||
/// .resource("/test", |r| {
|
||||
/// r.method(http::Method::GET).f(|_| httpcodes::HttpOk);
|
||||
/// r.method(http::Method::HEAD).f(|_| httpcodes::HttpMethodNotAllowed);
|
||||
/// r.method(http::Method::GET).f(|_| HttpResponse::Ok());
|
||||
/// r.method(http::Method::HEAD).f(|_| HttpResponse::MethodNotAllowed());
|
||||
/// });
|
||||
/// }
|
||||
/// ```
|
||||
@@ -277,12 +277,12 @@ impl<S> Application<S> where S: 'static {
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{Application, HttpRequest, HttpResponse, Result, httpcodes};
|
||||
/// use actix_web::{Application, HttpRequest, HttpResponse, Result};
|
||||
///
|
||||
/// fn index(mut req: HttpRequest) -> Result<HttpResponse> {
|
||||
/// let url = req.url_for("youtube", &["oHg5SJYRHA0"])?;
|
||||
/// assert_eq!(url.as_str(), "https://youtube.com/watch/oHg5SJYRHA0");
|
||||
/// Ok(httpcodes::HttpOk.into())
|
||||
/// Ok(HttpResponse::Ok().into())
|
||||
/// }
|
||||
///
|
||||
/// fn main() {
|
||||
@@ -315,15 +315,15 @@ impl<S> Application<S> where S: 'static {
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{Application, HttpRequest, http, httpcodes};
|
||||
/// use actix_web::{http, Application, HttpRequest, HttpResponse};
|
||||
///
|
||||
/// fn main() {
|
||||
/// let app = Application::new()
|
||||
/// .handler("/app", |req: HttpRequest| {
|
||||
/// match *req.method() {
|
||||
/// http::Method::GET => httpcodes::HttpOk,
|
||||
/// http::Method::POST => httpcodes::HttpMethodNotAllowed,
|
||||
/// _ => httpcodes::HttpNotFound,
|
||||
/// http::Method::GET => HttpResponse::Ok(),
|
||||
/// http::Method::POST => HttpResponse::MethodNotAllowed(),
|
||||
/// _ => HttpResponse::NotFound(),
|
||||
/// }});
|
||||
/// }
|
||||
/// ```
|
||||
@@ -352,14 +352,14 @@ impl<S> Application<S> where S: 'static {
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// use actix_web::{Application, http, httpcodes, fs, middleware};
|
||||
/// use actix_web::{Application, HttpResponse, http, fs, middleware};
|
||||
///
|
||||
/// // this function could be located in different module
|
||||
/// fn config(app: Application) -> Application {
|
||||
/// app
|
||||
/// .resource("/test", |r| {
|
||||
/// r.method(http::Method::GET).f(|_| httpcodes::HttpOk);
|
||||
/// r.method(http::Method::HEAD).f(|_| httpcodes::HttpMethodNotAllowed);
|
||||
/// r.method(http::Method::GET).f(|_| HttpResponse::Ok());
|
||||
/// r.method(http::Method::HEAD).f(|_| HttpResponse::MethodNotAllowed());
|
||||
/// })
|
||||
/// }
|
||||
///
|
||||
@@ -427,11 +427,11 @@ impl<S> Application<S> where S: 'static {
|
||||
/// HttpServer::new(|| { vec![
|
||||
/// Application::with_state(State1)
|
||||
/// .prefix("/app1")
|
||||
/// .resource("/", |r| r.h(httpcodes::HttpOk))
|
||||
/// .resource("/", |r| r.f(|r| HttpResponse::Ok()))
|
||||
/// .boxed(),
|
||||
/// Application::with_state(State2)
|
||||
/// .prefix("/app2")
|
||||
/// .resource("/", |r| r.h(httpcodes::HttpOk))
|
||||
/// .resource("/", |r| r.f(|r| HttpResponse::Ok()))
|
||||
/// .boxed() ]})
|
||||
/// .bind("127.0.0.1:8080").unwrap()
|
||||
/// .run()
|
||||
@@ -487,12 +487,12 @@ mod tests {
|
||||
use super::*;
|
||||
use test::TestRequest;
|
||||
use httprequest::HttpRequest;
|
||||
use httpcodes;
|
||||
use httpresponse::HttpResponse;
|
||||
|
||||
#[test]
|
||||
fn test_default_resource() {
|
||||
let mut app = Application::new()
|
||||
.resource("/test", |r| r.h(httpcodes::HttpOk))
|
||||
.resource("/test", |r| r.f(|_| HttpResponse::Ok()))
|
||||
.finish();
|
||||
|
||||
let req = TestRequest::with_uri("/test").finish();
|
||||
@@ -504,7 +504,7 @@ mod tests {
|
||||
assert_eq!(resp.as_response().unwrap().status(), StatusCode::NOT_FOUND);
|
||||
|
||||
let mut app = Application::new()
|
||||
.default_resource(|r| r.h(httpcodes::HttpMethodNotAllowed))
|
||||
.default_resource(|r| r.f(|_| HttpResponse::MethodNotAllowed()))
|
||||
.finish();
|
||||
let req = TestRequest::with_uri("/blah").finish();
|
||||
let resp = app.run(req);
|
||||
@@ -515,7 +515,7 @@ mod tests {
|
||||
fn test_unhandled_prefix() {
|
||||
let mut app = Application::new()
|
||||
.prefix("/test")
|
||||
.resource("/test", |r| r.h(httpcodes::HttpOk))
|
||||
.resource("/test", |r| r.f(|_| HttpResponse::Ok()))
|
||||
.finish();
|
||||
assert!(app.handle(HttpRequest::default()).is_err());
|
||||
}
|
||||
@@ -523,7 +523,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_state() {
|
||||
let mut app = Application::with_state(10)
|
||||
.resource("/", |r| r.h(httpcodes::HttpOk))
|
||||
.resource("/", |r| r.f(|_| HttpResponse::Ok()))
|
||||
.finish();
|
||||
let req = HttpRequest::default().with_state(Rc::clone(&app.state), app.router.clone());
|
||||
let resp = app.run(req);
|
||||
@@ -534,7 +534,7 @@ mod tests {
|
||||
fn test_prefix() {
|
||||
let mut app = Application::new()
|
||||
.prefix("/test")
|
||||
.resource("/blah", |r| r.h(httpcodes::HttpOk))
|
||||
.resource("/blah", |r| r.f(|_| HttpResponse::Ok()))
|
||||
.finish();
|
||||
let req = TestRequest::with_uri("/test").finish();
|
||||
let resp = app.handle(req);
|
||||
@@ -556,7 +556,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_handler() {
|
||||
let mut app = Application::new()
|
||||
.handler("/test", httpcodes::HttpOk)
|
||||
.handler("/test", |_| HttpResponse::Ok())
|
||||
.finish();
|
||||
|
||||
let req = TestRequest::with_uri("/test").finish();
|
||||
@@ -584,7 +584,7 @@ mod tests {
|
||||
fn test_handler_prefix() {
|
||||
let mut app = Application::new()
|
||||
.prefix("/app")
|
||||
.handler("/test", httpcodes::HttpOk)
|
||||
.handler("/test", |_| HttpResponse::Ok())
|
||||
.finish();
|
||||
|
||||
let req = TestRequest::with_uri("/test").finish();
|
||||
|
Reference in New Issue
Block a user