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

Subversion Repositories openrisc

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

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

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 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_test
6
 
7
import (
8
        "bytes"
9
        "fmt"
10
        "io"
11
        "io/ioutil"
12
        "mime/multipart"
13
        . "net/http"
14
        "net/http/httptest"
15
        "net/url"
16
        "os"
17
        "reflect"
18
        "regexp"
19
        "strings"
20
        "testing"
21
)
22
 
23
func TestQuery(t *testing.T) {
24
        req := &Request{Method: "GET"}
25
        req.URL, _ = url.Parse("http://www.google.com/search?q=foo&q=bar")
26
        if q := req.FormValue("q"); q != "foo" {
27
                t.Errorf(`req.FormValue("q") = %q, want "foo"`, q)
28
        }
29
}
30
 
31
func TestPostQuery(t *testing.T) {
32
        req, _ := NewRequest("POST", "http://www.google.com/search?q=foo&q=bar&both=x",
33
                strings.NewReader("z=post&both=y"))
34
        req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
35
 
36
        if q := req.FormValue("q"); q != "foo" {
37
                t.Errorf(`req.FormValue("q") = %q, want "foo"`, q)
38
        }
39
        if z := req.FormValue("z"); z != "post" {
40
                t.Errorf(`req.FormValue("z") = %q, want "post"`, z)
41
        }
42
        if both := req.Form["both"]; !reflect.DeepEqual(both, []string{"x", "y"}) {
43
                t.Errorf(`req.FormValue("both") = %q, want ["x", "y"]`, both)
44
        }
45
}
46
 
47
type stringMap map[string][]string
48
type parseContentTypeTest struct {
49
        shouldError bool
50
        contentType stringMap
51
}
52
 
53
var parseContentTypeTests = []parseContentTypeTest{
54
        {false, stringMap{"Content-Type": {"text/plain"}}},
55
        // Non-existent keys are not placed. The value nil is illegal.
56
        {true, stringMap{}},
57
        {true, stringMap{"Content-Type": {"text/plain; boundary="}}},
58
        {false, stringMap{"Content-Type": {"application/unknown"}}},
59
}
60
 
61
func TestParseFormUnknownContentType(t *testing.T) {
62
        for i, test := range parseContentTypeTests {
63
                req := &Request{
64
                        Method: "POST",
65
                        Header: Header(test.contentType),
66
                        Body:   ioutil.NopCloser(bytes.NewBufferString("body")),
67
                }
68
                err := req.ParseForm()
69
                switch {
70
                case err == nil && test.shouldError:
71
                        t.Errorf("test %d should have returned error", i)
72
                case err != nil && !test.shouldError:
73
                        t.Errorf("test %d should not have returned error, got %v", i, err)
74
                }
75
        }
76
}
77
 
78
func TestMultipartReader(t *testing.T) {
79
        req := &Request{
80
                Method: "POST",
81
                Header: Header{"Content-Type": {`multipart/form-data; boundary="foo123"`}},
82
                Body:   ioutil.NopCloser(new(bytes.Buffer)),
83
        }
84
        multipart, err := req.MultipartReader()
85
        if multipart == nil {
86
                t.Errorf("expected multipart; error: %v", err)
87
        }
88
 
89
        req.Header = Header{"Content-Type": {"text/plain"}}
90
        multipart, err = req.MultipartReader()
91
        if multipart != nil {
92
                t.Errorf("unexpected multipart for text/plain")
93
        }
94
}
95
 
96
func TestRedirect(t *testing.T) {
97
        ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
98
                switch r.URL.Path {
99
                case "/":
100
                        w.Header().Set("Location", "/foo/")
101
                        w.WriteHeader(StatusSeeOther)
102
                case "/foo/":
103
                        fmt.Fprintf(w, "foo")
104
                default:
105
                        w.WriteHeader(StatusBadRequest)
106
                }
107
        }))
108
        defer ts.Close()
109
 
110
        var end = regexp.MustCompile("/foo/$")
111
        r, err := Get(ts.URL)
112
        if err != nil {
113
                t.Fatal(err)
114
        }
115
        r.Body.Close()
116
        url := r.Request.URL.String()
117
        if r.StatusCode != 200 || !end.MatchString(url) {
118
                t.Fatalf("Get got status %d at %q, want 200 matching /foo/$", r.StatusCode, url)
119
        }
120
}
121
 
122
func TestSetBasicAuth(t *testing.T) {
123
        r, _ := NewRequest("GET", "http://example.com/", nil)
124
        r.SetBasicAuth("Aladdin", "open sesame")
125
        if g, e := r.Header.Get("Authorization"), "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="; g != e {
126
                t.Errorf("got header %q, want %q", g, e)
127
        }
128
}
129
 
130
func TestMultipartRequest(t *testing.T) {
131
        // Test that we can read the values and files of a
132
        // multipart request with FormValue and FormFile,
133
        // and that ParseMultipartForm can be called multiple times.
134
        req := newTestMultipartRequest(t)
135
        if err := req.ParseMultipartForm(25); err != nil {
136
                t.Fatal("ParseMultipartForm first call:", err)
137
        }
138
        defer req.MultipartForm.RemoveAll()
139
        validateTestMultipartContents(t, req, false)
140
        if err := req.ParseMultipartForm(25); err != nil {
141
                t.Fatal("ParseMultipartForm second call:", err)
142
        }
143
        validateTestMultipartContents(t, req, false)
144
}
145
 
