More tests

This commit is contained in:
Carl Lerche
2017-07-11 14:01:25 -07:00
parent 6d1d54f104
commit 4e0e01aa5c

View File

@@ -5,11 +5,11 @@ extern crate mock_io;
extern crate env_logger; extern crate env_logger;
extern crate bytes; extern crate bytes;
use h2::client; use h2::{client, Frame};
use http::request; use http::{request, status};
use bytes::Bytes;
use futures::*; use futures::*;
use bytes::Bytes;
// TODO: move into another file // TODO: move into another file
macro_rules! assert_user_err { macro_rules! assert_user_err {
@@ -32,7 +32,7 @@ fn handshake() {
.write(SETTINGS_ACK) .write(SETTINGS_ACK)
.build(); .build();
let mut h2 = client::handshake(mock) let h2 = client::handshake(mock)
.wait().unwrap(); .wait().unwrap();
// At this point, the connection should be closed // At this point, the connection should be closed
@@ -55,7 +55,7 @@ fn get_with_204_response() {
.read(&[0, 0, 1, 1, 5, 0, 0, 0, 1, 0x89]) .read(&[0, 0, 1, 1, 5, 0, 0, 0, 1, 0x89])
.build(); .build();
let mut h2 = client::handshake(mock) let h2 = client::handshake(mock)
.wait().unwrap(); .wait().unwrap();
// Send the request // Send the request
@@ -66,11 +66,18 @@ fn get_with_204_response() {
// Get the response // Get the response
let (resp, h2) = h2.into_future().wait().unwrap(); let (resp, h2) = h2.into_future().wait().unwrap();
match resp.unwrap() {
Frame::Headers { headers, .. } => {
assert_eq!(headers.status, status::NO_CONTENT);
}
_ => panic!("unexpected frame"),
}
// No more frames
assert!(Stream::wait(h2).next().is_none()); assert!(Stream::wait(h2).next().is_none());
} }
#[test] #[test]
#[ignore]
fn get_with_200_response() { fn get_with_200_response() {
let _ = ::env_logger::init(); let _ = ::env_logger::init();
@@ -78,15 +85,18 @@ fn get_with_200_response() {
.handshake() .handshake()
// Write GET / // Write GET /
.write(&[ .write(&[
0, 0, 0x10, 1, 5, 0, 0, 0, 1, 0x82, 0x87, 0x41, 0x8B, 0x9D, 0x29, 0, 0, 16, 1, 5, 0, 0, 0, 1, 130, 135, 65, 139, 157, 41, 172, 75,
0xAC, 0x4B, 0x8F, 0xA8, 0xE9, 0x19, 0x97, 0x21, 0xE9, 0x84, 143, 168, 233, 25, 151, 33, 233, 132
]) ])
.write(SETTINGS_ACK) .write(SETTINGS_ACK)
// Read response // Read response
.read(&[0, 0, 1, 1, 5, 0, 0, 0, 1, 0x89]) .read(&[
0, 0, 1, 1, 4, 0, 0, 0, 1, 136, 0, 0, 5, 0, 0, 0, 0, 0, 1, 104, 101,
108, 108, 111, 0, 0, 5, 0, 1, 0, 0, 0, 1, 119, 111, 114, 108, 100,
])
.build(); .build();
let mut h2 = client::handshake(mock) let h2 = client::handshake(mock)
.wait().unwrap(); .wait().unwrap();
// Send the request // Send the request
@@ -94,9 +104,40 @@ fn get_with_200_response() {
request.uri = "https://http2.akamai.com/".parse().unwrap(); request.uri = "https://http2.akamai.com/".parse().unwrap();
let h2 = h2.send_request(1.into(), request, true).wait().unwrap(); let h2 = h2.send_request(1.into(), request, true).wait().unwrap();
// Get the response // Get the response headers
let (resp, h2) = h2.into_future().wait().unwrap(); let (resp, h2) = h2.into_future().wait().unwrap();
match resp.unwrap() {
Frame::Headers { headers, .. } => {
assert_eq!(headers.status, status::OK);
}
_ => panic!("unexpected frame"),
}
// Get the response body
let (data, h2) = h2.into_future().wait().unwrap();
match data.unwrap() {
Frame::Data { id, data, end_of_stream } => {
assert_eq!(id, 1.into());
assert_eq!(data, &b"hello"[..]);
assert!(!end_of_stream);
}
_ => panic!("unexpected frame"),
}
// Get the response body
let (data, h2) = h2.into_future().wait().unwrap();
match data.unwrap() {
Frame::Data { id, data, end_of_stream } => {
assert_eq!(id, 1.into());
assert_eq!(data, &b"world"[..]);
assert!(end_of_stream);
}
_ => panic!("unexpected frame"),
}
assert!(Stream::wait(h2).next().is_none()); assert!(Stream::wait(h2).next().is_none());
} }
@@ -135,10 +176,16 @@ fn request_with_server_stream_id() {
} }
#[test] #[test]
#[ignore] fn send_headers_twice_with_same_stream_id() {
fn send_data_without_headers() { let _ = ::env_logger::init();
let mock = mock_io::Builder::new() let mock = mock_io::Builder::new()
.handshake() .handshake()
// Write GET /
.write(&[
0, 0, 0x10, 1, 5, 0, 0, 0, 1, 0x82, 0x87, 0x41, 0x8B, 0x9D, 0x29,
0xAC, 0x4B, 0x8F, 0xA8, 0xE9, 0x19, 0x97, 0x21, 0xE9, 0x84,
])
.build(); .build();
let h2 = client::handshake(mock) let h2 = client::handshake(mock)
@@ -147,11 +194,29 @@ fn send_data_without_headers() {
// Send the request // Send the request
let mut request = request::Head::default(); let mut request = request::Head::default();
request.uri = "https://http2.akamai.com/".parse().unwrap(); request.uri = "https://http2.akamai.com/".parse().unwrap();
let h2 = h2.send_request(1.into(), request, true).wait().unwrap();
/* // Send another request with the same stream ID
let err = h2.send_request(2.into(), request, true).wait().unwrap_err(); let mut request = request::Head::default();
assert_user_err!(err, InvalidStreamId); request.uri = "https://http2.akamai.com/".parse().unwrap();
*/ let err = h2.send_request(1.into(), request, true).wait().unwrap_err();
assert_user_err!(err, UnexpectedFrameType);
}
#[test]
fn send_data_without_headers() {
let mock = mock_io::Builder::new()
.handshake()
.build();
let h2 = client::handshake(mock)
.wait().unwrap();
let err = h2.send_data(1.into(), Bytes::from_static(b"hello world"), true)
.wait().unwrap_err();
assert_user_err!(err, InactiveStreamId);
} }
#[test] #[test]