2015-02-21 20:09:46 -06:00
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
2015-02-23 10:32:55 -06:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2015-02-21 20:09:46 -06:00
|
|
|
"testing"
|
2015-10-22 13:03:25 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-cleanhttp"
|
2015-02-21 20:09:46 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHTTPClient_impl(t *testing.T) {
|
|
|
|
var _ Client = new(HTTPClient)
|
2017-08-13 11:16:42 -05:00
|
|
|
var _ ClientLocker = new(HTTPClient)
|
2015-02-21 20:09:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPClient(t *testing.T) {
|
2015-02-23 10:32:55 -06:00
|
|
|
handler := new(testHTTPHandler)
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(handler.Handle))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
url, err := url.Parse(ts.URL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-10-22 13:03:25 -05:00
|
|
|
client := &HTTPClient{URL: url, Client: cleanhttp.DefaultClient()}
|
2015-02-23 10:32:55 -06:00
|
|
|
testClient(t, client)
|
2017-08-13 11:16:42 -05:00
|
|
|
|
|
|
|
a := &HTTPClient{URL: url, Client: cleanhttp.DefaultClient(), SupportsLocking: true}
|
|
|
|
b := &HTTPClient{URL: url, Client: cleanhttp.DefaultClient(), SupportsLocking: true}
|
|
|
|
TestRemoteLocks(t, a, b)
|
2015-02-23 10:32:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type testHTTPHandler struct {
|
2017-08-13 11:16:42 -05:00
|
|
|
Data []byte
|
|
|
|
Locked bool
|
2015-02-23 10:32:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *testHTTPHandler) Handle(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
|
|
|
w.Write(h.Data)
|
|
|
|
case "POST":
|
2017-08-13 11:16:42 -05:00
|
|
|
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
|
2015-02-23 10:32:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
case "DELETE":
|
|
|
|
h.Data = nil
|
|
|
|
w.WriteHeader(200)
|
|
|
|
default:
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte(fmt.Sprintf("Unknown method: %s", r.Method)))
|
|
|
|
}
|
2015-02-21 20:09:46 -06:00
|
|
|
}
|