Revert fixes of response handling and fix tests

This commit is contained in:
Takayuki Nakata 2020-01-22 09:19:59 +09:00
parent 798f9942c8
commit 58ae3d06b9
3 changed files with 10 additions and 49 deletions

View File

@ -149,41 +149,15 @@ impl Registry {
pub fn add_owners(&mut self, krate: &str, owners: &[&str]) -> Result<String> { pub fn add_owners(&mut self, krate: &str, owners: &[&str]) -> Result<String> {
let body = serde_json::to_string(&OwnersReq { users: owners })?; let body = serde_json::to_string(&OwnersReq { users: owners })?;
let body = self.put(&format!("/crates/{}/owners", krate), body.as_bytes())?; let body = self.put(&format!("/crates/{}/owners", krate), body.as_bytes())?;
let body = if body.is_empty() { assert!(serde_json::from_str::<OwnerResponse>(&body)?.ok);
r#"{"ok":false,"msg":"response body is empty"}"#.parse()? Ok(serde_json::from_str::<OwnerResponse>(&body)?.msg)
} else {
body
};
match serde_json::from_str::<OwnerResponse>(&body) {
Ok(response) => {
if response.ok {
Ok(response.msg)
} else {
bail!("{}", response.msg)
}
}
_ => bail!("failed to parse response body"),
}
} }
pub fn remove_owners(&mut self, krate: &str, owners: &[&str]) -> Result<String> { pub fn remove_owners(&mut self, krate: &str, owners: &[&str]) -> Result<()> {
let body = serde_json::to_string(&OwnersReq { users: owners })?; let body = serde_json::to_string(&OwnersReq { users: owners })?;
let body = self.delete(&format!("/crates/{}/owners", krate), Some(body.as_bytes()))?; let body = self.delete(&format!("/crates/{}/owners", krate), Some(body.as_bytes()))?;
let body = if body.is_empty() { assert!(serde_json::from_str::<OwnerResponse>(&body)?.ok);
r#"{"ok":false,"msg":"response body is empty"}"#.parse()? Ok(())
} else {
body
};
match serde_json::from_str::<OwnerResponse>(&body) {
Ok(response) => {
if response.ok {
Ok(response.msg)
} else {
bail!("{}", response.msg)
}
}
_ => bail!("failed to parse response body"),
}
} }
pub fn list_owners(&mut self, krate: &str) -> Result<Vec<User>> { pub fn list_owners(&mut self, krate: &str) -> Result<Vec<User>> {
@ -298,21 +272,8 @@ impl Registry {
pub fn unyank(&mut self, krate: &str, version: &str) -> Result<()> { pub fn unyank(&mut self, krate: &str, version: &str) -> Result<()> {
let body = self.put(&format!("/crates/{}/{}/unyank", krate, version), &[])?; let body = self.put(&format!("/crates/{}/{}/unyank", krate, version), &[])?;
let body = if body.is_empty() { assert!(serde_json::from_str::<R>(&body)?.ok);
r#"{"ok":false}"#.parse()?
} else {
body
};
match serde_json::from_str::<R>(&body) {
Ok(response) => {
if response.ok {
Ok(()) Ok(())
} else {
bail!("ok is false in response body")
}
}
_ => bail!("failed to parse response body"),
}
} }
fn put(&mut self, path: &str, b: &[u8]) -> Result<String> { fn put(&mut self, path: &str, b: &[u8]) -> Result<String> {

View File

@ -76,7 +76,7 @@ fn simple_add() {
.with_status(101) .with_status(101)
.with_stderr( .with_stderr(
" Updating `[..]` index " Updating `[..]` index
error: failed to invite owners to crate foo: response body is empty", error: failed to invite owners to crate foo: EOF while parsing a value at line 1 column 0",
) )
.run(); .run();
} }
@ -110,7 +110,7 @@ fn simple_remove() {
error: failed to remove owners from crate foo error: failed to remove owners from crate foo
Caused by: Caused by:
response body is empty", EOF while parsing a value at line 1 column 0",
) )
.run(); .run();
} }

View File

@ -45,7 +45,7 @@ fn simple() {
error: failed to undo a yank error: failed to undo a yank
Caused by: Caused by:
ok is false in response body", EOF while parsing a value at line 1 column 0",
) )
.run(); .run();
} }