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