|
2 | 2 |
|
3 | 3 | use header::extensions::Extension;
|
4 | 4 | use header::{Origin, WebSocketExtensions, WebSocketKey, WebSocketProtocol, WebSocketVersion};
|
5 |
| -use hyper::header::{Header, HeaderFormat, Headers}; |
| 5 | +use hyper::header::{Authorization, Basic, Header, HeaderFormat, Headers}; |
6 | 6 | use hyper::version::HttpVersion;
|
7 | 7 | use std::borrow::Cow;
|
8 | 8 | use std::convert::Into;
|
@@ -817,6 +817,17 @@ impl<'u> ClientBuilder<'u> {
|
817 | 817 | });
|
818 | 818 | }
|
819 | 819 |
|
| 820 | + // handle username/password from URL |
| 821 | + if !self.url.username().is_empty() { |
| 822 | + self.headers.set(Authorization(Basic { |
| 823 | + username: self.url.username().to_owned(), |
| 824 | + password: match self.url.password() { |
| 825 | + Some(password) => Some(password.to_owned()), |
| 826 | + None => None, |
| 827 | + }, |
| 828 | + })); |
| 829 | + } |
| 830 | + |
820 | 831 | self.headers
|
821 | 832 | .set(Connection(vec![ConnectionOption::ConnectionHeader(
|
822 | 833 | UniCase("Upgrade".to_string()),
|
@@ -980,4 +991,14 @@ mod tests {
|
980 | 991 | assert!(protos.contains(&"electric".to_string()));
|
981 | 992 | assert!(!protos.contains(&"rust-websocket".to_string()));
|
982 | 993 | }
|
| 994 | + |
| 995 | + #[test] |
| 996 | + fn build_client_with_username_password() { |
| 997 | + use super::*; |
| 998 | + let mut builder = ClientBuilder::new("ws://john:[email protected]:8080/hello").unwrap(); |
| 999 | + let _request = builder.build_request(); |
| 1000 | + let auth = builder.headers.get::<Authorization<Basic>>().unwrap(); |
| 1001 | + assert!(auth.username == "john"); |
| 1002 | + assert_eq!(auth.password, Some("pswd".to_owned())); |
| 1003 | + } |
983 | 1004 | }
|
0 commit comments