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.
 
 
 

84 lines
2.3 KiB

  1. // Copyright 2018 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. // +build go1.8
  15. package proxy
  16. import (
  17. "encoding/json"
  18. "net/http"
  19. "testing"
  20. "cloud.google.com/go/internal/testutil"
  21. "github.com/google/martian"
  22. "github.com/google/martian/har"
  23. )
  24. func TestWithRedactedHeaders(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. orig := http.Header{
  33. "Content-Type": {"text/plain"},
  34. "Authorization": {"oauth2-token"},
  35. "X-Goog-Encryption-Key": {"a-secret-key"},
  36. "X-Goog-Copy-Source-Encryption-Key": {"another-secret-key"},
  37. }
  38. req := &http.Request{Header: clone(orig)}
  39. var got http.Header
  40. mod := martian.RequestModifierFunc(func(req *http.Request) error {
  41. got = clone(req.Header)
  42. return nil
  43. })
  44. if err := withRedactedHeaders(req, mod); err != nil {
  45. t.Fatal(err)
  46. }
  47. // Logged headers should be redacted.
  48. want := http.Header{
  49. "Content-Type": {"text/plain"},
  50. "Authorization": {"REDACTED"},
  51. "X-Goog-Encryption-Key": {"REDACTED"},
  52. "X-Goog-Copy-Source-Encryption-Key": {"REDACTED"},
  53. }
  54. if !testutil.Equal(got, want) {
  55. t.Errorf("got %+v\nwant %+v", got, want)
  56. }
  57. // The request's headers should be the same.
  58. if got, want := req.Header, orig; !testutil.Equal(got, want) {
  59. t.Errorf("got %+v\nwant %+v", got, want)
  60. }
  61. }
  62. func TestMarshalPostData(t *testing.T) {
  63. want := string([]byte{150, 151, 152})
  64. pd := &har.PostData{Text: want}
  65. byts, err := json.Marshal(pd)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. var pd2 har.PostData
  70. if err := json.Unmarshal(byts, &pd2); err != nil {
  71. t.Fatal(err)
  72. }
  73. if got := pd2.Text; got != want {
  74. t.Errorf("got %v, want %v", got, want)
  75. }
  76. }