summaryrefslogtreecommitdiff
path: root/src/pkg/net/http/cgi/child_test.go
blob: 075d8411bcf2e1a6a8e72500ca8405b90dbf82f6 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2011 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.

// Tests for CGI (the child process perspective)

package cgi

import (
	"testing"
)

func TestRequest(t *testing.T) {
	env := map[string]string{
		"SERVER_PROTOCOL": "HTTP/1.1",
		"REQUEST_METHOD":  "GET",
		"HTTP_HOST":       "example.com",
		"HTTP_REFERER":    "elsewhere",
		"HTTP_USER_AGENT": "goclient",
		"HTTP_FOO_BAR":    "baz",
		"REQUEST_URI":     "/path?a=b",
		"CONTENT_LENGTH":  "123",
		"CONTENT_TYPE":    "text/xml",
		"REMOTE_ADDR":     "5.6.7.8",
	}
	req, err := RequestFromMap(env)
	if err != nil {
		t.Fatalf("RequestFromMap: %v", err)
	}
	if g, e := req.UserAgent(), "goclient"; e != g {
		t.Errorf("expected UserAgent %q; got %q", e, g)
	}
	if g, e := req.Method, "GET"; e != g {
		t.Errorf("expected Method %q; got %q", e, g)
	}
	if g, e := req.Header.Get("Content-Type"), "text/xml"; e != g {
		t.Errorf("expected Content-Type %q; got %q", e, g)
	}
	if g, e := req.ContentLength, int64(123); e != g {
		t.Errorf("expected ContentLength %d; got %d", e, g)
	}
	if g, e := req.Referer(), "elsewhere"; e != g {
		t.Errorf("expected Referer %q; got %q", e, g)
	}
	if req.Header == nil {
		t.Fatalf("unexpected nil Header")
	}
	if g, e := req.Header.Get("Foo-Bar"), "baz"; e != g {
		t.Errorf("expected Foo-Bar %q; got %q", e, g)
	}
	if g, e := req.URL.String(), "http://example.com/path?a=b"; e != g {
		t.Errorf("expected URL %q; got %q", e, g)
	}
	if g, e := req.FormValue("a"), "b"; e != g {
		t.Errorf("expected FormValue(a) %q; got %q", e, g)
	}
	if req.Trailer == nil {
		t.Errorf("unexpected nil Trailer")
	}
	if req.TLS != nil {
		t.Errorf("expected nil TLS")
	}
	if e, g := "5.6.7.8:0", req.RemoteAddr; e != g {
		t.Errorf("RemoteAddr: got %q; want %q", g, e)
	}
}

func TestRequestWithTLS(t *testing.T) {
	env := map[string]string{
		"SERVER_PROTOCOL": "HTTP/1.1",
		"REQUEST_METHOD":  "GET",
		"HTTP_HOST":       "example.com",
		"HTTP_REFERER":    "elsewhere",
		"REQUEST_URI":     "/path?a=b",
		"CONTENT_TYPE":    "text/xml",
		"HTTPS":           "1",
		"REMOTE_ADDR":     "5.6.7.8",
	}
	req, err := RequestFromMap(env)
	if err != nil {
		t.Fatalf("RequestFromMap: %v", err)
	}
	if g, e := req.URL.String(), "https://example.com/path?a=b"; e != g {
		t.Errorf("expected URL %q; got %q", e, g)
	}
	if req.TLS == nil {
		t.Errorf("expected non-nil TLS")
	}
}

func TestRequestWithoutHost(t *testing.T) {
	env := map[string]string{
		"SERVER_PROTOCOL": "HTTP/1.1",
		"HTTP_HOST":       "",
		"REQUEST_METHOD":  "GET",
		"REQUEST_URI":     "/path?a=b",
		"CONTENT_LENGTH":  "123",
	}
	req, err := RequestFromMap(env)
	if err != nil {
		t.Fatalf("RequestFromMap: %v", err)
	}
	if req.URL == nil {
		t.Fatalf("unexpected nil URL")
	}
	if g, e := req.URL.String(), "/path?a=b"; e != g {
		t.Errorf("URL = %q; want %q", g, e)
	}
}

func TestRequestWithoutRequestURI(t *testing.T) {
	env := map[string]string{
		"SERVER_PROTOCOL": "HTTP/1.1",
		"HTTP_HOST":       "example.com",
		"REQUEST_METHOD":  "GET",
		"SCRIPT_NAME":     "/dir/scriptname",
		"PATH_INFO":       "/p1/p2",
		"QUERY_STRING":    "a=1&b=2",
		"CONTENT_LENGTH":  "123",
	}
	req, err := RequestFromMap(env)
	if err != nil {
		t.Fatalf("RequestFromMap: %v", err)
	}
	if req.URL == nil {
		t.Fatalf("unexpected nil URL")
	}
	if g, e := req.URL.String(), "http://example.com/dir/scriptname/p1/p2?a=1&b=2"; e != g {
		t.Errorf("URL = %q; want %q", g, e)
	}
}