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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [net/] [textproto/] [reader_test.go] - Blame information for rev 761

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 textproto
6
 
7
import (
8
        "bufio"
9
        "io"
10
        "reflect"
11
        "strings"
12
        "testing"
13
)
14
 
15
type canonicalHeaderKeyTest struct {
16
        in, out string
17
}
18
 
19
var canonicalHeaderKeyTests = []canonicalHeaderKeyTest{
20
        {"a-b-c", "A-B-C"},
21
        {"a-1-c", "A-1-C"},
22
        {"User-Agent", "User-Agent"},
23
        {"uSER-aGENT", "User-Agent"},
24
        {"user-agent", "User-Agent"},
25
        {"USER-AGENT", "User-Agent"},
26
}
27
 
28
func TestCanonicalMIMEHeaderKey(t *testing.T) {
29
        for _, tt := range canonicalHeaderKeyTests {
30
                if s := CanonicalMIMEHeaderKey(tt.in); s != tt.out {
31
                        t.Errorf("CanonicalMIMEHeaderKey(%q) = %q, want %q", tt.in, s, tt.out)
32
                }
33
        }
34
}
35
 
36
func reader(s string) *Reader {
37
        return NewReader(bufio.NewReader(strings.NewReader(s)))
38
}
39
 
40
func TestReadLine(t *testing.T) {
41
        r := reader("line1\nline2\n")
42
        s, err := r.ReadLine()
43
        if s != "line1" || err != nil {
44
                t.Fatalf("Line 1: %s, %v", s, err)
45
        }
46
        s, err = r.ReadLine()
47
        if s != "line2" || err != nil {
48
                t.Fatalf("Line 2: %s, %v", s, err)
49
        }
50
        s, err = r.ReadLine()
51
        if s != "" || err != io.EOF {
52
                t.Fatalf("EOF: %s, %v", s, err)
53
        }
54
}
55
 
56
func TestReadContinuedLine(t *testing.T) {
57
        r := reader("line1\nline\n 2\nline3\n")
58
        s, err := r.ReadContinuedLine()
59
        if s != "line1" || err != nil {
60
                t.Fatalf("Line 1: %s, %v", s, err)
61
        }
62
        s, err = r.ReadContinuedLine()
63
        if s != "line 2" || err != nil {
64
                t.Fatalf("Line 2: %s, %v", s, err)
65
        }
66
        s, err = r.ReadContinuedLine()
67
        if s != "line3" || err != nil {
68
                t.Fatalf("Line 3: %s, %v", s, err)
69
        }
70
        s, err = r.ReadContinuedLine()
71
        if s != "" || err != io.EOF {
72
                t.Fatalf("EOF: %s, %v", s, err)
73
        }
74
}
75
 
76
func TestReadCodeLine(t *testing.T) {
77
        r := reader("123 hi\n234 bye\n345 no way\n")
78
        code, msg, err := r.ReadCodeLine(0)
79
        if code != 123 || msg != "hi" || err != nil {
80
                t.Fatalf("Line 1: %d, %s, %v", code, msg, err)
81
        }
82
        code, msg, err = r.ReadCodeLine(23)
83
        if code != 234 || msg != "bye" || err != nil {
84
                t.Fatalf("Line 2: %d, %s, %v", code, msg, err)
85
        }
86
        code, msg, err = r.ReadCodeLine(346)
87
        if code != 345 || msg != "no way" || err == nil {
88
                t.Fatalf("Line 3: %d, %s, %v", code, msg, err)
89
        }
90
        if e, ok := err.(*Error); !ok || e.Code != code || e.Msg != msg {
91
                t.Fatalf("Line 3: wrong error %v\n", err)
92
        }
93
        code, msg, err = r.ReadCodeLine(1)
94
        if code != 0 || msg != "" || err != io.EOF {
95
                t.Fatalf("EOF: %d, %s, %v", code, msg, err)
96
        }
97
}
98
 
99
func TestReadDotLines(t *testing.T) {
100
        r := reader("dotlines\r\n.foo\r\n..bar\n...baz\nquux\r\n\r\n.\r\nanother\n")
101
        s, err := r.ReadDotLines()
102
        want := []string{"dotlines", "foo", ".bar", "..baz", "quux", ""}
103
        if !reflect.DeepEqual(s, want) || err != nil {
104
                t.Fatalf("ReadDotLines: %v, %v", s, err)
105
        }
106
 
107
        s, err = r.ReadDotLines()
108
        want = []string{"another"}
109
        if !reflect.DeepEqual(s, want) || err != io.ErrUnexpectedEOF {
110
                t.Fatalf("ReadDotLines2: %v, %v", s, err)
111
        }
112
}
113
 
