OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [net/] [http/] [cookie_test.go] - Blame information for rev 848

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
        "encoding/json"
9
        "fmt"
10
        "reflect"
11
        "testing"
12
        "time"
13
)
14
 
15
var writeSetCookiesTests = []struct {
16
        Cookie *Cookie
17
        Raw    string
18
}{
19
        {
20
                &Cookie{Name: "cookie-1", Value: "v$1"},
21
                "cookie-1=v$1",
22
        },
23
        {
24
                &Cookie{Name: "cookie-2", Value: "two", MaxAge: 3600},
25
                "cookie-2=two; Max-Age=3600",
26
        },
27
        {
28
                &Cookie{Name: "cookie-3", Value: "three", Domain: ".example.com"},
29
                "cookie-3=three; Domain=.example.com",
30
        },
31
        {
32
                &Cookie{Name: "cookie-4", Value: "four", Path: "/restricted/"},
33
                "cookie-4=four; Path=/restricted/",
34
        },
35
}
36
 
37
func TestWriteSetCookies(t *testing.T) {
38
        for i, tt := range writeSetCookiesTests {
39
                if g, e := tt.Cookie.String(), tt.Raw; g != e {
40
                        t.Errorf("Test %d, expecting:\n%s\nGot:\n%s\n", i, e, g)
41
                        continue
42
                }
43
        }
44
}
45
 
46
type headerOnlyResponseWriter Header
47
 
48
func (ho headerOnlyResponseWriter) Header() Header {
49
        return Header(ho)
50
}
51
 
52
func (ho headerOnlyResponseWriter) Write([]byte) (int, error) {
53
        panic("NOIMPL")
54
}
55
 
56
func (ho headerOnlyResponseWriter) WriteHeader(int) {
57
        panic("NOIMPL")
58
}
59
 
60
func TestSetCookie(t *testing.T) {
61
        m := make(Header)
62
        SetCookie(headerOnlyResponseWriter(m), &Cookie{Name: "cookie-1", Value: "one", Path: "/restricted/"})
63
        SetCookie(headerOnlyResponseWriter(m), &Cookie{Name: "cookie-2", Value: "two", MaxAge: 3600})
64
        if l := len(m["Set-Cookie"]); l != 2 {
65
                t.Fatalf("expected %d cookies, got %d", 2, l)
66
        }
67
        if g, e := m["Set-Cookie"][0], "cookie-1=one; Path=/restricted/"; g != e {
68
                t.Errorf("cookie #1: want %q, got %q", e, g)
69
        }
70
        if g, e := m["Set-Cookie"][1], "cookie-2=two; Max-Age=3600"; g != e {
71
                t.Errorf("cookie #2: want %q, got %q", e, g)
72
        }
73
}
74
 
75
var addCookieTests = []struct {
76
        Cookies []*Cookie
77
        Raw     string
78
}{
79
        {
80
                []*Cookie{},
81
                "",
82
        },
83
        {
84
                []*Cookie{{Name: "cookie-1", Value: "v$1"}},
85
                "cookie-1=v$1",
86
        },
87
        {
88
                []*Cookie{
89
                        {Name: "cookie-1", Value: "v$1"},
90
                        {Name: "cookie-2", Value: "v$2"},
91
                        {Name: "cookie-3", Value: "v$3"},
92
                },
93
                "cookie-1=v$1; cookie-2=v$2; cookie-3=v$3",
94
        },
95
}
96
 
97
func TestAddCookie(t *testing.T) {
98
        for i, tt := range addCookieTests {
99
                req, _ := NewRequest("GET", "http://example.com/", nil)
100
                for _, c := range tt.Cookies {
101
                        req.AddCookie(c)
102
                }
103
                if g := req.Header.Get("Cookie"); g != tt.Raw {
104
                        t.Errorf("Test %d:\nwant: %s\n got: %s\n", i, tt.Raw, g)
105
                        continue
106
                }
107
        }
108
}
109
 
110
var readSetCookiesTests = []struct {
111
        Header  Header
112
        Cookies []*Cookie
113
}{
114
        {
115
                Header{"Set-Cookie": {"Cookie-1=v$1"}},
116
                []*Cookie{{Name: "Cookie-1", Value: "v$1", Raw: "Cookie-1=v$1"}},
117
        },
118
        {
119
                Header{"Set-Cookie": {"NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly"}},
120
                []*Cookie{{
121
                        Name:       "NID",
122
                        Value:      "99=YsDT5i3E-CXax-",
123
                        Path:       "/",
124
                        Domain:     ".google.ch",
125
                        HttpOnly:   true,
126
                        Expires:    time.Date(2011, 11, 23, 1, 5, 3, 0, time.UTC),
127
                        RawExpires: "Wed, 23-Nov-2011 01:05:03 GMT",
128
                        Raw:        "NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly",
129
                }},
130
        },
131
}
132
 
133
func toJSON(v interface{}) string {
134
        b, err := json.Marshal(v)
135
        if err != nil {
136
                return fmt.Sprintf("%#v", v)
137
        }
138
        return string(b)
139
}
140
 
141
func TestReadSetCookies(t *testing.T) {
142
        for i, tt := range readSetCookiesTests {
143
                for n := 0; n < 2; n++ { // to verify readSetCookies doesn't mutate its input
144
                        c := readSetCookies(tt.Header)
145
                        if !reflect.DeepEqual(c, tt.Cookies) {
146
                                t.Errorf("#%d readSetCookies: have\n%s\nwant\n%s\n", i, toJSON(c), toJSON(tt.Cookies))
147
                                continue
148
                        }
149
                }
150
        }
151
}
152
 
153
var readCookiesTests = []struct {
154
        Header  Header
155
        Filter  string
156
        Cookies []*Cookie
157
}{
158
        {
159
                Header{"Cookie": {"Cookie-1=v$1", "c2=v2"}},
160
                "",
161
                []*Cookie{
162
                        {Name: "Cookie-1", Value: "v$1"},
163
                        {Name: "c2", Value: "v2"},
164
                },
165
        },
166
        {
167
                Header{"Cookie": {"Cookie-1=v$1", "c2=v2"}},
168
                "c2",
169
                []*Cookie{
170
                        {Name: "c2", Value: "v2"},
171
                },
172
        },
173
        {
174
                Header{"Cookie": {"Cookie-1=v$1; c2=v2"}},
175
                "",
176
                []*Cookie{
177
                        {Name: "Cookie-1", Value: "v$1"},
178
                        {Name: "c2", Value: "v2"},
179
                },
180
        },
181
        {
182
                Header{"Cookie": {"Cookie-1=v$1; c2=v2"}},
183
                "c2",
184
                []*Cookie{
185
                        {Name: "c2", Value: "v2"},
186
                },
187
        },
188
}
189
 
190
func TestReadCookies(t *testing.T) {
191
        for i, tt := range readCookiesTests {
192
                for n := 0; n < 2; n++ { // to verify readCookies doesn't mutate its input
193
                        c := readCookies(tt.Header, tt.Filter)
194
                        if !reflect.DeepEqual(c, tt.Cookies) {
195
                                t.Errorf("#%d readCookies:\nhave: %s\nwant: %s\n", i, toJSON(c), toJSON(tt.Cookies))
196
                                continue
197
                        }
198
                }
199
        }
200
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.