github.com/i0n/terraform@v0.4.3-0.20150506151324-010a39a58ec1/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  
    13  func TestHTTPClient_impl(t *testing.T) {
    14  	var _ Client = new(HTTPClient)
    15  }
    16  
    17  func TestHTTPClient(t *testing.T) {
    18  	handler := new(testHTTPHandler)
    19  	ts := httptest.NewServer(http.HandlerFunc(handler.Handle))
    20  	defer ts.Close()
    21  
    22  	url, err := url.Parse(ts.URL)
    23  	if err != nil {
    24  		t.Fatalf("err: %s", err)
    25  	}
    26  
    27  	client := &HTTPClient{URL: url}
    28  	testClient(t, client)
    29  }
    30  
    31  type testHTTPHandler struct {
    32  	Data []byte
    33  }
    34  
    35  func (h *testHTTPHandler) Handle(w http.ResponseWriter, r *http.Request) {
    36  	switch r.Method {
    37  	case "GET":
    38  		w.Write(h.Data)
    39  	case "POST":
    40  		buf := new(bytes.Buffer)
    41  		if _, err := io.Copy(buf, r.Body); err != nil {
    42  			w.WriteHeader(500)
    43  		}
    44  
    45  		h.Data = buf.Bytes()
    46  	case "DELETE":
    47  		h.Data = nil
    48  		w.WriteHeader(200)
    49  	default:
    50  		w.WriteHeader(500)
    51  		w.Write([]byte(fmt.Sprintf("Unknown method: %s", r.Method)))
    52  	}
    53  }