You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

164 lines
4.1 KiB

  1. // Copyright 2015 Google Inc. All rights reserved.
  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 auth
  15. import (
  16. "net/http"
  17. "testing"
  18. "github.com/google/martian"
  19. "github.com/google/martian/martiantest"
  20. "github.com/google/martian/proxyutil"
  21. )
  22. func TestFilter(t *testing.T) {
  23. f := NewFilter()
  24. if f.RequestModifier("id") != nil {
  25. t.Fatalf("f.RequestModifier(%q): got reqmod, want nil", "id")
  26. }
  27. if f.ResponseModifier("id") != nil {
  28. t.Fatalf("f.ResponseModifier(%q): got resmod, want nil", "id")
  29. }
  30. tm := martiantest.NewModifier()
  31. f.SetRequestModifier("id", tm)
  32. f.SetResponseModifier("id", tm)
  33. if f.RequestModifier("id") != tm {
  34. t.Errorf("f.RequestModifier(%q): got nil, want martiantest.Modifier", "id")
  35. }
  36. if f.ResponseModifier("id") != tm {
  37. t.Errorf("f.ResponseModifier(%q): got nil, want martiantest.Modifier", "id")
  38. }
  39. }
  40. func TestModifyRequest(t *testing.T) {
  41. f := NewFilter()
  42. tm := martiantest.NewModifier()
  43. f.SetRequestModifier("id", tm)
  44. req, err := http.NewRequest("GET", "http://example.com", nil)
  45. if err != nil {
  46. t.Fatalf("NewRequest(): got %v, want no error", err)
  47. }
  48. // No ID, auth required.
  49. f.SetAuthRequired(true)
  50. ctx, remove, err := martian.TestContext(req, nil, nil)
  51. if err != nil {
  52. t.Fatalf("martian.TestContext(): got %v, want no error", err)
  53. }
  54. defer remove()
  55. if err := f.ModifyRequest(req); err != nil {
  56. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  57. }
  58. actx := FromContext(ctx)
  59. if actx.Error() == nil {
  60. t.Error("actx.Error(): got nil, want error")
  61. }
  62. if tm.RequestModified() {
  63. t.Error("tm.RequestModified(): got true, want false")
  64. }
  65. tm.Reset()
  66. // No ID, auth not required.
  67. f.SetAuthRequired(false)
  68. actx.SetError(nil)
  69. if err := f.ModifyRequest(req); err != nil {
  70. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  71. }
  72. if actx.Error() != nil {
  73. t.Errorf("actx.Error(): got %v, want no error", err)
  74. }
  75. if tm.RequestModified() {
  76. t.Error("tm.RequestModified(): got true, want false")
  77. }
  78. // Valid ID.
  79. actx.SetError(nil)
  80. actx.SetID("id")
  81. if err := f.ModifyRequest(req); err != nil {
  82. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  83. }
  84. if actx.Error() != nil {
  85. t.Errorf("actx.Error(): got %v, want no error", actx.Error())
  86. }
  87. if !tm.RequestModified() {
  88. t.Error("tm.RequestModified(): got false, want true")
  89. }
  90. }
  91. func TestModifyResponse(t *testing.T) {
  92. f := NewFilter()
  93. tm := martiantest.NewModifier()
  94. f.SetResponseModifier("id", tm)
  95. req, err := http.NewRequest("GET", "http://example.com", nil)
  96. if err != nil {
  97. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  98. }
  99. res := proxyutil.NewResponse(200, nil, req)
  100. // No ID, auth required.
  101. f.SetAuthRequired(true)
  102. ctx, remove, err := martian.TestContext(req, nil, nil)
  103. if err != nil {
  104. t.Fatalf("martian.TestContext(): got %v, want no error", err)
  105. }
  106. defer remove()
  107. if err := f.ModifyResponse(res); err != nil {
  108. t.Fatalf("ModifyResponse(): got %v, want no error", err)
  109. }
  110. actx := FromContext(ctx)
  111. if actx.Error() == nil {
  112. t.Error("actx.Error(): got nil, want error")
  113. }
  114. if tm.ResponseModified() {
  115. t.Error("tm.RequestModified(): got true, want false")
  116. }
  117. // No ID, no auth required.
  118. f.SetAuthRequired(false)
  119. actx.SetError(nil)
  120. if err := f.ModifyResponse(res); err != nil {
  121. t.Fatalf("ModifyResponse(): got %v, want no error", err)
  122. }
  123. if tm.ResponseModified() {
  124. t.Error("tm.ResponseModified(): got true, want false")
  125. }
  126. // Valid ID.
  127. actx.SetID("id")
  128. if err := f.ModifyResponse(res); err != nil {
  129. t.Fatalf("ModifyResponse(): got %v, want no error", err)
  130. }
  131. if !tm.ResponseModified() {
  132. t.Error("tm.ResponseModified(): got false, want true")
  133. }
  134. }