~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/net/http/request_test.go

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2014-11-18 15:12:26 UTC
  • mfrom: (14.2.12 vivid-proposed)
  • Revision ID: package-import@ubuntu.com-20141118151226-zug7vn93mn3dtiz3
Tags: 2:1.3.2-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - Support co-installability with gccgo-go tool:
    - d/rules,golang-go.install: Rename bin/go -> bin/golang-go
    - d/golang-go.{postinst,prerm}: Install/remove /usr/bin/go using
      alternatives.
  - d/copyright: Amendments for full compiliance with copyright format.
  - d/control: Demote golang-go.tools to Suggests to support Ubuntu MIR.
  - dropped patches (now upstream):
    - d/p/issue27650045_40001_50001.diff
    - d/p/issue28050043_60001_70001.diff
    - d/p/issue54790044_100001_110001.diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
60
60
        }
61
61
}
62
62
 
 
63
func TestPatchQuery(t *testing.T) {
 
64
        req, _ := NewRequest("PATCH", "http://www.google.com/search?q=foo&q=bar&both=x&prio=1&empty=not",
 
65
                strings.NewReader("z=post&both=y&prio=2&empty="))
 
66
        req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
 
67
 
 
68
        if q := req.FormValue("q"); q != "foo" {
 
69
                t.Errorf(`req.FormValue("q") = %q, want "foo"`, q)
 
70
        }
 
71
        if z := req.FormValue("z"); z != "post" {
 
72
                t.Errorf(`req.FormValue("z") = %q, want "post"`, z)
 
73
        }
 
74
        if bq, found := req.PostForm["q"]; found {
 
75
                t.Errorf(`req.PostForm["q"] = %q, want no entry in map`, bq)
 
76
        }
 
77
        if bz := req.PostFormValue("z"); bz != "post" {
 
78
                t.Errorf(`req.PostFormValue("z") = %q, want "post"`, bz)
 
79
        }
 
80
        if qs := req.Form["q"]; !reflect.DeepEqual(qs, []string{"foo", "bar"}) {
 
81
                t.Errorf(`req.Form["q"] = %q, want ["foo", "bar"]`, qs)
 
82
        }
 
83
        if both := req.Form["both"]; !reflect.DeepEqual(both, []string{"y", "x"}) {
 
84
                t.Errorf(`req.Form["both"] = %q, want ["y", "x"]`, both)
 
85
        }
 
86
        if prio := req.FormValue("prio"); prio != "2" {
 
87
                t.Errorf(`req.FormValue("prio") = %q, want "2" (from body)`, prio)
 
88
        }
 
89
        if empty := req.FormValue("empty"); empty != "" {
 
90
                t.Errorf(`req.FormValue("empty") = %q, want "" (from body)`, empty)
 
91
        }
 
92
}
 
93
 
63
94
type stringMap map[string][]string
64
95
type parseContentTypeTest struct {
65
96
        shouldError bool
68
99
 
69
100
var parseContentTypeTests = []parseContentTypeTest{
70
101
        {false, stringMap{"Content-Type": {"text/plain"}}},
71
 
        // Non-existent keys are not placed. The value nil is illegal.
72
 
        {true, stringMap{}},
 
102
        // Empty content type is legal - shoult be treated as
 
103
        // application/octet-stream (RFC 2616, section 7.2.1)
 
104
        {false, stringMap{}},
73
105
        {true, stringMap{"Content-Type": {"text/plain; boundary="}}},
74
106
        {false, stringMap{"Content-Type": {"application/unknown"}}},
75
107
}
79
111
                req := &Request{
80
112
                        Method: "POST",
81
113
                        Header: Header(test.contentType),
82
 
                        Body:   ioutil.NopCloser(bytes.NewBufferString("body")),
 
114
                        Body:   ioutil.NopCloser(strings.NewReader("body")),
83
115
                }
84
116
                err := req.ParseForm()
85
117
                switch {
122
154
        req.Header = Header{"Content-Type": {"text/plain"}}
123
155
        multipart, err = req.MultipartReader()
124
156
        if multipart != nil {
125
 
                t.Errorf("unexpected multipart for text/plain")
 
157
                t.Error("unexpected multipart for text/plain")
 
158
        }
 
159
}
 
160
 
 
161
func TestParseMultipartForm(t *testing.T) {
 
162
        req := &Request{
 
163
                Method: "POST",
 
164
                Header: Header{"Content-Type": {`multipart/form-data; boundary="foo123"`}},
 
165
                Body:   ioutil.NopCloser(new(bytes.Buffer)),
 
166
        }
 
167
        err := req.ParseMultipartForm(25)
 
168
        if err == nil {
 
169
                t.Error("expected multipart EOF, got nil")
 
170
        }
 
171
 
 
172
        req.Header = Header{"Content-Type": {"text/plain"}}
 
173
        err = req.ParseMultipartForm(25)
 
174
        if err != ErrNotMultipart {
 
175
                t.Error("expected ErrNotMultipart for text/plain")
126
176
        }
127
177
}
128
178
 
188
238
        validateTestMultipartContents(t, req, true)
189
239
}
190
240
 
191
 
func TestEmptyMultipartRequest(t *testing.T) {
192
 
        // Test that FormValue and FormFile automatically invoke
193
 
        // ParseMultipartForm and return the right values.
194
 
        req, err := NewRequest("GET", "/", nil)
195
 
        if err != nil {
196
 
                t.Errorf("NewRequest err = %q", err)
197
 
        }
 
241
func TestMissingFileMultipartRequest(t *testing.T) {
 
242
        // Test that FormFile returns an error if
 
243
        // the named file is missing.
 
244
        req := newTestMultipartRequest(t)
198
245
        testMissingFile(t, req)
199
246
}
200
247
 
201
 
func TestRequestMultipartCallOrder(t *testing.T) {
202
 
        req := newTestMultipartRequest(t)
203
 
        _, err := req.MultipartReader()
204
 
        if err != nil {
205
 
                t.Fatalf("MultipartReader: %v", err)
206
 
        }
207
 
        err = req.ParseMultipartForm(1024)
208
 
        if err == nil {
209
 
                t.Errorf("expected an error from ParseMultipartForm after call to MultipartReader")
 
248
// Test that FormValue invokes ParseMultipartForm.
 
249
func TestFormValueCallsParseMultipartForm(t *testing.T) {
 
250
        req, _ := NewRequest("POST", "http://www.google.com/", strings.NewReader("z=post"))
 
251
        req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
 
252
        if req.Form != nil {
 
253
                t.Fatal("Unexpected request Form, want nil")
 
254
        }
 
255
        req.FormValue("z")
 
256
        if req.Form == nil {
 
257
                t.Fatal("ParseMultipartForm not called by FormValue")
 
258
        }
 
259
}
 
260
 
 
261
// Test that FormFile invokes ParseMultipartForm.
 
262
func TestFormFileCallsParseMultipartForm(t *testing.T) {
 
263
        req := newTestMultipartRequest(t)
 
264
        if req.Form != nil {
 
265
                t.Fatal("Unexpected request Form, want nil")
 
266
        }
 
267
        req.FormFile("")
 
268
        if req.Form == nil {
 
269
                t.Fatal("ParseMultipartForm not called by FormFile")
 
270
        }
 
271
}
 
272
 
 
273
// Test that ParseMultipartForm errors if called
 
274
// after MultipartReader on the same request.
 
275
func TestParseMultipartFormOrder(t *testing.T) {
 
276
        req := newTestMultipartRequest(t)
 
277
        if _, err := req.MultipartReader(); err != nil {
 
278
                t.Fatalf("MultipartReader: %v", err)
 
279
        }
 
280
        if err := req.ParseMultipartForm(1024); err == nil {
 
281
                t.Fatal("expected an error from ParseMultipartForm after call to MultipartReader")
 
282
        }
 
283
}
 
284
 
 
285
// Test that MultipartReader errors if called
 
286
// after ParseMultipartForm on the same request.
 
287
func TestMultipartReaderOrder(t *testing.T) {
 
288
        req := newTestMultipartRequest(t)
 
289
        if err := req.ParseMultipartForm(25); err != nil {
 
290
                t.Fatalf("ParseMultipartForm: %v", err)
 
291
        }
 
292
        defer req.MultipartForm.RemoveAll()
 
293
        if _, err := req.MultipartReader(); err == nil {
 
294
                t.Fatal("expected an error from MultipartReader after call to ParseMultipartForm")
 
295
        }
 
296
}
 
297
 
 
298
// Test that FormFile errors if called after
 
299
// MultipartReader on the same request.
 
300
func TestFormFileOrder(t *testing.T) {
 
301
        req := newTestMultipartRequest(t)
 
302
        if _, err := req.MultipartReader(); err != nil {
 
303
                t.Fatalf("MultipartReader: %v", err)
 
304
        }
 
305
        if _, _, err := req.FormFile(""); err == nil {
 
306
                t.Fatal("expected an error from FormFile after call to MultipartReader")
210
307
        }
211
308
}
212
309
 
343
440
}
344
441
 
345
442
func newTestMultipartRequest(t *testing.T) *Request {
346
 
        b := bytes.NewBufferString(strings.Replace(message, "\n", "\r\n", -1))
 
443
        b := strings.NewReader(strings.Replace(message, "\n", "\r\n", -1))
347
444
        req, err := NewRequest("POST", "/", b)
348
445
        if err != nil {
349
446
                t.Fatal("NewRequest:", err)