mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
etcd: refactor tests
This commit is contained in:
parent
2ff3ce74c5
commit
00935c873f
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -42,6 +43,38 @@ func TestMultipleTLSNodes(t *testing.T) {
|
||||
afterTest(t)
|
||||
}
|
||||
|
||||
func TestV2Redirect(t *testing.T) {
|
||||
es, hs := buildCluster(3, false)
|
||||
waitCluster(t, es)
|
||||
u := hs[1].URL
|
||||
ru := fmt.Sprintf("%s%s", hs[0].URL, "/v2/keys/foo")
|
||||
tc := NewTestClient()
|
||||
|
||||
v := url.Values{}
|
||||
v.Set("value", "XXX")
|
||||
resp, _ := tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), v)
|
||||
if resp.StatusCode != http.StatusTemporaryRedirect {
|
||||
t.Errorf("status = %d, want %d", resp.StatusCode, http.StatusTemporaryRedirect)
|
||||
}
|
||||
location, err := resp.Location()
|
||||
if err != nil {
|
||||
t.Errorf("want err = %, want nil", err)
|
||||
}
|
||||
|
||||
if location.String() != ru {
|
||||
t.Errorf("location = %v, want %v", location.String(), ru)
|
||||
}
|
||||
|
||||
resp.Body.Close()
|
||||
for i := range es {
|
||||
es[len(es)-i-1].Stop()
|
||||
}
|
||||
for i := range hs {
|
||||
hs[len(hs)-i-1].Close()
|
||||
}
|
||||
afterTest(t)
|
||||
}
|
||||
|
||||
func buildCluster(number int, tls bool) ([]*Server, []*httptest.Server) {
|
||||
bootstrapper := 0
|
||||
es := make([]*Server, number)
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -10,13 +10,17 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type testHttpClient struct {
|
||||
*http.Client
|
||||
}
|
||||
|
||||
// Creates a new HTTP client with KeepAlive disabled.
|
||||
func NewHTTPClient() *http.Client {
|
||||
return &http.Client{Transport: &http.Transport{DisableKeepAlives: true}}
|
||||
func NewTestClient() *testHttpClient {
|
||||
return &testHttpClient{&http.Client{Transport: &http.Transport{DisableKeepAlives: true}}}
|
||||
}
|
||||
|
||||
// Reads the body from the response and closes it.
|
||||
func ReadBody(resp *http.Response) []byte {
|
||||
func (t *testHttpClient) ReadBody(resp *http.Response) []byte {
|
||||
if resp == nil {
|
||||
return []byte{}
|
||||
}
|
||||
@ -26,53 +30,52 @@ func ReadBody(resp *http.Response) []byte {
|
||||
}
|
||||
|
||||
// Reads the body from the response and parses it as JSON.
|
||||
func ReadBodyJSON(resp *http.Response) map[string]interface{} {
|
||||
func (t *testHttpClient) ReadBodyJSON(resp *http.Response) map[string]interface{} {
|
||||
m := make(map[string]interface{})
|
||||
b := ReadBody(resp)
|
||||
b := t.ReadBody(resp)
|
||||
if err := json.Unmarshal(b, &m); err != nil {
|
||||
panic(fmt.Sprintf("HTTP body JSON parse error: %v: %s", err, string(b)))
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func Head(url string) (*http.Response, error) {
|
||||
return send("HEAD", url, "application/json", nil)
|
||||
func (t *testHttpClient) Head(url string) (*http.Response, error) {
|
||||
return t.send("HEAD", url, "application/json", nil)
|
||||
}
|
||||
|
||||
func Get(url string) (*http.Response, error) {
|
||||
return send("GET", url, "application/json", nil)
|
||||
func (t *testHttpClient) Get(url string) (*http.Response, error) {
|
||||
return t.send("GET", url, "application/json", nil)
|
||||
}
|
||||
|
||||
func Post(url string, bodyType string, body io.Reader) (*http.Response, error) {
|
||||
return send("POST", url, bodyType, body)
|
||||
func (t *testHttpClient) Post(url string, bodyType string, body io.Reader) (*http.Response, error) {
|
||||
return t.send("POST", url, bodyType, body)
|
||||
}
|
||||
|
||||
func PostForm(url string, data url.Values) (*http.Response, error) {
|
||||
return Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
|
||||
func (t *testHttpClient) PostForm(url string, data url.Values) (*http.Response, error) {
|
||||
return t.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
|
||||
}
|
||||
|
||||
func Put(url string, bodyType string, body io.Reader) (*http.Response, error) {
|
||||
return send("PUT", url, bodyType, body)
|
||||
func (t *testHttpClient) Put(url string, bodyType string, body io.Reader) (*http.Response, error) {
|
||||
return t.send("PUT", url, bodyType, body)
|
||||
}
|
||||
|
||||
func PutForm(url string, data url.Values) (*http.Response, error) {
|
||||
return Put(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
|
||||
func (t *testHttpClient) PutForm(url string, data url.Values) (*http.Response, error) {
|
||||
return t.Put(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
|
||||
}
|
||||
|
||||
func Delete(url string, bodyType string, body io.Reader) (*http.Response, error) {
|
||||
return send("DELETE", url, bodyType, body)
|
||||
func (t *testHttpClient) Delete(url string, bodyType string, body io.Reader) (*http.Response, error) {
|
||||
return t.send("DELETE", url, bodyType, body)
|
||||
}
|
||||
|
||||
func DeleteForm(url string, data url.Values) (*http.Response, error) {
|
||||
return Delete(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
|
||||
func (t *testHttpClient) DeleteForm(url string, data url.Values) (*http.Response, error) {
|
||||
return t.Delete(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
|
||||
}
|
||||
|
||||
func send(method string, url string, bodyType string, body io.Reader) (*http.Response, error) {
|
||||
c := NewHTTPClient()
|
||||
func (t *testHttpClient) send(method string, url string, bodyType string, body io.Reader) (*http.Response, error) {
|
||||
req, err := http.NewRequest(method, url, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", bodyType)
|
||||
return c.Do(req)
|
||||
return t.Do(req)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user