Browse Source

fix(server/v2): set correct content-type for etcdError response

"net/http".Error reset the content type, so we get rid of it and
write our own one.
Yicheng Qin 11 years ago
parent
commit
db4c5e0eaa
2 changed files with 5 additions and 1 deletions
  1. 2 1
      error/error.go
  2. 3 0
      server/v2/tests/get_handler_test.go

+ 2 - 1
error/error.go

@@ -143,5 +143,6 @@ func (e Error) Write(w http.ResponseWriter) {
 			status = http.StatusInternalServerError
 			status = http.StatusInternalServerError
 		}
 		}
 	}
 	}
-	http.Error(w, e.toJsonString(), status)
+	w.WriteHeader(status)
+	fmt.Fprintln(w, e.toJsonString())
 }
 }

+ 3 - 0
server/v2/tests/get_handler_test.go

@@ -24,12 +24,15 @@ func TestV2GetKey(t *testing.T) {
 		v.Set("value", "XXX")
 		v.Set("value", "XXX")
 		fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
 		fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
 		resp, _ := tests.Get(fullURL)
 		resp, _ := tests.Get(fullURL)
+		assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
 		assert.Equal(t, resp.StatusCode, http.StatusNotFound)
 		assert.Equal(t, resp.StatusCode, http.StatusNotFound)
 
 
 		resp, _ = tests.PutForm(fullURL, v)
 		resp, _ = tests.PutForm(fullURL, v)
+		assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
 		tests.ReadBody(resp)
 		tests.ReadBody(resp)
 
 
 		resp, _ = tests.Get(fullURL)
 		resp, _ = tests.Get(fullURL)
+		assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
 		assert.Equal(t, resp.StatusCode, http.StatusOK)
 		assert.Equal(t, resp.StatusCode, http.StatusOK)
 		body := tests.ReadBodyJSON(resp)
 		body := tests.ReadBodyJSON(resp)
 		assert.Equal(t, body["action"], "get", "")
 		assert.Equal(t, body["action"], "get", "")