Add tests for state/http with locking

This commit is contained in:
Ross McFarland 2017-08-13 09:16:42 -07:00
parent 1d38569c91
commit ce4d9fb3c2
1 changed files with 24 additions and 5 deletions

View File

@ -14,6 +14,7 @@ import (
func TestHTTPClient_impl(t *testing.T) {
var _ Client = new(HTTPClient)
var _ ClientLocker = new(HTTPClient)
}
func TestHTTPClient(t *testing.T) {
@ -28,10 +29,15 @@ func TestHTTPClient(t *testing.T) {
client := &HTTPClient{URL: url, Client: cleanhttp.DefaultClient()}
testClient(t, client)
a := &HTTPClient{URL: url, Client: cleanhttp.DefaultClient(), SupportsLocking: true}
b := &HTTPClient{URL: url, Client: cleanhttp.DefaultClient(), SupportsLocking: true}
TestRemoteLocks(t, a, b)
}
type testHTTPHandler struct {
Data []byte
Data []byte
Locked bool
}
func (h *testHTTPHandler) Handle(w http.ResponseWriter, r *http.Request) {
@ -39,12 +45,25 @@ func (h *testHTTPHandler) Handle(w http.ResponseWriter, r *http.Request) {
case "GET":
w.Write(h.Data)
case "POST":
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, r.Body); err != nil {
w.WriteHeader(500)
switch r.URL.Path {
case "/":
// state
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, r.Body); err != nil {
w.WriteHeader(500)
}
h.Data = buf.Bytes()
case "/lock":
if h.Locked {
w.WriteHeader(409)
} else {
h.Locked = true
}
case "/unlock":
h.Locked = false
}
h.Data = buf.Bytes()
case "DELETE":
h.Data = nil
w.WriteHeader(200)