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

@@ -20,41 +20,3 @@ fn read_range() {
[83, 99, 111, 116, 116, 34, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32]
)
}
#[test]
#[cfg(any(feature = "tls", feature = "native-tls"))]
fn agent_pool() {
let agent = agent();
// req 1
let resp = agent
.get("https://ureq.s3.eu-central-1.amazonaws.com/sherlock.txt")
.set("Range", "bytes=1000-1999")
.call();
assert_eq!(resp.status(), 206);
let mut reader = resp.into_reader();
let mut buf = vec![];
// reading the entire content will return the connection to the pool
let len = reader.read_to_end(&mut buf).unwrap();
assert_eq!(len, 1000);
{
let mut lock = agent.state.lock().unwrap();
let state = lock.as_mut().unwrap();
let pool = state.pool();
assert_eq!(pool.len(), 1);
let f = format!("{:?}", pool.get("ureq.s3.eu-central-1.amazonaws.com", 443));
assert_eq!(f, "Some(Stream[https])"); // not a great way of testing.
}
// req 2 should be done with a reused connection
let resp = agent
.get("https://ureq.s3.eu-central-1.amazonaws.com/sherlock.txt")
.set("Range", "bytes=5000-6999")
.call();
assert_eq!(resp.status(), 206);
let mut reader = resp.into_reader();
let mut buf = vec![];
let len = reader.read_to_end(&mut buf).unwrap();
assert_eq!(len, 2000);
}