github.com/aznashwan/terraform@v0.4.3-0.20151118032030-21f93ca4558d/state/remote/http_test.go (about) 1 package remote 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "net/http" 8 "net/http/httptest" 9 "net/url" 10 "testing" 11 12 "github.com/hashicorp/go-cleanhttp" 13 ) 14 15 func TestHTTPClient_impl(t *testing.T) { 16 var _ Client = new(HTTPClient) 17 } 18 19 func TestHTTPClient(t *testing.T) { 20 handler := new(testHTTPHandler) 21 ts := httptest.NewServer(http.HandlerFunc(handler.Handle)) 22 defer ts.Close() 23 24 url, err := url.Parse(ts.URL) 25 if err != nil { 26 t.Fatalf("err: %s", err) 27 } 28 29 client := &HTTPClient{URL: url, Client: cleanhttp.DefaultClient()} 30 testClient(t, client) 31 } 32 33 type testHTTPHandler struct { 34 Data []byte 35 } 36 37 func (h *testHTTPHandler) Handle(w http.ResponseWriter, r *http.Request) { 38 switch r.Method { 39 case "GET": 40 w.Write(h.Data) 41 case "POST": 42 buf := new(bytes.Buffer) 43 if _, err := io.Copy(buf, r.Body); err != nil { 44 w.WriteHeader(500) 45 } 46 47 h.Data = buf.Bytes() 48 case "DELETE": 49 h.Data = nil 50 w.WriteHeader(200) 51 default: 52 w.WriteHeader(500) 53 w.Write([]byte(fmt.Sprintf("Unknown method: %s", r.Method))) 54 } 55 }