diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2018-08-20 20:16:47 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2018-08-20 21:22:18 +0200 |
commit | b54b523458a4f432874051490e1873e1d7f396ed (patch) | |
tree | 62b090915b6f3cf178a9ba18d00c8cb4b8960ad3 | |
parent | d91bfe175d451a2de61b6682dfda9171678195b3 (diff) | |
download | rust-zotapi-b54b523458a4f432874051490e1873e1d7f396ed.tar.gz rust-zotapi-b54b523458a4f432874051490e1873e1d7f396ed.tar.bz2 rust-zotapi-b54b523458a4f432874051490e1873e1d7f396ed.zip |
Allow setting title for new items.
-rw-r--r-- | src/item.rs | 15 | ||||
-rw-r--r-- | tests/zotapi.rs | 21 |
2 files changed, 32 insertions, 4 deletions
diff --git a/src/item.rs b/src/item.rs index 7fb751d..3440fd5 100644 --- a/src/item.rs +++ b/src/item.rs @@ -16,25 +16,32 @@ use client::{self, Client}; use error::Error; +use std::collections::BTreeMap; pub struct ItemBuilder<'a> { client : &'a Client, - body: &'a str, + data : BTreeMap<&'a str, &'a str>, } impl<'a> ItemBuilder<'a> { pub fn new(client: &'a Client) -> ItemBuilder<'a> { ItemBuilder { client: client, - body: "", + data: BTreeMap::new(), } } + + pub fn title(&mut self, text: &'a str) -> &mut ItemBuilder<'a> { + self.data.insert("title", text); + self + } + pub fn body(&mut self, text: &'a str) -> &mut ItemBuilder<'a> { - self.body = text; + self.data.insert("body", text); self } pub fn create(&self) -> Result<String, Error> { - self.client.post_data(client::ZOTAPI_ITEM_UPDATE_PATH, &[("body", self.body)]) + self.client.post_data(client::ZOTAPI_ITEM_UPDATE_PATH, &self.data) } } diff --git a/tests/zotapi.rs b/tests/zotapi.rs index 0437313..49dde02 100644 --- a/tests/zotapi.rs +++ b/tests/zotapi.rs @@ -79,3 +79,24 @@ fn create_new_post() { m.assert(); } + +#[test] +fn create_new_post_with_title() { + // Note this regex is not as good as it could be. But since the rust regex crate + // don't support back references it's the best we can do and not care about the + // oder of the entries. + let expected_body = Matcher::Regex(r"(title=A\+title&?|body=This\+is\+a\+test&?){2}".into()); + + let m = mock("POST", "/api/z/1.0/item/update") + .match_header("Authorization", Matcher::Regex(r"Basic \w+".into())) + .match_body(expected_body) + .with_status(200) + .with_header("content-type", "application/json") + .with_body("{}") + .create(); + + let z = zotapi::client(&format!("http://{}", mockito::SERVER_ADDRESS), "testuser", "test1234"); + let _res = z.item().title("A title").body("This is a test").create(); + + m.assert(); +} |