summaryrefslogtreecommitdiff
path: root/src/pkg/http/request_test.go
blob: 230fe7bbb8b39e0f6c2aa481f5b4d752f0aefb35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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{ "<key: 0x90>" } },
	},
}

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);
				}
			}
		}
	}
}