146
func TestMultipartRequestAuto(t *testing.T) {
147
        // Test that FormValue and FormFile automatically invoke
148
        // ParseMultipartForm and return the right values.
149
        req := newTestMultipartRequest(t)
150
        defer func() {
151
                if req.MultipartForm != nil {
152
                        req.MultipartForm.RemoveAll()
153
                }
154
        }()
155
        validateTestMultipartContents(t, req, true)
156
}
157
 
158
func TestEmptyMultipartRequest(t *testing.T) {
159
        // Test that FormValue and FormFile automatically invoke
160
        // ParseMultipartForm and return the right values.
161
        req, err := NewRequest("GET", "/", nil)
162
        if err != nil {
163
                t.Errorf("NewRequest err = %q", err)
164
        }
165
        testMissingFile(t, req)
166
}
167
 
168
func TestRequestMultipartCallOrder(t *testing.T) {
169
        req := newTestMultipartRequest(t)
170
        _, err := req.MultipartReader()
171
        if err != nil {
172
                t.Fatalf("MultipartReader: %v", err)
173
        }
174
        err = req.ParseMultipartForm(1024)
175
        if err == nil {
176
                t.Errorf("expected an error from ParseMultipartForm after call to MultipartReader")
177
        }
178
}
179
 
180
func testMissingFile(t *testing.T, req *Request) {
181
        f, fh, err := req.FormFile("missing")
182
        if f != nil {
183
                t.Errorf("FormFile file = %q, want nil", f)
184
        }
185
        if fh != nil {
186
                t.Errorf("FormFile file header = %q, want nil", fh)
187
        }
188
        if err != ErrMissingFile {
189
                t.Errorf("FormFile err = %q, want ErrMissingFile", err)
190
        }
191
}
192
 
193
func newTestMultipartRequest(t *testing.T) *Request {
194
        b := bytes.NewBufferString(strings.Replace(message, "\n", "\r\n", -1))
195
        req, err := NewRequest("POST", "/", b)
196
        if err != nil {
197
                t.Fatal("NewRequest:", err)
198
        }
199
        ctype := fmt.Sprintf(`multipart/form-data; boundary="%s"`, boundary)
200
        req.Header.Set("Content-type", ctype)
201
        return req
202
}
203
 
204
func validateTestMultipartContents(t *testing.T, req *Request, allMem bool) {
205
        if g, e := req.FormValue("texta"), textaValue; g != e {
206
                t.Errorf("texta value = %q, want %q", g, e)
207
        }
208
        if g, e := req.FormValue("textb"), textbValue; g != e {
209
                t.Errorf("textb value = %q, want %q", g, e)
210
        }
211
        if g := req.FormValue("missing"); g != "" {
212
                t.Errorf("missing value = %q, want empty string", g)
213
        }
214
 
215
        assertMem := func(n string, fd multipart.File) {
216
                if _, ok := fd.(*os.File); ok {
217
                        t.Error(n, " is *os.File, should not be")
218
                }
219
        }
220
        fda := testMultipartFile(t, req, "filea", "filea.txt", fileaContents)
221
        defer fda.Close()
222
        assertMem("filea", fda)
223
        fdb := testMultipartFile(t, req, "fileb", "fileb.txt", filebContents)
224
        defer fdb.Close()
225
        if allMem {
226
                assertMem("fileb", fdb)
227
        } else {
228
                if _, ok := fdb.(*os.File); !ok {
229
                        t.Errorf("fileb has unexpected underlying type %T", fdb)
230
                }
231
        }
232
 
233
        testMissingFile(t, req)
234
}
235
 
236
func testMultipartFile(t *testing.T, req *Request, key, expectFilename, expectContent string) multipart.File {
237
        f, fh, err := req.FormFile(key)
238
        if err != nil {
239
                t.Fatalf("FormFile(%q): %q", key, err)
240
        }
241
        if fh.Filename != expectFilename {
242
                t.Errorf("filename = %q, want %q", fh.Filename, expectFilename)
243
        }
244
        var b bytes.Buffer
245
        _, err = io.Copy(&b, f)
246
        if err != nil {
247
                t.Fatal("copying contents:", err)
248
        }
249
        if g := b.String(); g != expectContent {
250
                t.Errorf("contents = %q, want %q", g, expectContent)
251
        }
252
        return f
253
}
254
 
255
const (
256
        fileaContents = "This is a test file."
257
        filebContents = "Another test file."
258
        textaValue    = "foo"
259
        textbValue    = "bar"
260
        boundary      = `MyBoundary`
261
)
262
 
263
const message = `
264
--MyBoundary
265
Content-Disposition: form-data; name="filea"; filename="filea.txt"
266
Content-Type: text/plain
267
 
268
` + fileaContents + `
269
--MyBoundary
270
Content-Disposition: form-data; name="fileb"; filename="fileb.txt"
271
Content-Type: text/plain
272
 
273
` + filebContents + `
274
--MyBoundary
275
Content-Disposition: form-data; name="texta"
276
 
277
` + textaValue + `
278
--MyBoundary
279
Content-Disposition: form-data; name="textb"
280
 
281
` + textbValue + `
282
--MyBoundary--
283
`

powered by: WebSVN 2.1.0

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