state/remote: real HTTP client tests

This commit is contained in:
Mitchell Hashimoto 2015-02-23 08:32:55 -08:00
parent 0fcc417ddd
commit 5555059540
2 changed files with 46 additions and 2 deletions

View File

@ -70,6 +70,11 @@ func (c *HTTPClient) Get() (*Payload, error) {
Data: buf.Bytes(),
}
// If there was no data, then return nil
if len(payload.Data) == 0 {
return nil, nil
}
// Check for the MD5
if raw := resp.Header.Get("Content-MD5"); raw != "" {
md5, err := base64.StdEncoding.DecodeString(raw)

View File

@ -1,6 +1,12 @@
package remote
import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
@ -9,6 +15,39 @@ func TestHTTPClient_impl(t *testing.T) {
}
func TestHTTPClient(t *testing.T) {
// TODO
//testClient(t, client)
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)
}
client := &HTTPClient{URL: url}
testClient(t, client)
}
type testHTTPHandler struct {
Data []byte
}
func (h *testHTTPHandler) Handle(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
w.Write(h.Data)
case "POST":
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, r.Body); err != nil {
w.WriteHeader(500)
}
h.Data = buf.Bytes()
case "DELETE":
h.Data = nil
w.WriteHeader(200)
default:
w.WriteHeader(500)
w.Write([]byte(fmt.Sprintf("Unknown method: %s", r.Method)))
}
}