mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
etcdserver: admin endpoint accepts json body
This commit is contained in:
parent
d27d308935
commit
7be0f4b618
@ -157,12 +157,22 @@ func (h serverHandler) serveAdminMembers(w http.ResponseWriter, r *http.Request)
|
||||
|
||||
switch r.Method {
|
||||
case "POST":
|
||||
if err := r.ParseForm(); err != nil {
|
||||
ctype := r.Header.Get("Content-Type")
|
||||
if ctype != "application/json" {
|
||||
http.Error(w, fmt.Sprintf("bad Content-Type %s, accept application/json", ctype), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
b, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
peerURLs := r.PostForm["PeerURLs"]
|
||||
validURLs, err := types.NewURLs(peerURLs)
|
||||
raftAttr := etcdserver.RaftAttributes{}
|
||||
if err := json.Unmarshal(b, &raftAttr); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
validURLs, err := types.NewURLs(raftAttr.PeerURLs)
|
||||
if err != nil {
|
||||
http.Error(w, "bad peer urls", http.StatusBadRequest)
|
||||
return
|
||||
@ -174,7 +184,7 @@ func (h serverHandler) serveAdminMembers(w http.ResponseWriter, r *http.Request)
|
||||
writeError(w, err)
|
||||
return
|
||||
}
|
||||
log.Printf("etcdhttp: added node %x with peer urls %v", m.ID, peerURLs)
|
||||
log.Printf("etcdhttp: added node %x with peer urls %v", m.ID, raftAttr.PeerURLs)
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
case "DELETE":
|
||||
idStr := strings.TrimPrefix(r.URL.Path, adminMembersPrefix)
|
||||
|
@ -1425,22 +1425,25 @@ func TestServeAdminMembersFail(t *testing.T) {
|
||||
http.StatusMethodNotAllowed,
|
||||
},
|
||||
{
|
||||
// parse id error
|
||||
// parse body error
|
||||
&http.Request{
|
||||
URL: mustNewURL(t, adminMembersPrefix),
|
||||
Method: "POST",
|
||||
Body: ioutil.NopCloser(strings.NewReader("bad json")),
|
||||
},
|
||||
&resServer{},
|
||||
|
||||
http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
// parse body error
|
||||
// bad content type
|
||||
&http.Request{
|
||||
URL: mustNewURL(t, adminMembersPrefix),
|
||||
Method: "POST",
|
||||
Body: ioutil.NopCloser(strings.NewReader(`{"PeerURLs": ["http://127.0.0.1:1"]}`)),
|
||||
Header: map[string][]string{"Content-Type": []string{"application/bad"}},
|
||||
},
|
||||
&resServer{},
|
||||
&errServer{},
|
||||
|
||||
http.StatusBadRequest,
|
||||
},
|
||||
@ -1449,8 +1452,8 @@ func TestServeAdminMembersFail(t *testing.T) {
|
||||
&http.Request{
|
||||
URL: mustNewURL(t, adminMembersPrefix),
|
||||
Method: "POST",
|
||||
Body: ioutil.NopCloser(strings.NewReader(url.Values{"PeerURLs": []string{"http://bad"}}.Encode())),
|
||||
Header: map[string][]string{"Content-Type": []string{"application/x-www-form-urlencoded"}},
|
||||
Body: ioutil.NopCloser(strings.NewReader(`{"PeerURLs": ["http://a"]}`)),
|
||||
Header: map[string][]string{"Content-Type": []string{"application/json"}},
|
||||
},
|
||||
&errServer{},
|
||||
|
||||
@ -1461,8 +1464,8 @@ func TestServeAdminMembersFail(t *testing.T) {
|
||||
&http.Request{
|
||||
URL: mustNewURL(t, adminMembersPrefix),
|
||||
Method: "POST",
|
||||
Body: ioutil.NopCloser(strings.NewReader(url.Values{"PeerURLs": []string{"http://127.0.0.1:1"}}.Encode())),
|
||||
Header: map[string][]string{"Content-Type": []string{"application/x-www-form-urlencoded"}},
|
||||
Body: ioutil.NopCloser(strings.NewReader(`{"PeerURLs": ["http://127.0.0.1:1"]}`)),
|
||||
Header: map[string][]string{"Content-Type": []string{"application/json"}},
|
||||
},
|
||||
&errServer{
|
||||
errors.New("blah"),
|
||||
@ -1526,13 +1529,17 @@ func (s *serverRecorder) RemoveMember(_ context.Context, id uint64) error {
|
||||
|
||||
func TestServeAdminMembersPut(t *testing.T) {
|
||||
u := mustNewURL(t, adminMembersPrefix)
|
||||
form := url.Values{"PeerURLs": []string{"http://127.0.0.1:1"}}
|
||||
body := strings.NewReader(form.Encode())
|
||||
raftAttr := etcdserver.RaftAttributes{PeerURLs: []string{"http://127.0.0.1:1"}}
|
||||
b, err := json.Marshal(raftAttr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body := bytes.NewReader(b)
|
||||
req, err := http.NewRequest("POST", u.String(), body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
s := &serverRecorder{}
|
||||
h := &serverHandler{
|
||||
server: s,
|
||||
@ -1551,10 +1558,8 @@ func TestServeAdminMembersPut(t *testing.T) {
|
||||
t.Errorf("got body=%q, want %q", g, "")
|
||||
}
|
||||
wm := etcdserver.Member{
|
||||
ID: 3064321551348478165,
|
||||
RaftAttributes: etcdserver.RaftAttributes{
|
||||
PeerURLs: []string{"http://127.0.0.1:1"},
|
||||
},
|
||||
ID: 3064321551348478165,
|
||||
RaftAttributes: raftAttr,
|
||||
}
|
||||
wactions := []action{{name: "AddMember", params: []interface{}{wm}}}
|
||||
if !reflect.DeepEqual(s.actions, wactions) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user