proxyauth_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 proxyauth
  15. import (
  16. "encoding/base64"
  17. "errors"
  18. "net/http"
  19. "testing"
  20. "github.com/google/martian"
  21. "github.com/google/martian/auth"
  22. "github.com/google/martian/martiantest"
  23. "github.com/google/martian/proxyutil"
  24. )
  25. func encode(v string) string {
  26. return base64.StdEncoding.EncodeToString([]byte(v))
  27. }
  28. func TestNoModifiers(t *testing.T) {
  29. m := NewModifier()
  30. m.SetRequestModifier(nil)
  31. m.SetResponseModifier(nil)
  32. req, err := http.NewRequest("GET", "http://example.com", nil)
  33. if err != nil {
  34. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  35. }
  36. _, remove, err := martian.TestContext(req, nil, nil)
  37. if err != nil {
  38. t.Fatalf("martian.TestContext(): got %v, want no error", err)
  39. }
  40. defer remove()
  41. if err := m.ModifyRequest(req); err != nil {
  42. t.Errorf("ModifyRequest(): got %v, want no error", err)
  43. }
  44. res := proxyutil.NewResponse(200, nil, req)
  45. if err := m.ModifyResponse(res); err != nil {
  46. t.Errorf("ModifyResponse(): got %v, want no error", err)
  47. }
  48. }
  49. func TestProxyAuth(t *testing.T) {
  50. m := NewModifier()
  51. tm := martiantest.NewModifier()
  52. m.SetRequestModifier(tm)
  53. m.SetResponseModifier(tm)
  54. req, err := http.NewRequest("GET", "http://example.com", nil)
  55. if err != nil {
  56. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  57. }
  58. req.Header.Set("Proxy-Authorization", "Basic "+encode("user:pass"))
  59. ctx, remove, err := martian.TestContext(req, nil, nil)
  60. if err != nil {
  61. t.Fatalf("martian.TestContext(): got %v, want no error", err)
  62. }
  63. defer remove()
  64. if err := m.ModifyRequest(req); err != nil {
  65. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  66. }
  67. actx := auth.FromContext(ctx)
  68. if got, want := actx.ID(), "user:pass"; got != want {
  69. t.Fatalf("actx.ID(): got %q, want %q", got, want)
  70. }
  71. if err := m.ModifyRequest(req); err != nil {
  72. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  73. }
  74. if !tm.RequestModified() {
  75. t.Error("tm.RequestModified(): got false, want true")
  76. }
  77. res := proxyutil.NewResponse(200, nil, req)
  78. if err := m.ModifyResponse(res); err != nil {
  79. t.Fatalf("ModifyResponse(): got %v, want no error", err)
  80. }
  81. if err := m.ModifyResponse(res); err != nil {
  82. t.Fatalf("ModifyResponse(): got %v, want no error", err)
  83. }
  84. if !tm.ResponseModified() {
  85. t.Error("tm.ResponseModified(): got false, want true")
  86. }
  87. if got, want := res.StatusCode, 200; got != want {
  88. t.Errorf("res.StatusCode: got %d, want %d", got, want)
  89. }
  90. if got, want := res.Header.Get("Proxy-Authenticate"), ""; got != want {
  91. t.Errorf("res.Header.Get(%q): got %q, want %q", "Proxy-Authenticate", got, want)
  92. }
  93. }
  94. func TestProxyAuthInvalidCredentials(t *testing.T) {
  95. m := NewModifier()
  96. authErr := errors.New("auth error")
  97. tm := martiantest.NewModifier()
  98. tm.RequestFunc(func(req *http.Request) {
  99. ctx := martian.NewContext(req)
  100. actx := auth.FromContext(ctx)
  101. actx.SetError(authErr)
  102. })
  103. tm.ResponseFunc(func(res *http.Response) {
  104. ctx := martian.NewContext(res.Request)
  105. actx := auth.FromContext(ctx)
  106. actx.SetError(authErr)
  107. })
  108. m.SetRequestModifier(tm)
  109. m.SetResponseModifier(tm)
  110. req, err := http.NewRequest("GET", "http://example.com", nil)
  111. if err != nil {
  112. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  113. }
  114. req.Header.Set("Proxy-Authorization", "Basic "+encode("user:pass"))
  115. ctx, remove, err := martian.TestContext(req, nil, nil)
  116. if err != nil {
  117. t.Fatalf("martian.TestContext(): got %v, want no error", err)
  118. }
  119. defer remove()
  120. if err := m.ModifyRequest(req); err != nil {
  121. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  122. }
  123. if !tm.RequestModified() {
  124. t.Error("tm.RequestModified(): got false, want true")
  125. }
  126. actx := auth.FromContext(ctx)
  127. if actx.Error() != authErr {
  128. t.Fatalf("auth.Error(): got %v, want %v", actx.Error(), authErr)
  129. }
  130. actx.SetError(nil)
  131. res := proxyutil.NewResponse(200, nil, req)
  132. if err := m.ModifyResponse(res); err != nil {
  133. t.Fatalf("ModifyResponse(): got %v, want no error", err)
  134. }
  135. if !tm.ResponseModified() {
  136. t.Error("tm.ResponseModified(): got false, want true")
  137. }
  138. if actx.Error() != authErr {
  139. t.Fatalf("auth.Error(): got %v, want %v", actx.Error(), authErr)
  140. }
  141. if got, want := res.StatusCode, http.StatusProxyAuthRequired; got != want {
  142. t.Errorf("res.StatusCode: got %d, want %d", got, want)
  143. }
  144. if got, want := res.Header.Get("Proxy-Authenticate"), "Basic"; got != want {
  145. t.Errorf("res.Header.Get(%q): got %q, want %q", "Proxy-Authenticate", got, want)
  146. }
  147. }