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.
 
 
 

102 rindas
2.6 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. package proxy
  15. import (
  16. "net/http"
  17. "testing"
  18. "cloud.google.com/go/internal/testutil"
  19. )
  20. func TestParseRequestBody(t *testing.T) {
  21. wantMediaType := "multipart/mixed"
  22. wantParts := [][]byte{
  23. []byte("A section"),
  24. []byte("And another"),
  25. }
  26. for i, test := range []struct {
  27. contentType, body string
  28. }{
  29. {
  30. wantMediaType + "; boundary=foo",
  31. "--foo\r\nFoo: one\r\n\r\nA section\r\n" +
  32. "--foo\r\nFoo: two\r\n\r\nAnd another\r\n" +
  33. "--foo--\r\n",
  34. },
  35. // Same contents, different boundary.
  36. {
  37. wantMediaType + "; boundary=bar",
  38. "--bar\r\nFoo: one\r\n\r\nA section\r\n" +
  39. "--bar\r\nFoo: two\r\n\r\nAnd another\r\n" +
  40. "--bar--\r\n",
  41. },
  42. } {
  43. gotMediaType, gotParts, err := parseRequestBody(test.contentType, []byte(test.body))
  44. if err != nil {
  45. t.Fatalf("#%d: %v", i, err)
  46. }
  47. if gotMediaType != wantMediaType {
  48. t.Errorf("#%d: got %q, want %q", i, gotMediaType, wantMediaType)
  49. }
  50. if diff := testutil.Diff(gotParts, wantParts); diff != "" {
  51. t.Errorf("#%d: %s", i, diff)
  52. }
  53. }
  54. }
  55. func TestHeadersMatch(t *testing.T) {
  56. for _, test := range []struct {
  57. h1, h2 http.Header
  58. want bool
  59. }{
  60. {
  61. http.Header{"A": {"x"}, "B": {"y", "z"}},
  62. http.Header{"A": {"x"}, "B": {"y", "z"}},
  63. true,
  64. },
  65. {
  66. http.Header{"A": {"x"}, "B": {"y", "z"}},
  67. http.Header{"A": {"x"}, "B": {"w"}},
  68. false,
  69. },
  70. {
  71. http.Header{"A": {"x"}, "B": {"y", "z"}, "I": {"foo"}},
  72. http.Header{"A": {"x"}, "B": {"y", "z"}, "I": {"bar"}},
  73. true,
  74. },
  75. {
  76. http.Header{"A": {"x"}, "B": {"y", "z"}},
  77. http.Header{"A": {"x"}, "B": {"y", "z"}, "I": {"bar"}},
  78. true,
  79. },
  80. {
  81. http.Header{"A": {"x"}, "B": {"y", "z"}, "I": {"foo"}},
  82. http.Header{"A": {"x"}, "I": {"bar"}},
  83. false,
  84. },
  85. {
  86. http.Header{"A": {"x"}, "I": {"foo"}},
  87. http.Header{"A": {"x"}, "B": {"y", "z"}, "I": {"bar"}},
  88. false,
  89. },
  90. } {
  91. got := headersMatch(test.h1, test.h2, map[string]bool{"I": true})
  92. if got != test.want {
  93. t.Errorf("%v, %v: got %t, want %t", test.h1, test.h2, got, test.want)
  94. }
  95. }
  96. }