add Client config to disable server push
- Adds `Client::builder().enable_push(false)` to disable push - Client sends a GO_AWAY if receiving a push when it's disabled
This commit is contained in:
109
tests/push_promise.rs
Normal file
109
tests/push_promise.rs
Normal file
@@ -0,0 +1,109 @@
|
||||
extern crate h2_test_support;
|
||||
use h2_test_support::prelude::*;
|
||||
|
||||
#[test]
|
||||
fn recv_push_works() {
|
||||
// tests that by default, received push promises work
|
||||
// TODO: once API exists, read the pushed response
|
||||
let _ = ::env_logger::init();
|
||||
|
||||
let (io, srv) = mock::new();
|
||||
let mock = srv.assert_client_handshake()
|
||||
.unwrap()
|
||||
.recv_settings()
|
||||
.recv_frame(
|
||||
frames::headers(1)
|
||||
.request("GET", "https://http2.akamai.com/")
|
||||
.eos(),
|
||||
)
|
||||
.send_frame(
|
||||
frames::push_promise(1, 2).request("GET", "https://http2.akamai.com/style.css"),
|
||||
)
|
||||
.send_frame(frames::headers(1).response(200).eos())
|
||||
.send_frame(frames::headers(2).response(200).eos());
|
||||
|
||||
let h2 = Client::handshake(io).unwrap().and_then(|mut h2| {
|
||||
let request = Request::builder()
|
||||
.method(Method::GET)
|
||||
.uri("https://http2.akamai.com/")
|
||||
.body(())
|
||||
.unwrap();
|
||||
let req = h2.request(request, true)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.and_then(|resp| {
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
Ok(())
|
||||
});
|
||||
|
||||
h2.drive(req)
|
||||
});
|
||||
|
||||
h2.join(mock).wait().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recv_push_when_push_disabled_is_conn_error() {
|
||||
let _ = ::env_logger::init();
|
||||
|
||||
let (io, srv) = mock::new();
|
||||
let mock = srv.assert_client_handshake()
|
||||
.unwrap()
|
||||
.ignore_settings()
|
||||
.recv_frame(
|
||||
frames::headers(1)
|
||||
.request("GET", "https://http2.akamai.com/")
|
||||
.eos(),
|
||||
)
|
||||
.send_frame(
|
||||
frames::push_promise(1, 3).request("GET", "https://http2.akamai.com/style.css"),
|
||||
)
|
||||
.send_frame(frames::headers(1).response(200).eos())
|
||||
.recv_frame(frames::go_away(0).protocol_error());
|
||||
|
||||
let h2 = Client::builder()
|
||||
.enable_push(false)
|
||||
.handshake::<_, Bytes>(io)
|
||||
.unwrap()
|
||||
.and_then(|mut h2| {
|
||||
let request = Request::builder()
|
||||
.method(Method::GET)
|
||||
.uri("https://http2.akamai.com/")
|
||||
.body(())
|
||||
.unwrap();
|
||||
let req = h2.request(request, true).unwrap().then(|res| {
|
||||
let err = res.unwrap_err();
|
||||
assert_eq!(
|
||||
err.to_string(),
|
||||
"protocol error: unspecific protocol error detected"
|
||||
);
|
||||
Ok::<(), ()>(())
|
||||
});
|
||||
|
||||
// client should see a protocol error
|
||||
let conn = h2.then(|res| {
|
||||
let err = res.unwrap_err();
|
||||
assert_eq!(
|
||||
err.to_string(),
|
||||
"protocol error: unspecific protocol error detected"
|
||||
);
|
||||
Ok::<(), ()>(())
|
||||
});
|
||||
|
||||
conn.unwrap().join(req)
|
||||
});
|
||||
|
||||
h2.join(mock).wait().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn recv_push_promise_with_unsafe_method_is_stream_error() {
|
||||
// for instance, when :method = POST
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn recv_push_promise_with_wrong_authority_is_stream_error() {
|
||||
// if server is foo.com, :authority = bar.com is stream error
|
||||
}
|
||||
@@ -28,6 +28,18 @@ pub fn data<T, B>(id: T, buf: B) -> Mock<frame::Data>
|
||||
Mock(frame::Data::new(id.into(), buf.into()))
|
||||
}
|
||||
|
||||
pub fn push_promise<T1, T2>(id: T1, promised: T2) -> Mock<frame::PushPromise>
|
||||
where T1: Into<StreamId>,
|
||||
T2: Into<StreamId>,
|
||||
{
|
||||
Mock(frame::PushPromise::new(
|
||||
id.into(),
|
||||
promised.into(),
|
||||
frame::Pseudo::default(),
|
||||
HeaderMap::default(),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn window_update<T>(id: T, sz: u32) -> frame::WindowUpdate
|
||||
where T: Into<StreamId>,
|
||||
{
|
||||
@@ -140,9 +152,54 @@ impl From<Mock<frame::Data>> for SendFrame {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// PushPromise helpers
|
||||
|
||||
impl Mock<frame::PushPromise> {
|
||||
pub fn request<M, U>(self, method: M, uri: U) -> Self
|
||||
where M: HttpTryInto<http::Method>,
|
||||
U: HttpTryInto<http::Uri>,
|
||||
{
|
||||
let method = method.try_into().unwrap();
|
||||
let uri = uri.try_into().unwrap();
|
||||
let (id, promised, _, fields) = self.into_parts();
|
||||
let frame = frame::PushPromise::new(
|
||||
id,
|
||||
promised,
|
||||
frame::Pseudo::request(method, uri),
|
||||
fields
|
||||
);
|
||||
Mock(frame)
|
||||
}
|
||||
|
||||
pub fn fields(self, fields: HeaderMap) -> Self {
|
||||
let (id, promised, pseudo, _) = self.into_parts();
|
||||
let frame = frame::PushPromise::new(id, promised, pseudo, fields);
|
||||
Mock(frame)
|
||||
}
|
||||
|
||||
fn into_parts(self) -> (StreamId, StreamId, frame::Pseudo, HeaderMap) {
|
||||
assert!(self.0.is_end_headers(), "unset eoh will be lost");
|
||||
let id = self.0.stream_id();
|
||||
let promised = self.0.promised_id();
|
||||
let parts = self.0.into_parts();
|
||||
(id, promised, parts.0, parts.1)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Mock<frame::PushPromise>> for SendFrame {
|
||||
fn from(src: Mock<frame::PushPromise>) -> Self {
|
||||
Frame::PushPromise(src.0)
|
||||
}
|
||||
}
|
||||
|
||||
// GoAway helpers
|
||||
|
||||
impl Mock<frame::GoAway> {
|
||||
pub fn protocol_error(self) -> Self {
|
||||
Mock(frame::GoAway::new(self.0.last_stream_id(), frame::Reason::ProtocolError))
|
||||
}
|
||||
|
||||
pub fn flow_control(self) -> Self {
|
||||
Mock(frame::GoAway::new(self.0.last_stream_id(), frame::Reason::FlowControlError))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user