Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

119 righe
2.9 KiB

  1. // Copyright 2019 Google LLC
  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 proxy
  15. import (
  16. "bytes"
  17. "io/ioutil"
  18. "net/http"
  19. "net/url"
  20. "testing"
  21. "cloud.google.com/go/internal/testutil"
  22. "github.com/google/go-cmp/cmp"
  23. )
  24. func TestConvertRequest(t *testing.T) {
  25. clone := func(h http.Header) http.Header {
  26. h2 := http.Header{}
  27. for k, v := range h {
  28. h2[k] = v
  29. }
  30. return h2
  31. }
  32. body := []byte("hello")
  33. conv := defaultConverter()
  34. conv.registerClearParams("secret")
  35. conv.registerRemoveParams("rm*")
  36. url, err := url.Parse("https://www.example.com?a=1&rmx=x&secret=2&c=3&rmy=4")
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. in := &http.Request{
  41. Method: "GET",
  42. URL: url,
  43. Body: ioutil.NopCloser(bytes.NewReader(body)),
  44. Header: http.Header{
  45. "Content-Type": {"text/plain"},
  46. "Authorization": {"oauth2-token"},
  47. "X-Goog-Encryption-Key": {"a-secret-key"},
  48. "X-Goog-Copy-Source-Encryption-Key": {"another-secret-key"},
  49. },
  50. }
  51. origHeader := clone(in.Header)
  52. got, err := conv.convertRequest(in)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. want := &Request{
  57. Method: "GET",
  58. URL: "https://www.example.com?a=1&secret=CLEARED&c=3",
  59. MediaType: "text/plain",
  60. BodyParts: [][]byte{body},
  61. Header: http.Header{
  62. "X-Goog-Encryption-Key": {"CLEARED"},
  63. "X-Goog-Copy-Source-Encryption-Key": {"CLEARED"},
  64. },
  65. Trailer: http.Header{},
  66. }
  67. if diff := cmp.Diff(got, want); diff != "" {
  68. t.Error(diff)
  69. }
  70. // The original headers should be the same.
  71. if got, want := in.Header, origHeader; !testutil.Equal(got, want) {
  72. t.Errorf("got %+v\nwant %+v", got, want)
  73. }
  74. }
  75. func TestPattern(t *testing.T) {
  76. for _, test := range []struct {
  77. in, want string
  78. }{
  79. {"", "^$"},
  80. {"abc", "^abc$"},
  81. {"*ab*", "^.*ab.*$"},
  82. {`a\*b`, `^a\\.*b$`},
  83. {"***", "^.*.*.*$"},
  84. } {
  85. got := pattern(test.in).String()
  86. if got != test.want {
  87. t.Errorf("%q: got %s, want %s", test.in, got, test.want)
  88. }
  89. }
  90. }
  91. func TestScrubQuery(t *testing.T) {
  92. clear := []tRegexp{pattern("c*")}
  93. remove := []tRegexp{pattern("r*")}
  94. for _, test := range []struct {
  95. in, want string
  96. }{
  97. {"", ""},
  98. {"a=1", "a=1"},
  99. {"a=1&b=2;g=3", "a=1&b=2;g=3"},
  100. {"a=1&r=2;c=3", "a=1&c=CLEARED"},
  101. {"ra=1&rb=2&rc=3", ""},
  102. {"a=1&%Z=2&r=3&c=4", "a=1&%Z=2&c=CLEARED"},
  103. } {
  104. got := scrubQuery(test.in, clear, remove)
  105. if got != test.want {
  106. t.Errorf("%s: got %q, want %q", test.in, got, test.want)
  107. }
  108. }
  109. }