summaryrefslogtreecommitdiff
path: root/src/pkg/http/request_test.go
blob: 1cd91717db23813b00b0f5caad01186459db215e (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
63
64
65
66
67
68
69
70
71
72
// 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";
	"os";
	"testing";
)

type stringMultimap map[string] []string

type parseTest struct {
	query string;
	out stringMultimap;
}

var parseTests = []parseTest{
	parseTest{
		query: "a=1&b=2",
		out: stringMultimap{ "a": []string{ "1" }, "b": []string{ "2" } },
	},
	parseTest{
		query: "a=1&a=2&a=banana",
		out: stringMultimap{ "a": []string{ "1", "2", "banana" } },
	},
	parseTest{
		query: "ascii=%3Ckey%3A+0x90%3E",
		out: stringMultimap{ "ascii": []string{ "<key: 0x90>" } },
	},
}

func TestParseForm(t *testing.T) {
	for i, test := range parseTests {
		form, err := parseForm(test.query);
		if err != nil {
			t.Errorf("test %d: Unexpected error: %v", i, err);
			continue
		}
		if len(form) != len(test.out) {
			t.Errorf("test %d: len(form) = %d, want %d", i, len(form), len(test.out));
		}
		for k, evs := range test.out {
			vs, ok := form[k];
			if !ok {
				t.Errorf("test %d: Missing key %q", i, k);
				continue
			}
			if len(vs) != len(evs) {
				t.Errorf("test %d: len(form[%q]) = %d, want %d", i, k, len(vs), len(evs));
				continue
			}
			for j, ev := range evs {
				if v := vs[j]; v != ev {
					t.Errorf("test %d: form[%q][%d] = %q, want %q", i, k, j, v, ev);
				}
			}
		}
	}
}

func TestQuery(t *testing.T) {
	var err os.Error;
	req := &Request{ Method: "GET" };
	req.Url, err = ParseURL("http://www.google.com/search?q=foo&q=bar");
	if q := req.FormValue("q"); q != "foo" {
		t.Errorf(`req.FormValue("q") = %q, want "foo"`, q);
	}
}