1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-18 05:41:50 +01:00

consistent naming

This commit is contained in:
Nikolay Kim 2017-11-26 22:31:29 -08:00
parent 45433f71e5
commit 0519056199
10 changed files with 25 additions and 24 deletions

View File

@ -36,7 +36,7 @@ fn index_async(req: HttpRequest) -> Once<actix_web::Frame, Error>
{ {
println!("{:?}", req); println!("{:?}", req);
once(Ok(HttpResponse::builder(StatusCode::OK) once(Ok(HttpResponse::build(StatusCode::OK)
.content_type("text/html") .content_type("text/html")
.body(format!("Hello {}!", req.match_info().get("name").unwrap())) .body(format!("Hello {}!", req.match_info().get("name").unwrap()))
.unwrap() .unwrap()
@ -48,7 +48,7 @@ fn with_param(req: HttpRequest) -> Result<HttpResponse>
{ {
println!("{:?}", req); println!("{:?}", req);
Ok(HttpResponse::builder(StatusCode::OK) Ok(HttpResponse::build(StatusCode::OK)
.content_type("test/plain") .content_type("test/plain")
.body(format!("Hello {}!", req.match_info().get("name").unwrap()))?) .body(format!("Hello {}!", req.match_info().get("name").unwrap()))?)
} }
@ -79,7 +79,7 @@ fn main() {
println!("{:?}", req); println!("{:?}", req);
Ok(httpcodes::HTTPFound Ok(httpcodes::HTTPFound
.builder() .build()
.header("LOCATION", "/index.html") .header("LOCATION", "/index.html")
.body(Body::Empty)?) .body(Body::Empty)?)
})) }))

View File

@ -12,7 +12,7 @@ use actix_web::*;
fn index(req: HttpRequest) -> HttpResponse { fn index(req: HttpRequest) -> HttpResponse {
println!("{:?}", req); println!("{:?}", req);
httpcodes::HTTPOk httpcodes::HTTPOk
.builder() .build()
.content_type("text/plain") .content_type("text/plain")
.body("Welcome!").unwrap() .body("Welcome!").unwrap()
} }
@ -38,7 +38,7 @@ fn main() {
// with path parameters // with path parameters
.resource("/", |r| r.handler(Method::GET, |req| { .resource("/", |r| r.handler(Method::GET, |req| {
Ok(httpcodes::HTTPFound Ok(httpcodes::HTTPFound
.builder() .build()
.header("LOCATION", "/index.html") .header("LOCATION", "/index.html")
.body(Body::Empty)?) .body(Body::Empty)?)
}))) })))

View File

