handlers_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package handlers
  14. import (
  15. "errors"
  16. "net/http"
  17. "net/http/httptest"
  18. "testing"
  19. "k8s.io/kubernetes/pkg/api"
  20. "k8s.io/kubernetes/pkg/auth/authenticator"
  21. "k8s.io/kubernetes/pkg/auth/user"
  22. )
  23. func TestAuthenticateRequest(t *testing.T) {
  24. success := make(chan struct{})
  25. contextMapper := api.NewRequestContextMapper()
  26. auth, err := NewRequestAuthenticator(
  27. contextMapper,
  28. authenticator.RequestFunc(func(req *http.Request) (user.Info, bool, error) {
  29. return &user.DefaultInfo{Name: "user"}, true, nil
  30. }),
  31. http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
  32. t.Errorf("unexpected call to failed")
  33. }),
  34. http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
  35. ctx, ok := contextMapper.Get(req)
  36. if ctx == nil || !ok {
  37. t.Errorf("no context stored on contextMapper: %#v", contextMapper)
  38. }
  39. user, ok := api.UserFrom(ctx)
  40. if user == nil || !ok {
  41. t.Errorf("no user stored in context: %#v", ctx)
  42. }
  43. close(success)
  44. }),
  45. )
  46. auth.ServeHTTP(httptest.NewRecorder(), &http.Request{})
  47. <-success
  48. empty, err := api.IsEmpty(contextMapper)
  49. if err != nil {
  50. t.Fatalf("unexpected error: %v", err)
  51. }
  52. if !empty {
  53. t.Fatalf("contextMapper should have no stored requests: %v", contextMapper)
  54. }
  55. }
  56. func TestAuthenticateRequestFailed(t *testing.T) {
  57. failed := make(chan struct{})
  58. contextMapper := api.NewRequestContextMapper()
  59. auth, err := NewRequestAuthenticator(
  60. contextMapper,
  61. authenticator.RequestFunc(func(req *http.Request) (user.Info, bool, error) {
  62. return nil, false, nil
  63. }),
  64. http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
  65. close(failed)
  66. }),
  67. http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
  68. t.Errorf("unexpected call to handler")
  69. }),
  70. )
  71. auth.ServeHTTP(httptest.NewRecorder(), &http.Request{})
  72. <-failed
  73. empty, err := api.IsEmpty(contextMapper)
  74. if err != nil {
  75. t.Fatalf("unexpected error: %v", err)
  76. }
  77. if !empty {
  78. t.Fatalf("contextMapper should have no stored requests: %v", contextMapper)
  79. }
  80. }
  81. func TestAuthenticateRequestError(t *testing.T) {
  82. failed := make(chan struct{})
  83. contextMapper := api.NewRequestContextMapper()
  84. auth, err := NewRequestAuthenticator(
  85. contextMapper,
  86. authenticator.RequestFunc(func(req *http.Request) (user.Info, bool, error) {
  87. return nil, false, errors.New("failure")
  88. }),
  89. http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
  90. close(failed)
  91. }),
  92. http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
  93. t.Errorf("unexpected call to handler")
  94. }),
  95. )
  96. auth.ServeHTTP(httptest.NewRecorder(), &http.Request{})
  97. <-failed
  98. empty, err := api.IsEmpty(contextMapper)
  99. if err != nil {
  100. t.Fatalf("unexpected error: %v", err)
  101. }
  102. if !empty {
  103. t.Fatalf("contextMapper should have no stored requests: %v", contextMapper)
  104. }
  105. }