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.
 
 
 

129 lines
3.6 KiB

  1. // Copyright 2016 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 servemux
  15. import (
  16. "net/http"
  17. "testing"
  18. "github.com/google/martian/martiantest"
  19. "github.com/google/martian/proxyutil"
  20. )
  21. func TestModifyRequest(t *testing.T) {
  22. mux := http.NewServeMux()
  23. mux.HandleFunc("example.com/test", func(rw http.ResponseWriter, req *http.Request) {
  24. return
  25. })
  26. f := NewFilter(mux)
  27. tm := martiantest.NewModifier()
  28. f.RequestWhenTrue(tm)
  29. fm := martiantest.NewModifier()
  30. f.RequestWhenFalse(fm)
  31. req, err := http.NewRequest("GET", "http://example.com/test", nil)
  32. if err != nil {
  33. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  34. }
  35. if err := f.ModifyRequest(req); err != nil {
  36. t.Errorf("ModifyRequest(): got %v, want no error", err)
  37. }
  38. if got, want := tm.RequestModified(), true; got != want {
  39. t.Errorf("tm.RequestModified(): got %v, want %v", got, want)
  40. }
  41. if got, want := fm.RequestModified(), false; got != want {
  42. t.Errorf("fm.RequestModified(): got %v, want %v", got, want)
  43. }
  44. tm.Reset()
  45. fm.Reset()
  46. req, err = http.NewRequest("GET", "http://example.com/nomatch", nil)
  47. if err != nil {
  48. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  49. }
  50. if err := f.ModifyRequest(req); err != nil {
  51. t.Errorf("ModifyRequest(): got %v, want no error", err)
  52. }
  53. if got, want := tm.RequestModified(), false; got != want {
  54. t.Errorf("tm.RequestModified(): got %v, want %v", got, want)
  55. }
  56. if got, want := fm.RequestModified(), true; got != want {
  57. t.Errorf("fm.RequestModified(): got %v, want %v", got, want)
  58. }
  59. }
  60. func TestModifyResponse(t *testing.T) {
  61. mux := http.NewServeMux()
  62. mux.HandleFunc("example.com/restest", func(rw http.ResponseWriter, req *http.Request) {
  63. return
  64. })
  65. f := NewFilter(mux)
  66. tm := martiantest.NewModifier()
  67. f.ResponseWhenTrue(tm)
  68. fm := martiantest.NewModifier()
  69. f.ResponseWhenFalse(fm)
  70. req, err := http.NewRequest("GET", "http://example.com/restest", nil)
  71. if err != nil {
  72. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  73. }
  74. res := proxyutil.NewResponse(200, nil, req)
  75. if err := f.ModifyResponse(res); err != nil {
  76. t.Errorf("ModifyResponse(): got %v, want no error", err)
  77. }
  78. if got, want := tm.ResponseModified(), true; got != want {
  79. t.Errorf("tm.ResponseModified(): got %v, want %v", got, want)
  80. }
  81. if got, want := fm.ResponseModified(), false; got != want {
  82. t.Errorf("fm.ResponseModified(): got %v, want %v", got, want)
  83. }
  84. tm.Reset()
  85. fm.Reset()
  86. req, err = http.NewRequest("GET", "http://example.com/nomatch", nil)
  87. if err != nil {
  88. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  89. }
  90. res = proxyutil.NewResponse(200, nil, req)
  91. if err := f.ModifyResponse(res); err != nil {
  92. t.Errorf("ModifyResponse(): got %v, want no error", err)
  93. }
  94. if tm.ResponseModified() != false {
  95. t.Errorf("tm.ResponseModified(): got %t, want %t", tm.ResponseModified(), false)
  96. }
  97. if got, want := tm.ResponseModified(), false; got != want {
  98. t.Errorf("tm.ResponseModified(): got %v, want %v", got, want)
  99. }
  100. if got, want := fm.ResponseModified(), true; got != want {
  101. t.Errorf("fm.ResponseModified(): got %v, want %v", got, want)
  102. }
  103. }