1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 01:32:57 +01:00

ws tests and proper write payload ref

This commit is contained in:
Nikolay Kim 2018-03-19 09:30:58 -07:00
parent f4a47ef71e
commit e7ec0f9fd7
3 changed files with 45 additions and 4 deletions

View File

@ -238,7 +238,7 @@ impl AsRef<[u8]> for Binary {
#[inline] #[inline]
fn as_ref(&self) -> &[u8] { fn as_ref(&self) -> &[u8] {
match *self { match *self {
Binary::Bytes(ref bytes) => &bytes[..], Binary::Bytes(ref bytes) => bytes.as_ref(),
Binary::Slice(slice) => slice, Binary::Slice(slice) => slice,
Binary::SharedString(ref s) => s.as_bytes(), Binary::SharedString(ref s) => s.as_bytes(),
Binary::ArcSharedString(ref s) => s.as_bytes(), Binary::ArcSharedString(ref s) => s.as_bytes(),

View File

@ -227,9 +227,10 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
// shortcut for upgraded connection // shortcut for upgraded connection
if self.flags.contains(Flags::UPGRADE) { if self.flags.contains(Flags::UPGRADE) {
if self.buffer.is_empty() { if self.buffer.is_empty() {
let n = self.write_data(payload.as_ref())?; let pl: &[u8] = payload.as_ref();
if payload.len() < n { let n = self.write_data(pl)?;
self.buffer.extend_from_slice(&payload.as_ref()[n..]); if pl.len() < n {
self.buffer.extend_from_slice(&pl[n..]);
return Ok(WriterState::Done); return Ok(WriterState::Done);
} }
} else { } else {

View File

@ -3,9 +3,11 @@ extern crate actix_web;
extern crate futures; extern crate futures;
extern crate http; extern crate http;
extern crate bytes; extern crate bytes;
extern crate rand;
use bytes::Bytes; use bytes::Bytes;
use futures::Stream; use futures::Stream;
use rand::Rng;
use actix_web::*; use actix_web::*;
use actix::prelude::*; use actix::prelude::*;
@ -51,3 +53,41 @@ fn test_simple() {
let (item, _) = srv.execute(reader.into_future()).unwrap(); let (item, _) = srv.execute(reader.into_future()).unwrap();
assert_eq!(item, Some(ws::Message::Close(ws::CloseCode::Normal))); 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()))));
}
}