Add scheme to PoolKey and let port be None. (#84)

PoolKey calls unwrap() on an option that can be None. Specifically, the
local variable `port` can be None when PoolKey is constructed with a Url
whose scheme is unrecognized in url.port_or_known_default().

To fix that, make port an Option. Also, make scheme part of the PoolKey.
This prevents, for instance, a stream opened for `https://example.com:9999`
being reused on a request for `http://example.com:9999`.

Remove the test-only pool.get() accessor. This was used in only one test,
agent_pool in range.rs. This seemed like it was testing the agent more
than it was testing ranges, so I moved it to agent.rs and edited to
remove the range-testing parts.

Also, reject unrecognized URLs earlier in connect_socket so they don't
reach try_get_connection.
This commit is contained in:
Jacob Hoffman-Andrews
2020-06-22 23:23:39 -07:00
committed by GitHub
parent 00461fb5bd
commit 3014f58a28
4 changed files with 46 additions and 61 deletions

View File

@@ -262,6 +262,7 @@ pub(crate) fn basic_auth(user: &str, pass: &str) -> String {
#[cfg(test)]
mod tests {
use super::*;
use std::io::Read;
///////////////////// AGENT TESTS //////////////////////////////
@@ -273,6 +274,35 @@ mod tests {
});
}
#[test]
#[cfg(any(feature = "tls", feature = "native-tls"))]
fn agent_pool() {
let agent = crate::agent();
let url = "https://ureq.s3.eu-central-1.amazonaws.com/sherlock.txt";
// req 1
let resp = agent.get(url).call();
assert!(resp.ok());
let mut reader = resp.into_reader();
let mut buf = vec![];
// reading the entire content will return the connection to the pool
reader.read_to_end(&mut buf).unwrap();
fn poolsize(agent: &Agent) -> usize {
let mut lock = agent.state.lock().unwrap();
let state = lock.as_mut().unwrap();
state.pool().len()
}
assert_eq!(poolsize(&agent), 1);
// req 2 should be done with a reused connection
let resp = agent.get(url).call();
assert!(resp.ok());
assert_eq!(poolsize(&agent), 0);
let mut reader = resp.into_reader();
let mut buf = vec![];
reader.read_to_end(&mut buf).unwrap();
}
//////////////////// REQUEST TESTS /////////////////////////////
#[test]