Skip to content

Commit 165241b

Browse files
authored
Upgrade wiremock to 0.6.2 (#19)
wiremock replaces the `http-types` with the `http` crate. I've adapt `struct BasicAuth` from `http-types` into the code. Alternatively wiremock provides `BasicAuthMatcher` which would be simpler but produce less concrete errors.
2 parents 2373ed6 + d98d932 commit 165241b

File tree

5 files changed

+104
-58
lines changed

5 files changed

+104
-58
lines changed

open-build-service-mock/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ license = "MIT OR Apache-2.0"
99

1010
[dependencies]
1111
base16ct = { version = "0.1", features = ["std"] }
12-
http-types = "2.12"
12+
http = "1.2.0"
1313
md-5 = "0.10"
1414
quick-xml = { version = "0.23.1", features = [ "serialize" ] }
1515
rand = "0.8.5"
1616
serde = "1.0.125"
1717
strum = "0.23"
1818
strum_macros = "0.23"
1919
xml-builder = "=0.5.3"
20-
wiremock = "0.5.22"
20+
wiremock = "0.6.2"
21+
base64ct = { version = "1.6.0", features = ["std"] }

open-build-service-mock/src/api/build.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ use super::*;
1212

1313
fn unknown_repo(project: &str, repo: &str) -> ApiError {
1414
ApiError::new(
15-
StatusCode::NotFound,
15+
StatusCode::NOT_FOUND,
1616
"404".to_owned(),
1717
format!("project '{}' has no repository '{}'", project, repo),
1818
)
1919
}
2020

2121
fn unknown_arch(project: &str, repo: &str, arch: &str) -> ApiError {
2222
ApiError::new(
23-
StatusCode::NotFound,
23+
StatusCode::NOT_FOUND,
2424
"404".to_owned(),
2525
format!(
2626
"repository '{}/{}' has no architecture '{}'",
@@ -31,7 +31,7 @@ fn unknown_arch(project: &str, repo: &str, arch: &str) -> ApiError {
3131

3232
fn unknown_parameter(param: &str) -> ApiError {
3333
ApiError::new(
34-
StatusCode::BadRequest,
34+
StatusCode::BAD_REQUEST,
3535
"400".to_owned(),
3636
format!("unknown parameter '{}'", param),
3737
)
@@ -61,7 +61,7 @@ impl Respond for ProjectBuildCommandResponder {
6161

6262
let cmd = try_api!(
6363
find_query_param(request, "cmd").ok_or_else(|| ApiError::new(
64-
StatusCode::BadRequest,
64+
StatusCode::BAD_REQUEST,
6565
"missing_parameter".to_string(),
6666
"Missing parameter 'cmd'".to_string()
6767
))
@@ -76,7 +76,7 @@ impl Respond for ProjectBuildCommandResponder {
7676
"package" => package_names.push(value.clone().into_owned()),
7777
"arch" | "repository" | "code" | "lastbuild" => {
7878
return ApiError::new(
79-
StatusCode::MisdirectedRequest,
79+
StatusCode::MISDIRECTED_REQUEST,
8080
"unsupported".to_string(),
8181
"Operation not supported by the OBS mock server".to_owned(),
8282
)
@@ -106,7 +106,7 @@ impl Respond for ProjectBuildCommandResponder {
106106
inner_xml.render(&mut inner, false, true).unwrap();
107107

108108
return ApiError::new(
109-
StatusCode::NotFound,
109+
StatusCode::NOT_FOUND,
110110
"not_found".to_owned(),
111111
String::from_utf8_lossy(&inner).into_owned(),
112112
)
@@ -134,10 +134,10 @@ impl Respond for ProjectBuildCommandResponder {
134134
}
135135
}
136136

137-
ResponseTemplate::new(StatusCode::Ok).set_body_xml(build_status_xml("ok", None))
137+
ResponseTemplate::new(StatusCode::OK).set_body_xml(build_status_xml("ok", None))
138138
}
139139
_ => ApiError::new(
140-
StatusCode::BadRequest,
140+
StatusCode::BAD_REQUEST,
141141
"illegal_request".to_owned(),
142142
format!("unsupported POST command {} to {}", cmd, request.url),
143143
)
@@ -213,7 +213,7 @@ impl Respond for ArchListingResponder {
213213
xml.add_child(child_xml).unwrap();
214214
}
215215

216-
ResponseTemplate::new(StatusCode::Ok).set_body_xml(xml)
216+
ResponseTemplate::new(StatusCode::OK).set_body_xml(xml)
217217
}
218218
}
219219

@@ -330,7 +330,7 @@ impl Respond for BuildJobHistoryResponder {
330330
"code" => code_names.push(value.into_owned()),
331331
"limit" if limit.is_some() => {
332332
return ApiError::new(
333-
StatusCode::Ok,
333+
StatusCode::OK,
334334
"400".to_owned(),
335335
"parameter 'limit' set multiple times".to_owned(),
336336
)
@@ -410,7 +410,7 @@ impl Respond for BuildJobHistoryResponder {
410410
}
411411
}
412412

413-
ResponseTemplate::new(StatusCode::Ok).set_body_xml(xml)
413+
ResponseTemplate::new(StatusCode::OK).set_body_xml(xml)
414414
}
415415
}
416416

@@ -465,7 +465,7 @@ impl Respond for BuildBinaryListResponder {
465465
}
466466
}
467467

468-
ResponseTemplate::new(StatusCode::Ok).set_body_xml(xml)
468+
ResponseTemplate::new(StatusCode::OK).set_body_xml(xml)
469469
}
470470
}
471471

@@ -513,11 +513,11 @@ impl Respond for BuildBinaryFileResponder {
513513
let file = try_api!(package
514514
.and_then(|package| package.binaries.get(file_name))
515515
.ok_or_else(|| ApiError::new(
516-
StatusCode::NotFound,
516+
StatusCode::NOT_FOUND,
517517
"404".to_owned(),
518518
format!("{}: No such file or directory", file_name)
519519
)));
520-
ResponseTemplate::new(StatusCode::Ok)
520+
ResponseTemplate::new(StatusCode::OK)
521521
.set_body_raw(file.contents.clone(), "application/octet-stream")
522522
}
523523
}
@@ -562,7 +562,7 @@ impl Respond for BuildPackageStatusResponder {
562562
.ok_or_else(|| unknown_arch(project_name, repo_name, arch)));
563563

564564
let package = arch.packages.get(package_name);
565-
ResponseTemplate::new(StatusCode::Ok).set_body_xml(package.map_or_else(
565+
ResponseTemplate::new(StatusCode::OK).set_body_xml(package.map_or_else(
566566
|| {
567567
package_status_xml(
568568
package_name,
@@ -587,15 +587,15 @@ impl BuildLogResponder {
587587
fn parse_number_param(value: Cow<str>) -> Result<usize, ApiError> {
588588
if value.is_empty() {
589589
return Err(ApiError::new(
590-
StatusCode::BadRequest,
590+
StatusCode::BAD_REQUEST,
591591
"400".to_owned(),
592592
"number is empty".to_owned(),
593593
));
594594
}
595595

596596
value.as_ref().parse().map_err(|_| {
597597
ApiError::new(
598-
StatusCode::BadRequest,
598+
StatusCode::BAD_REQUEST,
599599
"400".to_owned(),
600600
format!("not a number: '{}'", value),
601601
)
@@ -607,7 +607,7 @@ fn parse_bool_param(value: Cow<str>) -> Result<bool, ApiError> {
607607
"1" => Ok(true),
608608
"0" => Ok(false),
609609
_ => Err(ApiError::new(
610-
StatusCode::BadRequest,
610+
StatusCode::BAD_REQUEST,
611611
"400".to_owned(),
612612
"not a boolean".to_owned(),
613613
)),
@@ -646,7 +646,7 @@ impl Respond for BuildLogResponder {
646646
ensure!(
647647
value == "entry",
648648
ApiError::new(
649-
StatusCode::BadRequest,
649+
StatusCode::BAD_REQUEST,
650650
"400".to_owned(),
651651
format!("unknown view '{}'", value)
652652
)
@@ -682,7 +682,7 @@ impl Respond for BuildLogResponder {
682682
.get(arch)
683683
.ok_or_else(|| unknown_arch(project_name, repo_name, arch)));
684684
let package = try_api!(arch.packages.get(package_name).ok_or_else(|| ApiError::new(
685-
StatusCode::BadRequest,
685+
StatusCode::BAD_REQUEST,
686686
"400".to_owned(),
687687
format!("remote error: {} no logfile", package_name)
688688
)));
@@ -706,13 +706,13 @@ impl Respond for BuildLogResponder {
706706
xml.add_child(entry_xml).unwrap();
707707
}
708708

709-
ResponseTemplate::new(StatusCode::Ok).set_body_xml(xml)
709+
ResponseTemplate::new(StatusCode::OK).set_body_xml(xml)
710710
} else {
711711
let contents = log.as_ref().map_or("", |log| &log.contents);
712712
ensure!(
713713
start <= contents.len(),
714714
ApiError::new(
715-
StatusCode::BadRequest,
715+
StatusCode::BAD_REQUEST,
716716
"400".to_owned(),
717717
format!("remote error: start out of range {}", start)
718718
)
@@ -727,7 +727,7 @@ impl Respond for BuildLogResponder {
727727
.unwrap_or(end),
728728
);
729729

730-
ResponseTemplate::new(StatusCode::Ok).set_body_string(&contents[start..end])
730+
ResponseTemplate::new(StatusCode::OK).set_body_string(&contents[start..end])
731731
}
732732
}
733733
}
@@ -791,6 +791,6 @@ impl Respond for BuildHistoryResponder {
791791
}
792792
}
793793

794-
ResponseTemplate::new(StatusCode::Ok).set_body_xml(xml)
794+
ResponseTemplate::new(StatusCode::OK).set_body_xml(xml)
795795
}
796796
}

open-build-service-mock/src/api/mod.rs

+51-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{borrow::Cow, fmt::Display, time::SystemTime};
22

3-
use http_types::{auth::BasicAuth, StatusCode};
3+
use http::{header::AUTHORIZATION, StatusCode};
44
use wiremock::{Request, ResponseTemplate};
55
use xml_builder::XMLElement;
66

@@ -10,6 +10,49 @@ pub(crate) use build::*;
1010
mod source;
1111
pub(crate) use source::*;
1212

13+
// BasicAuth Adapted from http-rs/http-types crate
14+
pub struct BasicAuth {
15+
username: String,
16+
password: String,
17+
}
18+
19+
impl BasicAuth {
20+
pub fn new<U: AsRef<str>, P: AsRef<str>>(username: U, password: P) -> Self {
21+
Self {
22+
username: username.as_ref().to_owned(),
23+
password: password.as_ref().to_owned(),
24+
}
25+
}
26+
27+
pub fn from_credentials(credentials: impl AsRef<[u8]>) -> Result<Self, ()> {
28+
use base64ct::{Base64, Encoding};
29+
let credentials = std::str::from_utf8(credentials.as_ref()).map_err(|_| ())?;
30+
let bytes = Base64::decode_vec(credentials).map_err(|_| ())?;
31+
32+
let credentials = String::from_utf8(bytes).map_err(|_| ())?;
33+
34+
let mut iter = credentials.splitn(2, ':');
35+
let username = iter.next();
36+
let password = iter.next();
37+
38+
let (username, password) = match (username, password) {
39+
(Some(username), Some(password)) => (username.to_string(), password.to_string()),
40+
(Some(_), None) => return Err(()),
41+
(None, _) => return Err(()),
42+
};
43+
44+
Ok(Self { username, password })
45+
}
46+
47+
pub fn username(&self) -> &str {
48+
self.username.as_str()
49+
}
50+
51+
pub fn password(&self) -> &str {
52+
self.password.as_str()
53+
}
54+
}
55+
1356
fn build_status_xml(code: &str, summary: Option<String>) -> XMLElement {
1457
let mut status_xml = XMLElement::new("status");
1558
status_xml.add_attribute("code", code);
@@ -69,25 +112,26 @@ impl Display for ApiError {
69112

70113
fn unknown_project(project: String) -> ApiError {
71114
ApiError {
72-
http_status: StatusCode::NotFound,
115+
http_status: StatusCode::NOT_FOUND,
73116
code: "unknown_project".to_owned(),
74117
summary: project,
75118
}
76119
}
77120

78121
fn unknown_package(package: String) -> ApiError {
79-
ApiError::new(StatusCode::NotFound, "unknown_package".to_owned(), package)
122+
ApiError::new(StatusCode::NOT_FOUND, "unknown_package".to_owned(), package)
80123
}
81124

82125
fn check_auth(auth: &BasicAuth, request: &Request) -> Result<(), ApiError> {
83126
let given_auth = request
84127
.headers
85-
.get(&"authorization".into())
86-
.and_then(|auth| auth.last().as_str().strip_prefix("Basic "))
128+
.get(AUTHORIZATION)
129+
.and_then(|auth| auth.to_str().ok())
130+
.and_then(|s| s.strip_prefix("Basic "))
87131
.and_then(|creds| BasicAuth::from_credentials(creds.trim().as_bytes()).ok())
88132
.ok_or_else(|| {
89133
ApiError::new(
90-
StatusCode::Unauthorized,
134+
StatusCode::UNAUTHORIZED,
91135
"authentication_required".to_owned(),
92136
"Authentication required".to_owned(),
93137
)
@@ -97,7 +141,7 @@ fn check_auth(auth: &BasicAuth, request: &Request) -> Result<(), ApiError> {
97141
Ok(())
98142
} else {
99143
Err(ApiError::new(
100-
StatusCode::Unauthorized,
144+
StatusCode::UNAUTHORIZED,
101145
"authentication_required".to_owned(),
102146
format!(
103147
"Unknown user '{}' or invalid password",

0 commit comments

Comments
 (0)