mirror of
https://github.com/servo/servo.git
synced 2025-08-04 21:20:23 +01:00
Convert AssertAuthHeaderRequestFactory.
This commit is contained in:
parent
36de07bfdf
commit
f712ddb0db
1 changed files with 17 additions and 46 deletions
|
@ -113,10 +113,9 @@ impl UIProvider for TestProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn basic_auth(headers: Headers) -> MockResponse {
|
fn basic_auth() -> MockResponse {
|
||||||
|
|
||||||
MockResponse::new(
|
MockResponse::new(
|
||||||
headers,
|
Headers::new(),
|
||||||
StatusCode::Unauthorized,
|
StatusCode::Unauthorized,
|
||||||
RawStatus(401, Cow::Borrowed("Unauthorized")),
|
RawStatus(401, Cow::Borrowed("Unauthorized")),
|
||||||
b"".to_vec()
|
b"".to_vec()
|
||||||
|
@ -138,7 +137,8 @@ enum ResponseType {
|
||||||
Redirect(String),
|
Redirect(String),
|
||||||
RedirectWithHeaders(String, Headers),
|
RedirectWithHeaders(String, Headers),
|
||||||
Text(Vec<u8>),
|
Text(Vec<u8>),
|
||||||
WithHeaders(Vec<u8>, Headers)
|
WithHeaders(Vec<u8>, Headers),
|
||||||
|
NeedsAuth,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MockRequest {
|
struct MockRequest {
|
||||||
|
@ -165,6 +165,9 @@ fn response_for_request_type(t: ResponseType) -> Result<MockResponse, LoadError>
|
||||||
},
|
},
|
||||||
ResponseType::WithHeaders(b, h) => {
|
ResponseType::WithHeaders(b, h) => {
|
||||||
Ok(respond_with_headers(b, h))
|
Ok(respond_with_headers(b, h))
|
||||||
|
},
|
||||||
|
ResponseType::NeedsAuth => {
|
||||||
|
Ok(basic_auth())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -179,40 +182,6 @@ impl HttpRequest for MockRequest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct AssertAuthHeaderRequest {
|
|
||||||
expected_headers: Headers,
|
|
||||||
request_headers: Headers,
|
|
||||||
t: ResponseType
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AssertAuthHeaderRequest {
|
|
||||||
fn new(t: ResponseType, expected_headers: Headers) -> Self {
|
|
||||||
AssertAuthHeaderRequest { expected_headers: expected_headers, request_headers: Headers::new(), t: t }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HttpRequest for AssertAuthHeaderRequest {
|
|
||||||
type R = MockResponse;
|
|
||||||
|
|
||||||
fn headers_mut(&mut self) -> &mut Headers { &mut self.request_headers }
|
|
||||||
|
|
||||||
fn send(self, _: &Option<Vec<u8>>) -> Result<MockResponse, LoadError> {
|
|
||||||
|
|
||||||
if self.request_headers.has::<Authorization<Basic>>() {
|
|
||||||
for header in self.expected_headers.iter() {
|
|
||||||
assert!(self.request_headers.get_raw(header.name()).is_some());
|
|
||||||
assert_eq!(
|
|
||||||
self.request_headers.get_raw(header.name()).unwrap(),
|
|
||||||
self.expected_headers.get_raw(header.name()).unwrap()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
response_for_request_type(self.t)
|
|
||||||
} else {
|
|
||||||
Ok(basic_auth(self.request_headers))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct AssertMustHaveHeadersRequestFactory {
|
struct AssertMustHaveHeadersRequestFactory {
|
||||||
expected_headers: Headers,
|
expected_headers: Headers,
|
||||||
body: Vec<u8>
|
body: Vec<u8>
|
||||||
|
@ -233,15 +202,17 @@ struct AssertAuthHeaderRequestFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HttpRequestFactory for AssertAuthHeaderRequestFactory {
|
impl HttpRequestFactory for AssertAuthHeaderRequestFactory {
|
||||||
type R = AssertAuthHeaderRequest;
|
type R = MockRequest;
|
||||||
|
|
||||||
fn create(&self, _: Url, _: Method) -> Result<AssertAuthHeaderRequest, LoadError> {
|
fn create_with_headers(&self, _: Url, _: Method, headers: Headers) -> Result<MockRequest, LoadError> {
|
||||||
Ok(
|
let request = if headers.has::<Authorization<Basic>>() {
|
||||||
AssertAuthHeaderRequest::new(
|
assert_headers_included(&self.expected_headers, &headers);
|
||||||
ResponseType::Text(self.body.clone()),
|
MockRequest::new(ResponseType::Text(self.body.clone()))
|
||||||
self.expected_headers.clone()
|
} else {
|
||||||
)
|
MockRequest::new(ResponseType::NeedsAuth)
|
||||||
)
|
};
|
||||||
|
|
||||||
|
Ok(request)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue