~chipaca/ubuntu-push/gsettings

« back to all changes in this revision

Viewing changes to http13client/cookie_test.go

  • Committer: Samuele Pedroni (Canonical Services Ltd.)
  • Date: 2014-03-19 20:20:19 UTC
  • mto: This revision was merged to the branch mainline in revision 82.
  • Revision ID: samuele.pedroni@canonical.com-20140319202019-p0w8krshj1098f82
grab go 1.3 dev net/http and massage it so that the test run on 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2010 The Go Authors.  All rights reserved.
 
2
// Use of this source code is governed by a BSD-style
 
3
// license that can be found in the LICENSE file.
 
4
 
 
5
package http
 
6
 
 
7
import (
 
8
        "bytes"
 
9
        "encoding/json"
 
10
        "fmt"
 
11
        "log"
 
12
        "os"
 
13
        "reflect"
 
14
        "strings"
 
15
        "testing"
 
16
        "time"
 
17
)
 
18
 
 
19
var writeSetCookiesTests = []struct {
 
20
        Cookie *Cookie
 
21
        Raw    string
 
22
}{
 
23
        {
 
24
                &Cookie{Name: "cookie-1", Value: "v$1"},
 
25
                "cookie-1=v$1",
 
26
        },
 
27
        {
 
28
                &Cookie{Name: "cookie-2", Value: "two", MaxAge: 3600},
 
29
                "cookie-2=two; Max-Age=3600",
 
30
        },
 
31
        {
 
32
                &Cookie{Name: "cookie-3", Value: "three", Domain: ".example.com"},
 
33
                "cookie-3=three; Domain=example.com",
 
34
        },
 
35
        {
 
36
                &Cookie{Name: "cookie-4", Value: "four", Path: "/restricted/"},
 
37
                "cookie-4=four; Path=/restricted/",
 
38
        },
 
39
        {
 
40
                &Cookie{Name: "cookie-5", Value: "five", Domain: "wrong;bad.abc"},
 
41
                "cookie-5=five",
 
42
        },
 
43
        {
 
44
                &Cookie{Name: "cookie-6", Value: "six", Domain: "bad-.abc"},
 
45
                "cookie-6=six",
 
46
        },
 
47
        {
 
48
                &Cookie{Name: "cookie-7", Value: "seven", Domain: "127.0.0.1"},
 
49
                "cookie-7=seven; Domain=127.0.0.1",
 
50
        },
 
51
        {
 
52
                &Cookie{Name: "cookie-8", Value: "eight", Domain: "::1"},
 
53
                "cookie-8=eight",
 
54
        },
 
55
}
 
56
 
 
57
func TestWriteSetCookies(t *testing.T) {
 
58
        defer log.SetOutput(os.Stderr)
 
59
        var logbuf bytes.Buffer
 
60
        log.SetOutput(&logbuf)
 
61
 
 
62
        for i, tt := range writeSetCookiesTests {
 
63
                if g, e := tt.Cookie.String(), tt.Raw; g != e {
 
64
                        t.Errorf("Test %d, expecting:\n%s\nGot:\n%s\n", i, e, g)
 
65
                        continue
 
66
                }
 
67
        }
 
68
 
 
69
        if got, sub := logbuf.String(), "dropping domain attribute"; !strings.Contains(got, sub) {
 
70
                t.Errorf("Expected substring %q in log output. Got:\n%s", sub, got)
 
71
        }
 
72
}
 
73
 
 
74
type headerOnlyResponseWriter Header
 
75
 
 
76
func (ho headerOnlyResponseWriter) Header() Header {
 
77
        return Header(ho)
 
78
}
 
79
 
 
80
func (ho headerOnlyResponseWriter) Write([]byte) (int, error) {
 
81
        panic("NOIMPL")
 
82
}
 
83
 
 
84
func (ho headerOnlyResponseWriter) WriteHeader(int) {
 
85
        panic("NOIMPL")
 
86
}
 
87
 
 
88
func TestSetCookie(t *testing.T) {
 
89
        m := make(Header)
 
90
        SetCookie(headerOnlyResponseWriter(m), &Cookie{Name: "cookie-1", Value: "one", Path: "/restricted/"})
 
91
        SetCookie(headerOnlyResponseWriter(m), &Cookie{Name: "cookie-2", Value: "two", MaxAge: 3600})
 
92
        if l := len(m["Set-Cookie"]); l != 2 {
 
93
                t.Fatalf("expected %d cookies, got %d", 2, l)
 
94
        }
 
95
        if g, e := m["Set-Cookie"][0], "cookie-1=one; Path=/restricted/"; g != e {
 
96
                t.Errorf("cookie #1: want %q, got %q", e, g)
 
97
        }
 
98
        if g, e := m["Set-Cookie"][1], "cookie-2=two; Max-Age=3600"; g != e {
 
99
                t.Errorf("cookie #2: want %q, got %q", e, g)
 
100
        }
 
101
}
 
102
 
 
103
var addCookieTests = []struct {
 
104
        Cookies []*Cookie
 
105
        Raw     string
 
106
}{
 
107
        {
 
108
                []*Cookie{},
 
109
                "",
 
110
        },
 
111
        {
 
112
                []*Cookie{{Name: "cookie-1", Value: "v$1"}},
 
113
                "cookie-1=v$1",
 
114
        },
 
115
        {
 
116
                []*Cookie{
 
117
                        {Name: "cookie-1", Value: "v$1"},
 
118
                        {Name: "cookie-2", Value: "v$2"},
 
119
                        {Name: "cookie-3", Value: "v$3"},
 
120
                },
 
121
                "cookie-1=v$1; cookie-2=v$2; cookie-3=v$3",
 
122
        },
 
123
}
 
124
 
 
125
func TestAddCookie(t *testing.T) {
 
126
        for i, tt := range addCookieTests {
 
127
                req, _ := NewRequest("GET", "http://example.com/", nil)
 
128
                for _, c := range tt.Cookies {
 
129
                        req.AddCookie(c)
 
130
                }
 
131
                if g := req.Header.Get("Cookie"); g != tt.Raw {
 
132
                        t.Errorf("Test %d:\nwant: %s\n got: %s\n", i, tt.Raw, g)
 
133
                        continue
 
134
                }
 
135
        }
 
136
}
 
137
 
 
138
var readSetCookiesTests = []struct {
 
139
        Header  Header
 
140
        Cookies []*Cookie
 
141
}{
 
142
        {
 
143
                Header{"Set-Cookie": {"Cookie-1=v$1"}},
 
144
                []*Cookie{{Name: "Cookie-1", Value: "v$1", Raw: "Cookie-1=v$1"}},
 
145
        },
 
146
        {
 
147
                Header{"Set-Cookie": {"NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly"}},
 
148
                []*Cookie{{
 
149
                        Name:       "NID",
 
150
                        Value:      "99=YsDT5i3E-CXax-",
 
151
                        Path:       "/",
 
152
                        Domain:     ".google.ch",
 
153
                        HttpOnly:   true,
 
154
                        Expires:    time.Date(2011, 11, 23, 1, 5, 3, 0, time.UTC),
 
155
                        RawExpires: "Wed, 23-Nov-2011 01:05:03 GMT",
 
156
                        Raw:        "NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly",
 
157
                }},
 
158
        },
 
159
        {
 
160
                Header{"Set-Cookie": {".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly"}},
 
161
                []*Cookie{{
 
162
                        Name:       ".ASPXAUTH",
 
163
                        Value:      "7E3AA",
 
164
                        Path:       "/",
 
165
                        Expires:    time.Date(2012, 3, 7, 14, 25, 6, 0, time.UTC),
 
166
                        RawExpires: "Wed, 07-Mar-2012 14:25:06 GMT",
 
167
                        HttpOnly:   true,
 
168
                        Raw:        ".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly",
 
169
                }},
 
170
        },
 
171
        {
 
172
                Header{"Set-Cookie": {"ASP.NET_SessionId=foo; path=/; HttpOnly"}},
 
173
                []*Cookie{{
 
174
                        Name:     "ASP.NET_SessionId",
 
175
                        Value:    "foo",
 
176
                        Path:     "/",
 
177
                        HttpOnly: true,
 
178
                        Raw:      "ASP.NET_SessionId=foo; path=/; HttpOnly",
 
179
                }},
 
180
        },
 
181
 
 
182
        // TODO(bradfitz): users have reported seeing this in the
 
183
        // wild, but do browsers handle it? RFC 6265 just says "don't
 
184
        // do that" (section 3) and then never mentions header folding
 
185
        // again.
 
186
        // Header{"Set-Cookie": {"ASP.NET_SessionId=foo; path=/; HttpOnly, .ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly"}},
 
187
}
 
188
 
 
189
func toJSON(v interface{}) string {
 
190
        b, err := json.Marshal(v)
 
191
        if err != nil {
 
192
                return fmt.Sprintf("%#v", v)
 
193
        }
 
194
        return string(b)
 
195
}
 
196
 
 
197
func TestReadSetCookies(t *testing.T) {
 
198
        for i, tt := range readSetCookiesTests {
 
199
                for n := 0; n < 2; n++ { // to verify readSetCookies doesn't mutate its input
 
200
                        c := readSetCookies(tt.Header)
 
201
                        if !reflect.DeepEqual(c, tt.Cookies) {
 
202
                                t.Errorf("#%d readSetCookies: have\n%s\nwant\n%s\n", i, toJSON(c), toJSON(tt.Cookies))
 
203
                                continue
 
204
                        }
 
205
                }
 
206
        }
 
207
}
 
208
 
 
209
var readCookiesTests = []struct {
 
210
        Header  Header
 
211
        Filter  string
 
212
        Cookies []*Cookie
 
213
}{
 
214
        {
 
215
                Header{"Cookie": {"Cookie-1=v$1", "c2=v2"}},
 
216
                "",
 
217
                []*Cookie{
 
218
                        {Name: "Cookie-1", Value: "v$1"},
 
219
                        {Name: "c2", Value: "v2"},
 
220
                },
 
221
        },
 
222
        {
 
223
                Header{"Cookie": {"Cookie-1=v$1", "c2=v2"}},
 
224
                "c2",
 
225
                []*Cookie{
 
226
                        {Name: "c2", Value: "v2"},
 
227
                },
 
228
        },
 
229
        {
 
230
                Header{"Cookie": {"Cookie-1=v$1; c2=v2"}},
 
231
                "",
 
232
                []*Cookie{
 
233
                        {Name: "Cookie-1", Value: "v$1"},
 
234
                        {Name: "c2", Value: "v2"},
 
235
                },
 
236
        },
 
237
        {
 
238
                Header{"Cookie": {"Cookie-1=v$1; c2=v2"}},
 
239
                "c2",
 
240
                []*Cookie{
 
241
                        {Name: "c2", Value: "v2"},
 
242
                },
 
243
        },
 
244
}
 
245
 
 
246
func TestReadCookies(t *testing.T) {
 
247
        for i, tt := range readCookiesTests {
 
248
                for n := 0; n < 2; n++ { // to verify readCookies doesn't mutate its input
 
249
                        c := readCookies(tt.Header, tt.Filter)
 
250
                        if !reflect.DeepEqual(c, tt.Cookies) {
 
251
                                t.Errorf("#%d readCookies:\nhave: %s\nwant: %s\n", i, toJSON(c), toJSON(tt.Cookies))
 
252
                                continue
 
253
                        }
 
254
                }
 
255
        }
 
256
}
 
257
 
 
258
func TestCookieSanitizeValue(t *testing.T) {
 
259
        defer log.SetOutput(os.Stderr)
 
260
        var logbuf bytes.Buffer
 
261
        log.SetOutput(&logbuf)
 
262
 
 
263
        tests := []struct {
 
264
                in, want string
 
265
        }{
 
266
                {"foo", "foo"},
 
267
                {"foo bar", "foobar"},
 
268
                {"\x00\x7e\x7f\x80", "\x7e"},
 
269
                {`"withquotes"`, "withquotes"},
 
270
        }
 
271
        for _, tt := range tests {
 
272
                if got := sanitizeCookieValue(tt.in); got != tt.want {
 
273
                        t.Errorf("sanitizeCookieValue(%q) = %q; want %q", tt.in, got, tt.want)
 
274
                }
 
275
        }
 
276
 
 
277
        if got, sub := logbuf.String(), "dropping invalid bytes"; !strings.Contains(got, sub) {
 
278
                t.Errorf("Expected substring %q in log output. Got:\n%s", sub, got)
 
279
        }
 
280
}
 
281
 
 
282
func TestCookieSanitizePath(t *testing.T) {
 
283
        defer log.SetOutput(os.Stderr)
 
284
        var logbuf bytes.Buffer
 
285
        log.SetOutput(&logbuf)
 
286
 
 
287
        tests := []struct {
 
288
                in, want string
 
289
        }{
 
290
                {"/path", "/path"},
 
291
                {"/path with space/", "/path with space/"},
 
292
                {"/just;no;semicolon\x00orstuff/", "/justnosemicolonorstuff/"},
 
293
        }
 
294
        for _, tt := range tests {
 
295
                if got := sanitizeCookiePath(tt.in); got != tt.want {
 
296
                        t.Errorf("sanitizeCookiePath(%q) = %q; want %q", tt.in, got, tt.want)
 
297
                }
 
298
        }
 
299
 
 
300
        if got, sub := logbuf.String(), "dropping invalid bytes"; !strings.Contains(got, sub) {
 
301
                t.Errorf("Expected substring %q in log output. Got:\n%s", sub, got)
 
302
        }
 
303
}