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.
 
 
 

78 lines
2.5 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 header
  15. import (
  16. "net/http"
  17. "strings"
  18. "testing"
  19. "github.com/google/martian"
  20. "github.com/google/martian/proxyutil"
  21. )
  22. func TestViaModifier(t *testing.T) {
  23. m := NewViaModifier("martian")
  24. req, err := http.NewRequest("GET", "/", nil)
  25. if err != nil {
  26. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  27. }
  28. res := proxyutil.NewResponse(200, nil, req)
  29. ctx, remove, err := martian.TestContext(req, nil, nil)
  30. if err != nil {
  31. t.Fatalf("martian.TestContext(): got %v, want no error", err)
  32. }
  33. defer remove()
  34. if err := m.ModifyRequest(req); err != nil {
  35. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  36. }
  37. if got, want := req.Header.Get("Via"), "1.1 martian"; !strings.HasPrefix(got, want) {
  38. t.Errorf("req.Header.Get(%q): got %q, want prefixed with %q", "Via", got, want)
  39. }
  40. if err := m.ModifyResponse(res); err != nil {
  41. t.Fatalf("ModifyResponse(): got %v, want no error", err)
  42. }
  43. req.Header.Set("Via", "1.0\talpha\t(martian)")
  44. if err := m.ModifyRequest(req); err != nil {
  45. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  46. }
  47. if got, want := req.Header.Get("Via"), "1.0\talpha\t(martian), 1.1 martian"; !strings.HasPrefix(got, want) {
  48. t.Errorf("req.Header.Get(%q): got %q, want prefixed with %q", "Via", got, want)
  49. }
  50. m.SetBoundary("boundary")
  51. req.Header.Set("Via", "1.0\talpha\t(martian), 1.1 martian-boundary, 1.1 beta")
  52. if err := m.ModifyRequest(req); err == nil {
  53. t.Fatal("ModifyRequest(): got nil, want request loop error")
  54. }
  55. if !ctx.SkippingRoundTrip() {
  56. t.Errorf("ctx.SkippingRoundTrip(): got false, want true")
  57. }
  58. if err := m.ModifyResponse(res); err == nil {
  59. t.Fatal("ModifyResponse(): got nil, want request loop error")
  60. }
  61. if got, want := res.StatusCode, 400; got != want {
  62. t.Errorf("res.StatusCode: got %d, want %d", got, want)
  63. }
  64. if got, want := res.Status, http.StatusText(400); got != want {
  65. t.Errorf("res.Status: got %q, want %q", got, want)
  66. }
  67. }