mirror of
https://github.com/actix/actix-web.git
synced 2024-11-18 15:41:17 +00:00
ws tests and proper write payload ref
This commit is contained in:
parent
f4a47ef71e
commit
e7ec0f9fd7
3 changed files with 45 additions and 4 deletions
|
@ -238,7 +238,7 @@ impl AsRef<[u8]> for Binary {
|
|||
#[inline]
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
match *self {
|
||||
Binary::Bytes(ref bytes) => &bytes[..],
|
||||
Binary::Bytes(ref bytes) => bytes.as_ref(),
|
||||
Binary::Slice(slice) => slice,
|
||||
Binary::SharedString(ref s) => s.as_bytes(),
|
||||
Binary::ArcSharedString(ref s) => s.as_bytes(),
|
||||
|
|
|
@ -227,9 +227,10 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
|
|||
// shortcut for upgraded connection
|
||||
if self.flags.contains(Flags::UPGRADE) {
|
||||
if self.buffer.is_empty() {
|
||||
let n = self.write_data(payload.as_ref())?;
|
||||
if payload.len() < n {
|
||||
self.buffer.extend_from_slice(&payload.as_ref()[n..]);
|
||||
let pl: &[u8] = payload.as_ref();
|
||||
let n = self.write_data(pl)?;
|
||||
if pl.len() < n {
|
||||
self.buffer.extend_from_slice(&pl[n..]);
|
||||
return Ok(WriterState::Done);
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -3,9 +3,11 @@ extern crate actix_web;
|
|||
extern crate futures;
|
||||
extern crate http;
|
||||
extern crate bytes;
|
||||
extern crate rand;
|
||||
|
||||
use bytes::Bytes;
|
||||
use futures::Stream;
|
||||
use rand::Rng;
|
||||
|
||||
use actix_web::*;
|
||||
use actix::prelude::*;
|
||||
|
@ -51,3 +53,41 @@ fn test_simple() {
|
|||
let (item, _) = srv.execute(reader.into_future()).unwrap();
|
||||
assert_eq!(item, Some(ws::Message::Close(ws::CloseCode::Normal)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_large_text() {
|
||||
let data = rand::thread_rng()
|
||||
.gen_ascii_chars()
|
||||
.take(65_536)
|
||||
.collect::<String>();
|
||||
|
||||
let mut srv = test::TestServer::new(
|
||||
|app| app.handler(|req| ws::start(req, Ws)));
|
||||
let (mut reader, mut writer) = srv.ws().unwrap();
|
||||
|
||||
for _ in 0..100 {
|
||||
writer.text(data.clone());
|
||||
let (item, r) = srv.execute(reader.into_future()).unwrap();
|
||||
reader = r;
|
||||
assert_eq!(item, Some(ws::Message::Text(data.clone())));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_large_bin() {
|
||||
let data = rand::thread_rng()
|
||||
.gen_ascii_chars()
|
||||
.take(65_536)
|
||||
.collect::<String>();
|
||||
|
||||
let mut srv = test::TestServer::new(
|
||||
|app| app.handler(|req| ws::start(req, Ws)));
|
||||
let (mut reader, mut writer) = srv.ws().unwrap();
|
||||
|
||||
for _ in 0..100 {
|
||||
writer.binary(data.clone());
|
||||
let (item, r) = srv.execute(reader.into_future()).unwrap();
|
||||
reader = r;
|
||||
assert_eq!(item, Some(ws::Message::Binary(Binary::from(data.clone()))));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue