Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

79 rindas
2.3 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 httpspec
  15. import (
  16. "net/http"
  17. "strings"
  18. "testing"
  19. "github.com/google/martian"
  20. "github.com/google/martian/martiantest"
  21. "github.com/google/martian/proxyutil"
  22. )
  23. func TestNewStack(t *testing.T) {
  24. stack, fg := NewStack("martian")
  25. tm := martiantest.NewModifier()
  26. fg.AddRequestModifier(tm)
  27. fg.AddResponseModifier(tm)
  28. req, err := http.NewRequest("GET", "http://example.com", nil)
  29. if err != nil {
  30. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  31. }
  32. _, remove, err := martian.TestContext(req, nil, nil)
  33. if err != nil {
  34. t.Fatalf("martian.TestContext(): got %v, want no error", err)
  35. }
  36. defer remove()
  37. // Hop-by-hop header to be removed.
  38. req.Header.Set("Hop-By-Hop", "true")
  39. req.Header.Set("Connection", "Hop-By-Hop")
  40. req.RemoteAddr = "10.0.0.1:5000"
  41. if err := stack.ModifyRequest(req); err != nil {
  42. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  43. }
  44. if got, want := req.Header.Get("Hop-By-Hop"), ""; got != want {
  45. t.Errorf("req.Header.Get(%q): got %q, want %q", "Hop-By-Hop", got, want)
  46. }
  47. if got, want := req.Header.Get("X-Forwarded-For"), "10.0.0.1"; got != want {
  48. t.Errorf("req.Header.Get(%q): got %q, want %q", "X-Forwarded-For", got, want)
  49. }
  50. if got, want := req.Header.Get("Via"), "1.1 martian"; !strings.HasPrefix(got, want) {
  51. t.Errorf("req.Header.Get(%q): got %q, want %q", "Via", got, want)
  52. }
  53. res := proxyutil.NewResponse(200, nil, req)
  54. // Hop-by-hop header to be removed.
  55. res.Header.Set("Hop-By-Hop", "true")
  56. res.Header.Set("Connection", "Hop-By-Hop")
  57. if err := stack.ModifyResponse(res); err != nil {
  58. t.Fatalf("ModifyResponse(): got %v, want no error", err)
  59. }
  60. if got, want := res.Header.Get("Hop-By-Hop"), ""; got != want {
  61. t.Errorf("res.Header.Get(%q): got %q, want %q", "Hop-By-Hop", got, want)
  62. }
  63. }