github.com/hooklift/terraform@v0.11.0-beta1.0.20171117000744-6786c1361ffe/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  	var _ ClientLocker = new(HTTPClient)
    18  }
    19  
    20  func TestHTTPClient(t *testing.T) {
    21  	handler := new(testHTTPHandler)
    22  	ts := httptest.NewServer(http.HandlerFunc(handler.Handle))
    23  	defer ts.Close()
    24  
    25  	url, err := url.Parse(ts.URL)
    26  	if err != nil {
    27  		t.Fatalf("err: %s", err)
    28  	}
    29  
    30  	// Test basic get/update
    31  	client := &HTTPClient{URL: url, Client: cleanhttp.DefaultClient()}
    32  	testClient(t, client)
    33  
    34  	// Test locking and alternative UpdateMethod
    35  	a := &HTTPClient{
    36  		URL:          url,
    37  		UpdateMethod: "PUT",
    38  		LockURL:      url,
    39  		LockMethod:   "LOCK",
    40  		UnlockURL:    url,
    41  		UnlockMethod: "UNLOCK",
    42  		Client:       cleanhttp.DefaultClient(),
    43  	}
    44  	b := &HTTPClient{
    45  		URL:          url,
    46  		UpdateMethod: "PUT",
    47  		LockURL:      url,
    48  		LockMethod:   "LOCK",
    49  		UnlockURL:    url,
    50  		UnlockMethod: "UNLOCK",
    51  		Client:       cleanhttp.DefaultClient(),
    52  	}
    53  	TestRemoteLocks(t, a, b)
    54  
    55  }
    56  
    57  func assertError(t *testing.T, err error, expected string) {
    58  	if err == nil {
    59  		t.Fatalf("Expected empty config to err")
    60  	} else if err.Error() != expected {
    61  		t.Fatalf("Expected err.Error() to be \"%s\", got \"%s\"", expected, err.Error())
    62  	}
    63  }
    64  
    65  func TestHTTPClientFactory(t *testing.T) {
    66  	// missing address
    67  	_, err := httpFactory(map[string]string{})
    68  	assertError(t, err, "missing 'address' configuration")
    69  
    70  	// defaults
    71  	conf := map[string]string{
    72  		"address": "http://127.0.0.1:8888/foo",
    73  	}
    74  	c, err := httpFactory(conf)
    75  	client, _ := c.(*HTTPClient)
    76  	if client == nil || err != nil {
    77  		t.Fatal("Unexpected failure, address")
    78  	}
    79  	if client.URL.String() != conf["address"] {
    80  		t.Fatalf("Expected address \"%s\", got \"%s\"", conf["address"], client.URL.String())
    81  	}
    82  	if client.UpdateMethod != "POST" {
    83  		t.Fatalf("Expected update_method \"%s\", got \"%s\"", "POST", client.UpdateMethod)
    84  	}
    85  	if client.LockURL != nil || client.LockMethod != "LOCK" {
    86  		t.Fatal("Unexpected lock_address or lock_method")
    87  	}
    88  	if client.UnlockURL != nil || client.UnlockMethod != "UNLOCK" {
    89  		t.Fatal("Unexpected unlock_address or unlock_method")
    90  	}
    91  	if client.Username != "" || client.Password != "" {
    92  		t.Fatal("Unexpected username or password")
    93  	}
    94  
    95  	// custom
    96  	conf = map[string]string{
    97  		"address":        "http://127.0.0.1:8888/foo",
    98  		"update_method":  "BLAH",
    99  		"lock_address":   "http://127.0.0.1:8888/bar",
   100  		"lock_method":    "BLIP",
   101  		"unlock_address": "http://127.0.0.1:8888/baz",
   102  		"unlock_method":  "BLOOP",
   103  		"username":       "user",
   104  		"password":       "pass",
   105  	}
   106  	c, err = httpFactory(conf)
   107  	client, _ = c.(*HTTPClient)
   108  	if client == nil || err != nil {
   109  		t.Fatal("Unexpected failure, update_method")
   110  	}
   111  	if client.UpdateMethod != "BLAH" {
   112  		t.Fatalf("Expected update_method \"%s\", got \"%s\"", "BLAH", client.UpdateMethod)
   113  	}
   114  	if client.LockURL.String() != conf["lock_address"] || client.LockMethod != "BLIP" {
   115  		t.Fatalf("Unexpected lock_address \"%s\" vs \"%s\" or lock_method \"%s\" vs \"%s\"", client.LockURL.String(),
   116  			conf["lock_address"], client.LockMethod, conf["lock_method"])
   117  	}
   118  	if client.UnlockURL.String() != conf["unlock_address"] || client.UnlockMethod != "BLOOP" {
   119  		t.Fatalf("Unexpected unlock_address \"%s\" vs \"%s\" or unlock_method \"%s\" vs \"%s\"", client.UnlockURL.String(),
   120  			conf["unlock_address"], client.UnlockMethod, conf["unlock_method"])
   121  	}
   122  	if client.Username != "user" || client.Password != "pass" {
   123  		t.Fatalf("Unexpected username \"%s\" vs \"%s\" or password \"%s\" vs \"%s\"", client.Username, conf["username"],
   124  			client.Password, conf["password"])
   125  	}
   126  }
   127  
   128  type testHTTPHandler struct {
   129  	Data   []byte
   130  	Locked bool
   131  }
   132  
   133  func (h *testHTTPHandler) Handle(w http.ResponseWriter, r *http.Request) {
   134  	switch r.Method {
   135  	case "GET":
   136  		w.Write(h.Data)
   137  	case "POST", "PUT":
   138  		buf := new(bytes.Buffer)
   139  		if _, err := io.Copy(buf, r.Body); err != nil {
   140  			w.WriteHeader(500)
   141  		}
   142  
   143  		h.Data = buf.Bytes()
   144  	case "LOCK":
   145  		if h.Locked {
   146  			w.WriteHeader(423)
   147  		} else {
   148  			h.Locked = true
   149  		}
   150  	case "UNLOCK":
   151  		h.Locked = false
   152  	case "DELETE":
   153  		h.Data = nil
   154  		w.WriteHeader(200)
   155  	default:
   156  		w.WriteHeader(500)
   157  		w.Write([]byte(fmt.Sprintf("Unknown method: %s", r.Method)))
   158  	}
   159  }