members_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2015 CoreOS, Inc.
  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 client
  15. import (
  16. "net/http"
  17. "net/url"
  18. "reflect"
  19. "testing"
  20. "github.com/coreos/etcd/pkg/types"
  21. )
  22. func TestMembersAPIActionList(t *testing.T) {
  23. ep := url.URL{Scheme: "http", Host: "example.com"}
  24. act := &membersAPIActionList{}
  25. wantURL := &url.URL{
  26. Scheme: "http",
  27. Host: "example.com",
  28. Path: "/v2/members",
  29. }
  30. got := *act.HTTPRequest(ep)
  31. err := assertResponse(got, wantURL, http.Header{}, nil)
  32. if err != nil {
  33. t.Error(err.Error())
  34. }
  35. }
  36. func TestMembersAPIActionAdd(t *testing.T) {
  37. ep := url.URL{Scheme: "http", Host: "example.com"}
  38. act := &membersAPIActionAdd{
  39. peerURLs: types.URLs([]url.URL{
  40. url.URL{Scheme: "https", Host: "127.0.0.1:8081"},
  41. url.URL{Scheme: "http", Host: "127.0.0.1:8080"},
  42. }),
  43. }
  44. wantURL := &url.URL{
  45. Scheme: "http",
  46. Host: "example.com",
  47. Path: "/v2/members",
  48. }
  49. wantHeader := http.Header{
  50. "Content-Type": []string{"application/json"},
  51. }
  52. wantBody := []byte(`{"peerURLs":["https://127.0.0.1:8081","http://127.0.0.1:8080"]}`)
  53. got := *act.HTTPRequest(ep)
  54. err := assertResponse(got, wantURL, wantHeader, wantBody)
  55. if err != nil {
  56. t.Error(err.Error())
  57. }
  58. }
  59. func TestMembersAPIActionRemove(t *testing.T) {
  60. ep := url.URL{Scheme: "http", Host: "example.com"}
  61. act := &membersAPIActionRemove{memberID: "XXX"}
  62. wantURL := &url.URL{
  63. Scheme: "http",
  64. Host: "example.com",
  65. Path: "/v2/members/XXX",
  66. }
  67. got := *act.HTTPRequest(ep)
  68. err := assertResponse(got, wantURL, http.Header{}, nil)
  69. if err != nil {
  70. t.Error(err.Error())
  71. }
  72. }
  73. func TestAssertStatusCode(t *testing.T) {
  74. if err := assertStatusCode(404, 400); err == nil {
  75. t.Errorf("assertStatusCode failed to detect conflict in 400 vs 404")
  76. }
  77. if err := assertStatusCode(404, 400, 404); err != nil {
  78. t.Errorf("assertStatusCode found conflict in (404,400) vs 400: %v", err)
  79. }
  80. }
  81. func TestV2MembersURL(t *testing.T) {
  82. got := v2MembersURL(url.URL{
  83. Scheme: "http",
  84. Host: "foo.example.com:4002",
  85. Path: "/pants",
  86. })
  87. want := &url.URL{
  88. Scheme: "http",
  89. Host: "foo.example.com:4002",
  90. Path: "/pants/v2/members",
  91. }
  92. if !reflect.DeepEqual(want, got) {
  93. t.Fatalf("v2MembersURL got %#v, want %#v", got, want)
  94. }
  95. }