From e4e75aa8c85b15b91ef57b4288e620bf3d906500 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 16 Dec 2018 23:03:33 +0100 Subject: Upload one file should now work. --- src/client.rs | 17 ++++++++++++----- tests/fixtures/testfile.txt | 1 + tests/zotapi.rs | 16 ++++++++++++++-- 3 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 tests/fixtures/testfile.txt diff --git a/src/client.rs b/src/client.rs index 374cfb2..3dbca36 100644 --- a/src/client.rs +++ b/src/client.rs @@ -23,6 +23,7 @@ use reqwest::{ }; use serde::Serialize; use serde_urlencoded; +use std::collections::BTreeMap; use std::io::Read; use abook::AbookFetcher; use abconfig::ABConfigFetcher; @@ -133,17 +134,23 @@ impl Client { } } - pub fn post_multipart(&self, path: &str, _data: &T, _files: &Vec<&str>) -> Result - where T: Serialize, + pub fn post_multipart(&self, path: &str, data: &BTreeMap<&str, &str>, files: &Vec<&str>) -> Result { let url = self.url(path, &()); - let f = reqwest::multipart::Form::new() - .text("balle", "klorin"); + let mut form = reqwest::multipart::Form::new(); + + for (key, value) in data.iter() { + form = form.text(key.to_string(), value.to_string()); + } + + for f in files.iter() { + form = form.file("files", f).unwrap(); + } let res = self.inner.post(&url) .basic_auth(self.user.clone(), Some(self.pw.clone())) .header(CONTENT_TYPE, "multipart/form-data") - .multipart(f) + .multipart(form) .send()?; match res.status() { diff --git a/tests/fixtures/testfile.txt b/tests/fixtures/testfile.txt new file mode 100644 index 0000000..bf48860 --- /dev/null +++ b/tests/fixtures/testfile.txt @@ -0,0 +1 @@ +testfile contents diff --git a/tests/zotapi.rs b/tests/zotapi.rs index 37979fd..3105240 100644 --- a/tests/zotapi.rs +++ b/tests/zotapi.rs @@ -104,8 +104,20 @@ fn create_new_post_with_title() { #[test] fn upload_item_with_media_file() { let m = mock("POST", "/api/z/1.0/item/update") - .match_header("authorization", Matcher::Regex(r"Basic \w+".into())) + .match_header("authorization", Matcher::Regex(r"Basic \w+=".into())) .match_header("content-type", Matcher::Regex("^multipart/form-data; boundary=.+$".into())) + .match_body(Matcher::Regex( + "--.+\r\n".to_owned() + + "Content-Disposition: form-data; name=\"body\"\r\n" + + "\r\nThis is a test\r\n" + + "--.+\r\n" + + "Content-Disposition: form-data; name=\"title\"\r\n" + + "\r\nA title\r\n" + + "--.+\r\n" + + "Content-Disposition: form-data; name=\"files\"; filename=\"testfile.txt\"\r\n" + + "Content-Type: text/plain\r\n" + + "\r\ntestfile contents\n" + + "\r\n--.+--\r\n")) .with_status(200) .with_header("content-type", "application/json") .with_body("{}") @@ -115,7 +127,7 @@ fn upload_item_with_media_file() { let _res = z.item() .title("A title") .body("This is a test") - .file("testfile.jpg") + .file("tests/fixtures/testfile.txt") .create(); m.assert(); -- cgit v1.2.3