@ -212,7 +212,7 @@ fn main() {
.resource("/", |r| .resource("/", |r|
r.handler(Method::GET, |req| { r.handler(Method::GET, |req| {
Ok(httpcodes::HTTPFound Ok(httpcodes::HTTPFound
.builder() .build()
.header("LOCATION", "/static/websocket.html") .header("LOCATION", "/static/websocket.html")
.body(Body::Empty)?) .body(Body::Empty)?)
})) }))

View File

@ -325,7 +325,7 @@ impl ErrorResponse for WsHandshakeError {
match *self { match *self {
WsHandshakeError::GetMethodRequired => { WsHandshakeError::GetMethodRequired => {
HTTPMethodNotAllowed HTTPMethodNotAllowed
.builder() .build()
.header(header::ALLOW, "GET") .header(header::ALLOW, "GET")
.finish() .finish()
.unwrap() .unwrap()

View File

@ -52,8 +52,8 @@ pub const HTTPInternalServerError: StaticResponse =
pub struct StaticResponse(StatusCode); pub struct StaticResponse(StatusCode);
impl StaticResponse { impl StaticResponse {
pub fn builder(&self) -> HttpResponseBuilder { pub fn build(&self) -> HttpResponseBuilder {
HttpResponse::builder(self.0) HttpResponse::build(self.0)
} }
pub fn response(&self) -> HttpResponse { pub fn response(&self) -> HttpResponse {
HttpResponse::new(self.0, Body::Empty) HttpResponse::new(self.0, Body::Empty)
@ -87,8 +87,8 @@ mod tests {
use super::{HTTPOk, HTTPBadRequest, Body, HttpResponse}; use super::{HTTPOk, HTTPBadRequest, Body, HttpResponse};
#[test] #[test]
fn test_builder() { fn test_build() {
let resp = HTTPOk.builder().body(Body::Empty).unwrap(); let resp = HTTPOk.build().body(Body::Empty).unwrap();
assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.status(), StatusCode::OK);
} }

View File

@ -39,8 +39,9 @@ pub struct HttpResponse {
impl HttpResponse { impl HttpResponse {
/// Create http response builder with specific status.
#[inline] #[inline]
pub fn builder(status: StatusCode) -> HttpResponseBuilder { pub fn build(status: StatusCode) -> HttpResponseBuilder {
HttpResponseBuilder { HttpResponseBuilder {
parts: Some(Parts::new(status)), parts: Some(Parts::new(status)),
err: None, err: None,
@ -450,31 +451,31 @@ mod tests {
#[test] #[test]
fn test_upgrade() { fn test_upgrade() {
let resp = HttpResponse::builder(StatusCode::OK) let resp = HttpResponse::build(StatusCode::OK)
.upgrade().body(Body::Empty).unwrap(); .upgrade().body(Body::Empty).unwrap();
assert!(resp.upgrade()) assert!(resp.upgrade())
} }
#[test] #[test]
fn test_force_close() { fn test_force_close() {
let resp = HttpResponse::builder(StatusCode::OK) let resp = HttpResponse::build(StatusCode::OK)
.force_close().body(Body::Empty).unwrap(); .force_close().body(Body::Empty).unwrap();
assert!(!resp.keep_alive().unwrap()) assert!(!resp.keep_alive().unwrap())
} }
#[test] #[test]
fn test_content_type() { fn test_content_type() {
let resp = HttpResponse::builder(StatusCode::OK) let resp = HttpResponse::build(StatusCode::OK)
.content_type("text/plain").body(Body::Empty).unwrap(); .content_type("text/plain").body(Body::Empty).unwrap();
assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/plain") assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/plain")
} }
#[test] #[test]
fn test_content_encoding() { fn test_content_encoding() {
let resp = HttpResponse::builder(StatusCode::OK).finish().unwrap(); let resp = HttpResponse::build(StatusCode::OK).finish().unwrap();
assert_eq!(*resp.content_encoding(), ContentEncoding::Auto); assert_eq!(*resp.content_encoding(), ContentEncoding::Auto);
let resp = HttpResponse::builder(StatusCode::OK) let resp = HttpResponse::build(StatusCode::OK)
.content_encoding(ContentEncoding::Br).finish().unwrap(); .content_encoding(ContentEncoding::Br).finish().unwrap();
assert_eq!(*resp.content_encoding(), ContentEncoding::Br); assert_eq!(*resp.content_encoding(), ContentEncoding::Br);
} }

View File

@ -301,7 +301,7 @@ mod tests {
headers.insert(header::USER_AGENT, header::HeaderValue::from_static("ACTIX-WEB")); headers.insert(header::USER_AGENT, header::HeaderValue::from_static("ACTIX-WEB"));
let mut req = HttpRequest::new( let mut req = HttpRequest::new(
Method::GET, "/".to_owned(), Version::HTTP_11, headers, String::new(), Payload::empty()); Method::GET, "/".to_owned(), Version::HTTP_11, headers, String::new(), Payload::empty());
let resp = HttpResponse::builder(StatusCode::OK) let resp = HttpResponse::build(StatusCode::OK)
.header("X-Test", "ttt") .header("X-Test", "ttt")
.force_close().body(Body::Empty).unwrap(); .force_close().body(Body::Empty).unwrap();
@ -332,7 +332,7 @@ mod tests {
headers.insert(header::USER_AGENT, header::HeaderValue::from_static("ACTIX-WEB")); headers.insert(header::USER_AGENT, header::HeaderValue::from_static("ACTIX-WEB"));
let req = HttpRequest::new( let req = HttpRequest::new(
Method::GET, "/".to_owned(), Version::HTTP_11, headers, String::new(), Payload::empty()); Method::GET, "/".to_owned(), Version::HTTP_11, headers, String::new(), Payload::empty());
let resp = HttpResponse::builder(StatusCode::OK) let resp = HttpResponse::build(StatusCode::OK)
.force_close().body(Body::Empty).unwrap(); .force_close().body(Body::Empty).unwrap();
let entry_time = time::now(); let entry_time = time::now();
@ -350,7 +350,7 @@ mod tests {
let req = HttpRequest::new( let req = HttpRequest::new(
Method::GET, "/".to_owned(), Version::HTTP_11, HeaderMap::new(), Method::GET, "/".to_owned(), Version::HTTP_11, HeaderMap::new(),
"test".to_owned(), Payload::empty()); "test".to_owned(), Payload::empty());
let resp = HttpResponse::builder(StatusCode::OK) let resp = HttpResponse::build(StatusCode::OK)
.force_close().body(Body::Empty).unwrap(); .force_close().body(Body::Empty).unwrap();
let entry_time = time::now(); let entry_time = time::now();

View File

@ -106,7 +106,7 @@ impl StaticFiles {
{}\ {}\
</ul></body>\n</html>", index_of, index_of, body); </ul></body>\n</html>", index_of, index_of, body);
Ok( Ok(
HTTPOk.builder() HTTPOk.build()
.content_type("text/html; charset=utf-8") .content_type("text/html; charset=utf-8")
.body(html).unwrap() .body(html).unwrap()
) )
@ -176,7 +176,7 @@ impl<S: 'static> RouteHandler<S> for StaticFiles {
} }
} }
} else { } else {
let mut resp = HTTPOk.builder(); let mut resp = HTTPOk.build();
if let Some(ext) = filename.extension() { if let Some(ext) = filename.extension() {
let mime = get_mime_type(&ext.to_string_lossy()); let mime = get_mime_type(&ext.to_string_lossy());
resp.content_type(format!("{}", mime).as_str()); resp.content_type(format!("{}", mime).as_str());

View File

@ -157,7 +157,7 @@ pub fn handshake<S>(req: &HttpRequest<S>) -> Result<HttpResponse, WsHandshakeErr
hash_key(key.as_ref()) hash_key(key.as_ref())
}; };
Ok(HttpResponse::builder(StatusCode::SWITCHING_PROTOCOLS) Ok(HttpResponse::build(StatusCode::SWITCHING_PROTOCOLS)
.connection_type(ConnectionType::Upgrade) .connection_type(ConnectionType::Upgrade)
.header(header::UPGRADE, "websocket") .header(header::UPGRADE, "websocket")
.header(header::TRANSFER_ENCODING, "chunked") .header(header::TRANSFER_ENCODING, "chunked")

View File

@ -18,7 +18,7 @@ fn test_response_cookies() {
let cookies = req.load_cookies().unwrap(); let cookies = req.load_cookies().unwrap();
let resp = httpcodes::HTTPOk let resp = httpcodes::HTTPOk
.builder() .build()
.cookie(Cookie::build("name", "value") .cookie(Cookie::build("name", "value")
.domain("www.rust-lang.org") .domain("www.rust-lang.org")
.path("/test") .path("/test")