2018-03-16 20:53:21 -05:00
|
|
|
package http
|
2015-02-21 20:09:46 -06:00
|
|
|
|
|
|
|
import (
|
2015-02-23 10:32:55 -06:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2018-01-18 20:30:21 -06:00
|
|
|
"reflect"
|
2015-02-21 20:09:46 -06:00
|
|
|
"testing"
|
2015-10-22 13:03:25 -05:00
|
|
|
|
2019-06-05 15:12:07 -05:00
|
|
|
"github.com/hashicorp/go-retryablehttp"
|
2018-03-16 20:53:21 -05:00
|
|
|
"github.com/hashicorp/terraform/state/remote"
|
2015-02-21 20:09:46 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHTTPClient_impl(t *testing.T) {
|
2018-03-16 20:53:21 -05:00
|
|
|
var _ remote.Client = new(httpClient)
|
|
|
|
var _ remote.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)
|
|
|
|
}
|
|
|
|
|
2017-08-20 07:59:04 -05:00
|
|
|
// Test basic get/update
|
2019-06-05 15:12:07 -05:00
|
|
|
client := &httpClient{URL: url, Client: retryablehttp.NewClient()}
|
2018-03-16 20:53:21 -05:00
|
|
|
remote.TestClient(t, client)
|
2017-08-13 11:16:42 -05:00
|
|
|
|
2018-01-11 16:09:59 -06:00
|
|
|
// test just a single PUT
|
2018-03-16 20:53:21 -05:00
|
|
|
p := &httpClient{
|
2018-01-11 16:09:59 -06:00
|
|
|
URL: url,
|
|
|
|
UpdateMethod: "PUT",
|
2019-06-05 15:12:07 -05:00
|
|
|
Client: retryablehttp.NewClient(),
|
2018-01-11 16:09:59 -06:00
|
|
|
}
|
2018-03-16 20:53:21 -05:00
|
|
|
remote.TestClient(t, p)
|
2018-01-11 16:09:59 -06:00
|
|
|
|
2017-08-20 07:59:04 -05:00
|
|
|
// Test locking and alternative UpdateMethod
|
2018-03-16 20:53:21 -05:00
|
|
|
a := &httpClient{
|
2017-08-19 12:31:47 -05:00
|
|
|
URL: url,
|
2017-08-20 07:59:04 -05:00
|
|
|
UpdateMethod: "PUT",
|
2017-08-19 12:31:47 -05:00
|
|
|
LockURL: url,
|
|
|
|
LockMethod: "LOCK",
|
|
|
|
UnlockURL: url,
|
|
|
|
UnlockMethod: "UNLOCK",
|
2019-06-05 15:12:07 -05:00
|
|
|
Client: retryablehttp.NewClient(),
|
2017-08-19 12:31:47 -05:00
|
|
|
}
|
2018-03-16 20:53:21 -05:00
|
|
|
b := &httpClient{
|
2017-08-19 12:31:47 -05:00
|
|
|
URL: url,
|
2017-08-20 07:59:04 -05:00
|
|
|
UpdateMethod: "PUT",
|
2017-08-19 12:31:47 -05:00
|
|
|
LockURL: url,
|
|
|
|
LockMethod: "LOCK",
|
|
|
|
UnlockURL: url,
|
|
|
|
UnlockMethod: "UNLOCK",
|
2019-06-05 15:12:07 -05:00
|
|
|
Client: retryablehttp.NewClient(),
|
2017-08-19 12:31:47 -05:00
|
|
|
}
|
2018-03-16 20:53:21 -05:00
|
|
|
remote.TestRemoteLocks(t, a, b)
|
2017-08-19 13:17:25 -05:00
|
|
|
|
2018-01-18 20:30:21 -06:00
|
|
|
// test a WebDAV-ish backend
|
|
|
|
davhandler := new(testHTTPHandler)
|
|
|
|
ts = httptest.NewServer(http.HandlerFunc(davhandler.HandleWebDAV))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
url, err = url.Parse(ts.URL)
|
2019-06-05 15:12:07 -05:00
|
|
|
client = &httpClient{
|
2018-01-18 20:30:21 -06:00
|
|
|
URL: url,
|
|
|
|
UpdateMethod: "PUT",
|
2019-06-05 15:12:07 -05:00
|
|
|
Client: retryablehttp.NewClient(),
|
2018-01-18 20:30:21 -06:00
|
|
|
}
|
2019-06-05 15:12:07 -05:00
|
|
|
remote.TestClient(t, client) // first time through: 201
|
|
|
|
remote.TestClient(t, client) // second time, with identical data: 204
|
|
|
|
|
|
|
|
// test a broken backend
|
|
|
|
brokenHandler := new(testBrokenHTTPHandler)
|
|
|
|
brokenHandler.handler = new(testHTTPHandler)
|
|
|
|
ts = httptest.NewServer(http.HandlerFunc(brokenHandler.Handle))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
url, err = url.Parse(ts.URL)
|
|
|
|
client = &httpClient{URL: url, Client: retryablehttp.NewClient()}
|
|
|
|
remote.TestClient(t, client)
|
2017-08-19 13:17:25 -05:00
|
|
|
}
|
|
|
|
|
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)
|
2018-01-11 16:09:59 -06:00
|
|
|
case "PUT":
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if _, err := io.Copy(buf, r.Body); err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
}
|
|
|
|
w.WriteHeader(201)
|
|
|
|
h.Data = buf.Bytes()
|
|
|
|
case "POST":
|
2017-08-19 12:31:47 -05:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if _, err := io.Copy(buf, r.Body); err != nil {
|
|
|
|
w.WriteHeader(500)
|
2015-02-23 10:32:55 -06:00
|
|
|
}
|
2017-08-19 12:31:47 -05:00
|
|
|
h.Data = buf.Bytes()
|
|
|
|
case "LOCK":
|
|
|
|
if h.Locked {
|
2017-08-19 13:17:25 -05:00
|
|
|
w.WriteHeader(423)
|
2017-08-19 12:31:47 -05:00
|
|
|
} 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
|
|
|
}
|
2018-01-18 20:30:21 -06:00
|
|
|
|
|
|
|
// 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)))
|
|
|
|
}
|
|
|
|
}
|
2019-06-05 15:12:07 -05:00
|
|
|
|
|
|
|
type testBrokenHTTPHandler struct {
|
|
|
|
lastRequestWasBroken bool
|
|
|
|
handler *testHTTPHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *testBrokenHTTPHandler) Handle(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if h.lastRequestWasBroken {
|
|
|
|
h.lastRequestWasBroken = false
|
|
|
|
h.handler.Handle(w, r)
|
|
|
|
} else {
|
|
|
|
h.lastRequestWasBroken = true
|
|
|
|
w.WriteHeader(500)
|
|
|
|
}
|
|
|
|
}
|