25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

127 satır
3.6 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 cookie
  15. import (
  16. "net/http"
  17. "testing"
  18. "github.com/google/martian/parse"
  19. "github.com/google/martian/proxyutil"
  20. )
  21. func TestCookieModifier(t *testing.T) {
  22. cookie := &http.Cookie{
  23. Name: "name",
  24. Value: "value",
  25. }
  26. mod := NewModifier(cookie)
  27. req, err := http.NewRequest("GET", "/", nil)
  28. if err != nil {
  29. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  30. }
  31. if err := mod.ModifyRequest(req); err != nil {
  32. t.Fatalf("ModifyRequest(): got %v, want no error", err)
  33. }
  34. if got, want := len(req.Cookies()), 1; got != want {
  35. t.Errorf("len(req.Cookies): got %v, want %v", got, want)
  36. }
  37. if got, want := req.Cookies()[0].Name, cookie.Name; got != want {
  38. t.Errorf("req.Cookies()[0].Name: got %v, want %v", got, want)
  39. }
  40. if got, want := req.Cookies()[0].Value, cookie.Value; got != want {
  41. t.Errorf("req.Cookies()[0].Value: got %v, want %v", got, want)
  42. }
  43. res := proxyutil.NewResponse(200, nil, req)
  44. if err := mod.ModifyResponse(res); err != nil {
  45. t.Fatalf("ModifyResponse(): got %v, want no error", err)
  46. }
  47. if got, want := len(res.Cookies()), 1; got != want {
  48. t.Errorf("len(res.Cookies): got %v, want %v", got, want)
  49. }
  50. if got, want := res.Cookies()[0].Name, cookie.Name; got != want {
  51. t.Errorf("res.Cookies()[0].Name: got %v, want %v", got, want)
  52. }
  53. if got, want := res.Cookies()[0].Value, cookie.Value; got != want {
  54. t.Errorf("res.Cookies()[0].Value: got %v, want %v", got, want)
  55. }
  56. }
  57. func TestModifierFromJSON(t *testing.T) {
  58. msg := []byte(`{
  59. "cookie.Modifier": {
  60. "scope": ["request", "response"],
  61. "name": "martian",
  62. "value": "value"
  63. }
  64. }`)
  65. r, err := parse.FromJSON(msg)
  66. if err != nil {
  67. t.Fatalf("parse.FromJSON(): got %v, want no error", err)
  68. }
  69. req, err := http.NewRequest("GET", "http://example.com/path/", nil)
  70. if err != nil {
  71. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  72. }
  73. reqmod := r.RequestModifier()
  74. if reqmod == nil {
  75. t.Fatal("reqmod: got nil, want not nil")
  76. }
  77. if err := reqmod.ModifyRequest(req); err != nil {
  78. t.Fatalf("reqmod.ModifyRequest(): got %v, want no error", err)
  79. }
  80. if got, want := len(req.Cookies()), 1; got != want {
  81. t.Fatalf("len(req.Cookies): got %v, want %v", got, want)
  82. }
  83. if got, want := req.Cookies()[0].Name, "martian"; got != want {
  84. t.Errorf("req.Cookies()[0].Name: got %v, want %v", got, want)
  85. }
  86. if got, want := req.Cookies()[0].Value, "value"; got != want {
  87. t.Errorf("req.Cookies()[0].Value: got %v, want %v", got, want)
  88. }
  89. resmod := r.ResponseModifier()
  90. if resmod == nil {
  91. t.Fatal("resmod: got nil, want not nil")
  92. }
  93. res := proxyutil.NewResponse(200, nil, req)
  94. if err := resmod.ModifyResponse(res); err != nil {
  95. t.Fatalf("resmod.ModifyResponse(): got %v, want no error", err)
  96. }
  97. if got, want := len(res.Cookies()), 1; got != want {
  98. t.Fatalf("len(res.Cookies): got %v, want %v", got, want)
  99. }
  100. if got, want := res.Cookies()[0].Name, "martian"; got != want {
  101. t.Errorf("res.Cookies()[0].Name: got %v, want %v", got, want)
  102. }
  103. if got, want := res.Cookies()[0].Value, "value"; got != want {
  104. t.Errorf("res.Cookies()[0].Value: got %v, want %v", got, want)
  105. }
  106. }