version_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2017 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package etcdhttp
  15. import (
  16. "encoding/json"
  17. "net/http"
  18. "net/http/httptest"
  19. "testing"
  20. "github.com/coreos/etcd/version"
  21. )
  22. func TestServeVersion(t *testing.T) {
  23. req, err := http.NewRequest("GET", "", nil)
  24. if err != nil {
  25. t.Fatalf("error creating request: %v", err)
  26. }
  27. rw := httptest.NewRecorder()
  28. serveVersion(rw, req, "2.1.0")
  29. if rw.Code != http.StatusOK {
  30. t.Errorf("code=%d, want %d", rw.Code, http.StatusOK)
  31. }
  32. vs := version.Versions{
  33. Server: version.Version,
  34. Cluster: "2.1.0",
  35. }
  36. w, err := json.Marshal(&vs)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. if g := rw.Body.String(); g != string(w) {
  41. t.Fatalf("body = %q, want %q", g, string(w))
  42. }
  43. if ct := rw.HeaderMap.Get("Content-Type"); ct != "application/json" {
  44. t.Errorf("contet-type header = %s, want %s", ct, "application/json")
  45. }
  46. }
  47. func TestServeVersionFails(t *testing.T) {
  48. for _, m := range []string{
  49. "CONNECT", "TRACE", "PUT", "POST", "HEAD",
  50. } {
  51. req, err := http.NewRequest(m, "", nil)
  52. if err != nil {
  53. t.Fatalf("error creating request: %v", err)
  54. }
  55. rw := httptest.NewRecorder()
  56. serveVersion(rw, req, "2.1.0")
  57. if rw.Code != http.StatusMethodNotAllowed {
  58. t.Errorf("method %s: code=%d, want %d", m, rw.Code, http.StatusMethodNotAllowed)
  59. }
  60. }
  61. }