diff options
author | Quentin Dufour <quentin@deuxfleurs.fr> | 2024-05-22 23:22:03 +0200 |
---|---|---|
committer | Quentin Dufour <quentin@deuxfleurs.fr> | 2024-05-22 23:22:03 +0200 |
commit | 2ca485fb87125b543307748e73b04bcd68f2d9ad (patch) | |
tree | 5fa7f5b04fb2afc6929413f8bbc0497a4322483c | |
parent | e522251bec3519b4ca867e6ef5131c6fdf6cd2b1 (diff) | |
download | aerogramme-2ca485fb87125b543307748e73b04bcd68f2d9ad.tar.gz aerogramme-2ca485fb87125b543307748e73b04bcd68f2d9ad.zip |
test webdav core get, delete, update
-rw-r--r-- | aero-proto/src/dav/controller.rs | 10 | ||||
-rw-r--r-- | aerogramme/tests/behavior.rs | 34 |
2 files changed, 41 insertions, 3 deletions
diff --git a/aero-proto/src/dav/controller.rs b/aero-proto/src/dav/controller.rs index 306b035..0a47cf4 100644 --- a/aero-proto/src/dav/controller.rs +++ b/aero-proto/src/dav/controller.rs @@ -223,7 +223,15 @@ impl Controller { }) .boxed(); - let etag = self.node.put(put_policy, stream_of_bytes).await?; + let etag = match self.node.put(put_policy, stream_of_bytes).await { + Ok(etag) => etag, + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { + tracing::warn!("put pre-condition failed"); + let response = Response::builder().status(412).body(text_body(""))?; + return Ok(response); + } + Err(e) => Err(e)?, + }; let response = Response::builder() .status(201) diff --git a/aerogramme/tests/behavior.rs b/aerogramme/tests/behavior.rs index c514f06..18095ef 100644 --- a/aerogramme/tests/behavior.rs +++ b/aerogramme/tests/behavior.rs @@ -465,24 +465,54 @@ fn rfc4918_webdav_core() { assert_eq!(multistatus.responses.len(), 1); // --- PUT --- - let resp = http.request(reqwest::Method::from_bytes(b"PUT")?, "http://localhost:8087/alice/calendar/Personal/rfc2.ics").header("If-None-Match", "*").body(ICAL_RFC2).send()?; + // first object + let resp = http.put("http://localhost:8087/alice/calendar/Personal/rfc2.ics").header("If-None-Match", "*").body(ICAL_RFC2).send()?; + let obj1_etag = resp.headers().get("etag").expect("etag must be set"); assert_eq!(resp.status(), 201); let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087/alice/calendar/Personal/").header("Depth", "1").send()?.text()?; let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); assert_eq!(multistatus.responses.len(), 2); - let resp = http.request(reqwest::Method::from_bytes(b"PUT")?, "http://localhost:8087/alice/calendar/Personal/rfc3.ics").header("If-None-Match", "*").body(ICAL_RFC3).send()?; + // second object + let resp = http.put("http://localhost:8087/alice/calendar/Personal/rfc3.ics").header("If-None-Match", "*").body(ICAL_RFC3).send()?; assert_eq!(resp.status(), 201); let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087/alice/calendar/Personal/").header("Depth", "1").send()?.text()?; let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); assert_eq!(multistatus.responses.len(), 3); + // can't create an event on an existing path + let resp = http.put("http://localhost:8087/alice/calendar/Personal/rfc2.ics").header("If-None-Match", "*").body(ICAL_RFC1).send()?; + assert_eq!(resp.status(), 412); + + // update first object by knowing its ETag + let resp = http.put("http://localhost:8087/alice/calendar/Personal/rfc2.ics").header("If-Match", obj1_etag).body(ICAL_RFC1).send()?; + assert_eq!(resp.status(), 201); + // --- GET --- + let body = http.get("http://localhost:8087/alice/calendar/Personal/rfc2.ics").send()?.text()?; + assert_eq!(body.as_bytes(), ICAL_RFC1); + + let body = http.get("http://localhost:8087/alice/calendar/Personal/rfc3.ics").send()?.text()?; + assert_eq!(body.as_bytes(), ICAL_RFC3); // --- DELETE --- + // delete 1st object + let resp = http.delete("http://localhost:8087/alice/calendar/Personal/rfc2.ics").send()?; + assert_eq!(resp.status(), 204); + + let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087/alice/calendar/Personal/").header("Depth", "1").send()?.text()?; + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); + assert_eq!(multistatus.responses.len(), 2); + + // delete 2nd object + let resp = http.delete("http://localhost:8087/alice/calendar/Personal/rfc3.ics").send()?; + assert_eq!(resp.status(), 204); + let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087/alice/calendar/Personal/").header("Depth", "1").send()?.text()?; + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); + assert_eq!(multistatus.responses.len(), 1); Ok(()) }) |