http: allow GET, HEAD for /v2/machines

release-2.0
Yicheng Qin 2014-09-09 15:22:52 -07:00
parent 0c1d1b7aeb
commit 4087fa5c7a
2 changed files with 24 additions and 8 deletions

View File

@ -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'.
// TODO: rethink the format of machine list because it is not json format.
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)
for _, addrs := range h.Peers {
for _, addr := range addrs {

View File

@ -349,18 +349,29 @@ func TestWaitForEventCancelledContext(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)
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}
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"
if g := writer.Body.String(); g != w {
t.Errorf("data = %s, want %s", g, w)