From 3d4642719f273a97c1c65c142c476bda52d4821c Mon Sep 17 00:00:00 2001 From: jwa Date: Fri, 19 Jan 2018 02:30:21 +0000 Subject: [PATCH] simulate a webdav backend --- state/remote/http_test.go | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/state/remote/http_test.go b/state/remote/http_test.go index 904d830b1..214dffa5c 100644 --- a/state/remote/http_test.go +++ b/state/remote/http_test.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httptest" "net/url" + "reflect" "testing" "github.com/hashicorp/go-cleanhttp" @@ -60,6 +61,19 @@ func TestHTTPClient(t *testing.T) { } TestRemoteLocks(t, a, b) + // test a WebDAV-ish backend + davhandler := new(testHTTPHandler) + ts = httptest.NewServer(http.HandlerFunc(davhandler.HandleWebDAV)) + defer ts.Close() + + url, err = url.Parse(ts.URL) + c := &HTTPClient{ + URL: url, + UpdateMethod: "PUT", + Client: cleanhttp.DefaultClient(), + } + testClient(t, c) // first time through: 201 + testClient(t, c) // second time, with identical data: 204 } func assertError(t *testing.T, err error, expected string) { @@ -171,3 +185,29 @@ func (h *testHTTPHandler) Handle(w http.ResponseWriter, r *http.Request) { w.Write([]byte(fmt.Sprintf("Unknown method: %s", r.Method))) } } + +// mod_dav-ish behavior +func (h *testHTTPHandler) HandleWebDAV(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case "GET": + w.Write(h.Data) + case "PUT": + buf := new(bytes.Buffer) + if _, err := io.Copy(buf, r.Body); err != nil { + w.WriteHeader(500) + } + if reflect.DeepEqual(h.Data, buf.Bytes()) { + h.Data = buf.Bytes() + w.WriteHeader(204) + } else { + h.Data = buf.Bytes() + w.WriteHeader(201) + } + case "DELETE": + h.Data = nil + w.WriteHeader(200) + default: + w.WriteHeader(500) + w.Write([]byte(fmt.Sprintf("Unknown method: %s", r.Method))) + } +}