Skip to content

Commit a5d632b

Browse files
committed
feat(net): add set_ssl_verifier method to NetworkConnector trait
The commit includes an implementation of the new trait method for all existing trait impls. BREAKING CHANGE: Adding a new required method to a public trait is a breaking change.
1 parent 38f40c7 commit a5d632b

File tree

5 files changed

+86
-5
lines changed

5 files changed

+86
-5
lines changed

benches/client.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::fmt;
88
use std::io::{self, Read, Write, Cursor};
99
use std::net::SocketAddr;
1010

11-
use hyper::net;
11+
use hyper::net::{self, ContextVerifier};
1212

1313
static README: &'static [u8] = include_bytes!("../README.md");
1414

@@ -83,6 +83,9 @@ impl net::NetworkConnector for MockConnector {
8383
Ok(MockStream::new())
8484
}
8585

86+
fn set_ssl_verifier(&mut self, _verifier: ContextVerifier) {
87+
// pass
88+
}
8689
}
8790

8891
#[bench]

src/client/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ impl<C: NetworkConnector<Stream=S> + Send, S: NetworkStream + Send> NetworkConne
147147
-> ::Result<Box<NetworkStream + Send>> {
148148
Ok(try!(self.0.connect(host, port, scheme)).into())
149149
}
150+
#[inline]
151+
fn set_ssl_verifier(&mut self, verifier: ContextVerifier) {
152+
self.0.set_ssl_verifier(verifier);
153+
}
150154
}
151155

152156
struct Connector(Box<NetworkConnector<Stream=Box<NetworkStream + Send>> + Send>);
@@ -158,6 +162,10 @@ impl NetworkConnector for Connector {
158162
-> ::Result<Box<NetworkStream + Send>> {
159163
Ok(try!(self.0.connect(host, port, scheme)).into())
160164
}
165+
#[inline]
166+
fn set_ssl_verifier(&mut self, verifier: ContextVerifier) {
167+
self.0.set_ssl_verifier(verifier);
168+
}
161169
}
162170

163171
/// Options for an individual Request.

src/client/pool.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::io::{self, Read, Write};
55
use std::net::{SocketAddr, Shutdown};
66
use std::sync::{Arc, Mutex};
77

8-
use net::{NetworkConnector, NetworkStream, HttpConnector};
8+
use net::{NetworkConnector, NetworkStream, HttpConnector, ContextVerifier};
99

1010
/// The `NetworkConnector` that behaves as a connection pool used by hyper's `Client`.
1111
pub struct Pool<C: NetworkConnector> {
@@ -119,6 +119,10 @@ impl<C: NetworkConnector<Stream=S>, S: NetworkStream + Send> NetworkConnector fo
119119
pool: self.inner.clone()
120120
})
121121
}
122+
#[inline]
123+
fn set_ssl_verifier(&mut self, verifier: ContextVerifier) {
124+
self.connector.set_ssl_verifier(verifier);
125+
}
122126
}
123127

