Browse Source

http: allow GET, HEAD for /v2/machines

Yicheng Qin 11 years ago
parent
commit
4087fa5c7a
2 changed files with 24 additions and 8 deletions
  1. 4 0
      etcdserver/etcdhttp/http.go
  2. 20 8
      etcdserver/etcdhttp/http_test.go

+ 4 - 0
etcdserver/etcdhttp/http.go

@@ -203,6 +203,10 @@ func (h Handler) serveKeys(ctx context.Context, w http.ResponseWriter, r *http.R
 // serveMachines responds address list in the format '0.0.0.0, 1.1.1.1'.
 // serveMachines responds address list in the format '0.0.0.0, 1.1.1.1'.
 // TODO: rethink the format of machine list because it is not json format.
 // TODO: rethink the format of machine list because it is not json format.
 func (h Handler) serveMachines(w http.ResponseWriter, r *http.Request) {
 func (h Handler) serveMachines(w http.ResponseWriter, r *http.Request) {
+	if r.Method != "GET" && r.Method != "HEAD" {
+		w.WriteHeader(http.StatusMethodNotAllowed)
+		return
+	}
 	urls := make([]string, 0)
 	urls := make([]string, 0)
 	for _, addrs := range h.Peers {
 	for _, addrs := range h.Peers {
 		for _, addr := range addrs {
 		for _, addr := range addrs {

+ 20 - 8
etcdserver/etcdhttp/http_test.go

@@ -349,18 +349,29 @@ func TestWaitForEventCancelledContext(t *testing.T) {
 }
 }
 
 
 func TestV2MachinesEndpoint(t *testing.T) {
 func TestV2MachinesEndpoint(t *testing.T) {
-	h := Handler{Peers: Peers{}}
+	tests := []struct {
+		method string
+		wcode  int
+	}{
+		{"GET", http.StatusOK},
+		{"HEAD", http.StatusOK},
+		{"POST", http.StatusMethodNotAllowed},
+	}
 
 
+	h := Handler{Peers: Peers{}}
 	s := httptest.NewServer(h)
 	s := httptest.NewServer(h)
 	defer s.Close()
 	defer s.Close()
 
 
-	resp, err := http.Get(s.URL + machinesPrefix)
-	if err != nil {
-		t.Fatal(err)
-	}
+	for _, tt := range tests {
+		req, _ := http.NewRequest(tt.method, s.URL + machinesPrefix, nil)
+		resp, err := http.DefaultClient.Do(req)
+		if err != nil {
+			t.Fatal(err)
+		}
 
 
-	if resp.StatusCode != http.StatusOK {
-		t.Errorf("StatusCode = %d, expected %d", resp.StatusCode, http.StatusOK)
+		if resp.StatusCode != tt.wcode {
+			t.Errorf("StatusCode = %d, expected %d", resp.StatusCode, tt.wcode)
+		}
 	}
 	}
 }
 }
 
 
@@ -370,7 +381,8 @@ func TestServeMachines(t *testing.T) {
 	h := Handler{Peers: peers}
 	h := Handler{Peers: peers}
 
 
 	writer := httptest.NewRecorder()
 	writer := httptest.NewRecorder()
-	h.serveMachines(writer, nil)
+	req, _ := http.NewRequest("GET", "", nil)
+	h.serveMachines(writer, req)
 	w := "http://localhost:8080, http://localhost:8081, http://localhost:8082"
 	w := "http://localhost:8080, http://localhost:8081, http://localhost:8082"
 	if g := writer.Body.String(); g != w {
 	if g := writer.Body.String(); g != w {
 		t.Errorf("data = %s, want %s", g, w)
 		t.Errorf("data = %s, want %s", g, w)