From dd5f6ae3ff81db61370b4d13fd03ae46b0bc23da Mon Sep 17 00:00:00 2001 From: David Symonds Date: Fri, 19 Jun 2009 18:02:15 -0700 Subject: Add form body parsing to http.Request. better error handling throughout. R=r,rsc APPROVED=r DELTA=254 (201 added, 3 deleted, 50 changed) OCL=30515 CL=30545 --- src/pkg/http/request_test.go | 62 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/pkg/http/request_test.go (limited to 'src/pkg/http/request_test.go') diff --git a/src/pkg/http/request_test.go b/src/pkg/http/request_test.go new file mode 100644 index 000000000..ab611a0ca --- /dev/null +++ b/src/pkg/http/request_test.go @@ -0,0 +1,62 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package http + +import ( + "fmt"; + "http"; + "testing"; +) + +type stringMultimap map[string] []string + +type parseTest struct { + body string; + out stringMultimap; +} + +var parseTests = []parseTest{ + parseTest{ + body: "a=1&b=2", + out: stringMultimap{ "a": []string{ "1" }, "b": []string{ "2" } }, + }, + parseTest{ + body: "a=1&a=2&a=banana", + out: stringMultimap{ "a": []string{ "1", "2", "banana" } }, + }, + parseTest{ + body: "ascii=%3Ckey%3A+0x90%3E", + out: stringMultimap{ "ascii": []string{ "" } }, + }, +} + +func TestParseForm(t *testing.T) { + for i, test := range parseTests { + data, err := parseForm(test.body); + if err != nil { + t.Errorf("test %d: Unexpected error: %v", i, err); + continue + } + if dlen, olen := len(data), len(test.out); dlen != olen { + t.Errorf("test %d: Have %d keys, want %d keys", i, dlen, olen); + } + for k, vs := range(test.out) { + vec, ok := data[k]; + if !ok { + t.Errorf("test %d: Missing key %q", i, k); + continue + } + if dlen, olen := vec.Len(), len(vs); dlen != olen { + t.Errorf("test %d: key %q: Have %d keys, want %d keys", i, k, dlen, olen); + continue + } + for j, v := range(vs) { + if dv := vec.At(j); dv != v { + t.Errorf("test %d: key %q: val %d: Have %q, want %q", i, k, j, dv, v); + } + } + } + } +} -- cgit v1.2.3