124128
/// A Stream that will try to be returned to the Pool when dropped.
@@ -184,8 +188,9 @@ impl<S> Drop for PooledStream<S> {
184188
#[cfg(test)]
185189
mod tests {
186190
use std::net::Shutdown;
187-
use mock::MockConnector;
191+
use mock::{MockConnector, ChannelMockConnector};
188192
use net::{NetworkConnector, NetworkStream};
193+
use std::sync::mpsc;
189194

190195
use super::{Pool, key};
191196

@@ -223,5 +228,19 @@ mod tests {
223228
assert_eq!(locked.conns.len(), 0);
224229
}
225230

231+
/// Tests that the `Pool::set_ssl_verifier` method sets the SSL verifier of
232+
/// the underlying `Connector` instance that it uses.
233+
#[test]
234+
fn test_set_ssl_verifier_delegates_to_connector() {
235+
let (tx, rx) = mpsc::channel();
236+
let mut pool = Pool::with_connector(
237+
Default::default(), ChannelMockConnector::new(tx));
238+
239+
pool.set_ssl_verifier(Box::new(|_| { }));
226240

241+
match rx.try_recv() {
242+
Ok(meth) => assert_eq!(meth, "set_ssl_verifier"),
243+
_ => panic!("Expected a call to `set_ssl_verifier`"),
244+
};
245+
}
227246
}

src/mock.rs

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::fmt;
22
use std::io::{self, Read, Write, Cursor};
33
use std::net::SocketAddr;
4+
use std::sync::mpsc::Sender;
45

5-
use net::{NetworkStream, NetworkConnector};
6+
use net::{NetworkStream, NetworkConnector, ContextVerifier};
67

78
pub struct MockStream {
89
pub read: Cursor<Vec<u8>>,
@@ -76,6 +77,39 @@ impl NetworkConnector for MockConnector {
7677
fn connect(&mut self, _host: &str, _port: u16, _scheme: &str) -> ::Result<MockStream> {
7778
Ok(MockStream::new())
7879
}
80+
81+
fn set_ssl_verifier(&mut self, _verifier: ContextVerifier) {
82+
// pass
83+
}
84+
}
85+
86+
/// A mock implementation of the `NetworkConnector` trait that keeps track of all calls to its
87+
/// methods by sending corresponding messages onto a channel.
88+
///
89+
/// Otherwise, it behaves the same as `MockConnector`.
90+
pub struct ChannelMockConnector {
91+
calls: Sender<String>,
92+
}
93+
94+
impl ChannelMockConnector {
95+
pub fn new(calls: Sender<String>) -> ChannelMockConnector {
96+
ChannelMockConnector { calls: calls }
97+
}
98+
}
99+
100+
impl NetworkConnector for ChannelMockConnector {
101+
type Stream = MockStream;
102+
#[inline]
103+
fn connect(&mut self, _host: &str, _port: u16, _scheme: &str)
104+
-> ::Result<MockStream> {
105+
self.calls.send("connect".into()).unwrap();
106+
Ok(MockStream::new())
107+
}
108+
109+
#[inline]
110+
fn set_ssl_verifier(&mut self, _verifier: ContextVerifier) {
111+
self.calls.send("set_ssl_verifier".into()).unwrap();
112+
}
79113
}
80114

81115
/// new connectors must be created if you wish to intercept requests.
@@ -107,6 +141,9 @@ macro_rules! mock_connector (
107141
}
108142
}
109143

144+
fn set_ssl_verifier(&mut self, _verifier: ::net::ContextVerifier) {
145+
// pass
146+
}
110147
}
111148

112149
)

src/net.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ pub trait NetworkConnector {
7070
type Stream: Into<Box<NetworkStream + Send>>;
7171
/// Connect to a remote address.
7272
fn connect(&mut self, host: &str, port: u16, scheme: &str) -> ::Result<Self::Stream>;
73+
/// Sets the given `ContextVerifier` to be used when verifying the SSL context
74+
/// on the establishment of a new connection.
75+
fn set_ssl_verifier(&mut self, verifier: ContextVerifier);
7376
}
7477

7578
impl<T: NetworkStream + Send> From<T> for Box<NetworkStream + Send> {
@@ -344,12 +347,15 @@ impl NetworkConnector for HttpConnector {
344347
}
345348
}))
346349
}
350+
fn set_ssl_verifier(&mut self, verifier: ContextVerifier) {
351+
self.0 = Some(verifier);
352+
}
347353
}
348354

349355
#[cfg(test)]
350356
mod tests {
351357
use mock::MockStream;
352-
use super::NetworkStream;
358+
use super::{NetworkStream, HttpConnector, NetworkConnector};
353359

354360
#[test]
355361
fn test_downcast_box_stream() {
@@ -371,4 +377,12 @@ mod tests {
371377

372378
}
373379

380+
#[test]
381+
fn test_http_connector_set_ssl_verifier() {
382+
let mut connector = HttpConnector(None);
383+
384+
connector.set_ssl_verifier(Box::new(|_| {}));
385+
386+
assert!(connector.0.is_some());
387+
}
374388
}

0 commit comments

Comments
 (0)