114
func TestReadDotBytes(t *testing.T) {
115
        r := reader("dotlines\r\n.foo\r\n..bar\n...baz\nquux\r\n\r\n.\r\nanot.her\r\n")
116
        b, err := r.ReadDotBytes()
117
        want := []byte("dotlines\nfoo\n.bar\n..baz\nquux\n\n")
118
        if !reflect.DeepEqual(b, want) || err != nil {
119
                t.Fatalf("ReadDotBytes: %q, %v", b, err)
120
        }
121
 
122
        b, err = r.ReadDotBytes()
123
        want = []byte("anot.her\n")
124
        if !reflect.DeepEqual(b, want) || err != io.ErrUnexpectedEOF {
125
                t.Fatalf("ReadDotBytes2: %q, %v", b, err)
126
        }
127
}
128
 
129
func TestReadMIMEHeader(t *testing.T) {
130
        r := reader("my-key: Value 1  \r\nLong-key: Even \n Longer Value\r\nmy-Key: Value 2\r\n\n")
131
        m, err := r.ReadMIMEHeader()
132
        want := MIMEHeader{
133
                "My-Key":   {"Value 1", "Value 2"},
134
                "Long-Key": {"Even Longer Value"},
135
        }
136
        if !reflect.DeepEqual(m, want) || err != nil {
137
                t.Fatalf("ReadMIMEHeader: %v, %v; want %v", m, err, want)
138
        }
139
}
140
 
141
func TestReadMIMEHeaderSingle(t *testing.T) {
142
        r := reader("Foo: bar\n\n")
143
        m, err := r.ReadMIMEHeader()
144
        want := MIMEHeader{"Foo": {"bar"}}
145
        if !reflect.DeepEqual(m, want) || err != nil {
146
                t.Fatalf("ReadMIMEHeader: %v, %v; want %v", m, err, want)
147
        }
148
}
149
 
150
func TestLargeReadMIMEHeader(t *testing.T) {
151
        data := make([]byte, 16*1024)
152
        for i := 0; i < len(data); i++ {
153
                data[i] = 'x'
154
        }
155
        sdata := string(data)
156
        r := reader("Cookie: " + sdata + "\r\n\n")
157
        m, err := r.ReadMIMEHeader()
158
        if err != nil {
159
                t.Fatalf("ReadMIMEHeader: %v", err)
160
        }
161
        cookie := m.Get("Cookie")
162
        if cookie != sdata {
163
                t.Fatalf("ReadMIMEHeader: %v bytes, want %v bytes", len(cookie), len(sdata))
164
        }
165
}
166
 
167
type readResponseTest struct {
168
        in       string
169
        inCode   int
170
        wantCode int
171
        wantMsg  string
172
}
173
 
174
var readResponseTests = []readResponseTest{
175
        {"230-Anonymous access granted, restrictions apply\n" +
176
                "Read the file README.txt,\n" +
177
                "230  please",
178
                23,
179
                230,
180
                "Anonymous access granted, restrictions apply\nRead the file README.txt,\n please",
181
        },
182
 
183
        {"230 Anonymous access granted, restrictions apply\n",
184
                23,
185
                230,
186
                "Anonymous access granted, restrictions apply",
187
        },
188
 
189
        {"400-A\n400-B\n400 C",
190
                4,
191
                400,
192
                "A\nB\nC",
193
        },
194
 
195
        {"400-A\r\n400-B\r\n400 C\r\n",
196
                4,
197
                400,
198
                "A\nB\nC",
199
        },
200
}
201
 
202
// See http://www.ietf.org/rfc/rfc959.txt page 36.
203
func TestRFC959Lines(t *testing.T) {
204
        for i, tt := range readResponseTests {
205
                r := reader(tt.in + "\nFOLLOWING DATA")
206
                code, msg, err := r.ReadResponse(tt.inCode)
207
                if err != nil {
208
                        t.Errorf("#%d: ReadResponse: %v", i, err)
209
                        continue
210
                }
211
                if code != tt.wantCode {
212
                        t.Errorf("#%d: code=%d, want %d", i, code, tt.wantCode)
213
                }
214
                if msg != tt.wantMsg {
215
                        t.Errorf("#%d: msg=%q, want %q", i, msg, tt.wantMsg)
216
                }
217
        }
218
}

powered by: WebSVN 2.1.0

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