1
0
mirror of https://github.com/actix/actix-website synced 2025-01-23 00:25:55 +01:00

Merge pull request #81 from yim7/master

fixed testing examples
This commit is contained in:
Nikolay Kim 2019-04-03 15:14:49 -07:00 committed by GitHub
commit 0c8ce6f832
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -63,7 +63,7 @@ use actix_web::{HttpRequest, HttpMessage};
use actix_web::test::TestServer; use actix_web::test::TestServer;
use std::str; use std::str;
fn index(req: HttpRequest) -> &'static str { fn index(req: &HttpRequest) -> &'static str {
"Hello world!" "Hello world!"
} }
@ -151,17 +151,17 @@ use bytes::Bytes;
use futures::stream::poll_fn; use futures::stream::poll_fn;
use futures::{Async, Poll, Stream}; use futures::{Async, Poll, Stream};
use actix_web::{HttpRequest, HttpResponse, Error}; use actix_web::{Error, HttpMessage, HttpRequest, HttpResponse};
use actix_web::http::{ContentEncoding, StatusCode}; use actix_web::http::{ContentEncoding, StatusCode};
use actix_web::test::TestServer; use actix_web::test::TestServer;
fn sse(_req: HttpRequest) -> HttpResponse { fn sse(_req: &HttpRequest) -> HttpResponse {
let mut counter = 5usize; let mut counter = 5usize;
// yields `data: N` where N in [5; 1] // yields `data: N` where N in [5; 1]
let server_events = poll_fn(move || -> Poll<Option<Bytes>, Error> { let server_events = poll_fn(move || -> Poll<Option<Bytes>, Error> {
if counter == 0 { if counter == 0 {
return Ok(Async::NotReady); return Ok(Async::Ready(None));
} }
let payload = format!("data: {}\n\n", counter); let payload = format!("data: {}\n\n", counter);
counter -= 1; counter -= 1;
@ -185,12 +185,17 @@ fn main() {
assert!(response.status().is_success()); assert!(response.status().is_success());
// convert ClientResponse to future, start read body and wait first chunk // convert ClientResponse to future, start read body and wait first chunk
let (bytes, response) = srv.execute(response.into_future()).unwrap(); let mut stream = response.payload();
assert_eq!(bytes.unwrap(), Bytes::from("data: 5\n\n")); loop {
match srv.execute(stream.into_future()) {
// next chunk Ok((Some(bytes), remain)) => {
let (bytes, _) = srv.execute(response.into_future()).unwrap(); println!("{:?}", bytes);
assert_eq!(bytes.unwrap(), Bytes::from("data: 4\n\n")); stream = remain
}
Ok((None, _)) => break,
Err(_) => panic!(),
}
}
} }
``` ```
@ -205,6 +210,7 @@ result of the future computation.
The following example demonstrates how to test a websocket handler: The following example demonstrates how to test a websocket handler:
```rust ```rust
use actix::{Actor, StreamHandler};
use actix_web::*; use actix_web::*;
use futures::Stream; use futures::Stream;