simulate a webdav backend

This commit is contained in:
jwa 2018-01-19 02:30:21 +00:00
parent b3d432e6bc
commit 3d4642719f
1 changed files with 40 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"reflect"
"testing" "testing"
"github.com/hashicorp/go-cleanhttp" "github.com/hashicorp/go-cleanhttp"
@ -60,6 +61,19 @@ func TestHTTPClient(t *testing.T) {
} }
TestRemoteLocks(t, a, b) 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) { 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))) 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)))
}
}