Skip to content

Commit

Permalink
fix(client): keep the underlying connector when setting an SSL verifier
Browse files Browse the repository at this point in the history
The client no longer drops the old `Connector` instance, along with its
entire context (such as the settings and pooled connections in the case
of a `Pool` connector); rather, it passes the SSL verifier on to the
`Connector` so that it uses it for any future connections that it needs
to establish.

A regression test is included.

Closes #495
  • Loading branch information
mlalic committed May 9, 2015
1 parent a5d632b commit f4556d5
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use url::ParseError as UrlError;
use header::{Headers, Header, HeaderFormat};
use header::{ContentLength, Location};
use method::Method;
use net::{NetworkConnector, NetworkStream, HttpConnector, ContextVerifier};
use net::{NetworkConnector, NetworkStream, ContextVerifier};
use status::StatusClass::Redirection;
use {Url};
use Error;
Expand Down Expand Up @@ -85,10 +85,7 @@ impl Client {

/// Set the SSL verifier callback for use with OpenSSL.
pub fn set_ssl_verifier(&mut self, verifier: ContextVerifier) {
self.connector = with_connector(Pool::with_connector(
Default::default(),
HttpConnector(Some(verifier))
));
self.connector.set_ssl_verifier(verifier);
}

/// Set the RedirectPolicy.
Expand Down Expand Up @@ -409,6 +406,8 @@ mod tests {
use header::Server;
use super::{Client, RedirectPolicy};
use url::Url;
use mock::ChannelMockConnector;
use std::sync::mpsc::{self, TryRecvError};

mock_connector!(MockRedirectPolicy {
"http://127.0.0.1" => "HTTP/1.1 301 Redirect\r\n\
Expand Down Expand Up @@ -455,4 +454,30 @@ mod tests {
assert_eq!(res.headers.get(), Some(&Server("mock2".to_string())));
}

/// Tests that the `Client::set_ssl_verifier` method does not drop the
/// old connector, but rather delegates the change to the connector itself.
#[test]
fn test_client_set_ssl_verifer() {
let (tx, rx) = mpsc::channel();
let mut client = Client::with_connector(ChannelMockConnector::new(tx));

client.set_ssl_verifier(Box::new(|_| {}));

// Make sure that the client called the `set_ssl_verifier` method
match rx.try_recv() {
Ok(meth) => {
assert_eq!(meth, "set_ssl_verifier");
},
_ => panic!("Expected a call to `set_ssl_verifier`"),
};
// Now make sure that no other method was called, as well as that
// the connector is still alive (i.e. wasn't dropped by the client).
match rx.try_recv() {
Err(TryRecvError::Empty) => {},
Err(TryRecvError::Disconnected) => {
panic!("Expected the connector to still be alive.");
},
Ok(_) => panic!("Did not expect any more method calls."),
};
}
}

0 comments on commit f4556d5

Please sign in to comment.