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.

83 lines
2.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 martiantest
  15. import (
  16. "errors"
  17. "net/http"
  18. "testing"
  19. "github.com/google/martian/proxyutil"
  20. )
  21. func TestModifier(t *testing.T) {
  22. var reqrun bool
  23. var resrun bool
  24. moderr := errors.New("modifier error")
  25. tm := NewModifier()
  26. tm.RequestError(moderr)
  27. tm.RequestFunc(func(*http.Request) {
  28. reqrun = true
  29. })
  30. tm.ResponseError(moderr)
  31. tm.ResponseFunc(func(*http.Response) {
  32. resrun = true
  33. })
  34. req, err := http.NewRequest("GET", "http://example.com", nil)
  35. if err != nil {
  36. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  37. }
  38. if err := tm.ModifyRequest(req); err != moderr {
  39. t.Fatalf("tm.ModifyRequest(): got %v, want %v", err, moderr)
  40. }
  41. if !tm.RequestModified() {
  42. t.Errorf("tm.RequestModified(): got false, want true")
  43. }
  44. if tm.RequestCount() != 1 {
  45. t.Errorf("tm.RequestCount(): got %d, want %d", tm.RequestCount(), 1)
  46. }
  47. if !reqrun {
  48. t.Error("reqrun: got false, want true")
  49. }
  50. res := proxyutil.NewResponse(200, nil, req)
  51. if err := tm.ModifyResponse(res); err != moderr {
  52. t.Fatalf("tm.ModifyResponse(): got %v, want %v", err, moderr)
  53. }
  54. if !tm.ResponseModified() {
  55. t.Errorf("tm.ResponseModified(): got false, want true")
  56. }
  57. if tm.ResponseCount() != 1 {
  58. t.Errorf("tm.ResponseCount(): got %d, want %d", tm.ResponseCount(), 1)
  59. }
  60. if !resrun {
  61. t.Error("resrun: got false, want true")
  62. }
  63. tm.Reset()
  64. if tm.RequestModified() {
  65. t.Error("tm.RequestModified(): got true, want false")
  66. }
  67. if tm.ResponseModified() {
  68. t.Error("tm.ResponseModified(): got true, want false")
  69. }
  70. }