testutil: working on helpers

This commit is contained in:
Ryan Uber 2015-03-10 18:08:14 -07:00
parent 78f9f53bf1
commit 576c49eaf0

View File

@ -1,8 +1,10 @@
package testutil package testutil
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
@ -59,6 +61,7 @@ type TestServer struct {
PID int PID int
Config *TestServerConfig Config *TestServerConfig
APIAddr string APIAddr string
t *testing.T
} }
func NewTestServer(t *testing.T) *TestServer { func NewTestServer(t *testing.T) *TestServer {
@ -110,6 +113,7 @@ func NewTestServerConfig(t *testing.T, cb ServerConfigCallback) *TestServer {
Config: consulConfig, Config: consulConfig,
PID: cmd.Process.Pid, PID: cmd.Process.Pid,
APIAddr: fmt.Sprintf("127.0.0.1:%d", consulConfig.Ports.HTTP), APIAddr: fmt.Sprintf("127.0.0.1:%d", consulConfig.Ports.HTTP),
t: t,
} }
// Wait for the server to be ready // Wait for the server to be ready
@ -156,3 +160,46 @@ func (s *TestServer) waitForLeader() error {
return nil return nil
} }
func (s *TestServer) url(path string) string {
return fmt.Sprintf("http://127.0.0.1:%d%s", s.Config.Ports.HTTP, path)
}
func (s *TestServer) put(path string, body io.Reader) {
req, err := http.NewRequest("PUT", s.url(path), body)
if err != nil {
s.t.Fatalf("err: %s", err)
}
s.request(req)
}
func (s *TestServer) delete(path string) {
var body io.Reader
req, err := http.NewRequest("DELETE", s.url(path), body)
if err != nil {
s.t.Fatalf("err: %s", err)
}
s.request(req)
}
func (s *TestServer) request(req *http.Request) {
// Perform the PUT
resp, err := http.DefaultClient.Do(req)
if err != nil {
s.t.Fatalf("err: %s", err)
}
defer resp.Body.Close()
// Check status code
if resp.StatusCode != 200 {
s.t.Fatalf("Bad response code: %d", resp.StatusCode)
}
}
func (s *TestServer) KVSet(key string, val []byte) {
s.put("/v1/kv/"+key, bytes.NewBuffer(val))
}
func (s *TestServer) KVDelete(key string) {
s.delete("/v1/kv/" + key)
}