1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Tests for CGI (the child process perspective)
13 func TestRequest(t *testing.T) {
14 env := map[string]string{
15 "SERVER_PROTOCOL": "HTTP/1.1",
16 "REQUEST_METHOD": "GET",
17 "HTTP_HOST": "example.com",
18 "HTTP_REFERER": "elsewhere",
19 "HTTP_USER_AGENT": "goclient",
20 "HTTP_FOO_BAR": "baz",
21 "REQUEST_URI": "/path?a=b",
22 "CONTENT_LENGTH": "123",
23 "CONTENT_TYPE": "text/xml",
25 "REMOTE_ADDR": "5.6.7.8",
27 req, err := RequestFromMap(env)
29 t.Fatalf("RequestFromMap: %v", err)
31 if g, e := req.UserAgent, "goclient"; e != g {
32 t.Errorf("expected UserAgent %q; got %q", e, g)
34 if g, e := req.Method, "GET"; e != g {
35 t.Errorf("expected Method %q; got %q", e, g)
37 if g, e := req.Header.Get("User-Agent"), ""; e != g {
38 // Tests that we don't put recognized headers in the map
39 t.Errorf("expected User-Agent %q; got %q", e, g)
41 if g, e := req.Header.Get("Content-Type"), "text/xml"; e != g {
42 t.Errorf("expected Content-Type %q; got %q", e, g)
44 if g, e := req.ContentLength, int64(123); e != g {
45 t.Errorf("expected ContentLength %d; got %d", e, g)
47 if g, e := req.Referer, "elsewhere"; e != g {
48 t.Errorf("expected Referer %q; got %q", e, g)
50 if req.Header == nil {
51 t.Fatalf("unexpected nil Header")
53 if g, e := req.Header.Get("Foo-Bar"), "baz"; e != g {
54 t.Errorf("expected Foo-Bar %q; got %q", e, g)
56 if g, e := req.RawURL, "http://example.com/path?a=b"; e != g {
57 t.Errorf("expected RawURL %q; got %q", e, g)
59 if g, e := req.URL.String(), "http://example.com/path?a=b"; e != g {
60 t.Errorf("expected URL %q; got %q", e, g)
62 if g, e := req.FormValue("a"), "b"; e != g {
63 t.Errorf("expected FormValue(a) %q; got %q", e, g)
65 if req.Trailer == nil {
66 t.Errorf("unexpected nil Trailer")
69 t.Errorf("expected non-nil TLS")
71 if e, g := "5.6.7.8:0", req.RemoteAddr; e != g {
72 t.Errorf("RemoteAddr: got %q; want %q", g, e)
76 func TestRequestWithoutHost(t *testing.T) {
77 env := map[string]string{
78 "SERVER_PROTOCOL": "HTTP/1.1",
80 "REQUEST_METHOD": "GET",
81 "REQUEST_URI": "/path?a=b",
82 "CONTENT_LENGTH": "123",
84 req, err := RequestFromMap(env)
86 t.Fatalf("RequestFromMap: %v", err)
88 if g, e := req.RawURL, "/path?a=b"; e != g {
89 t.Errorf("expected RawURL %q; got %q", e, g)
92 t.Fatalf("unexpected nil URL")
94 if g, e := req.URL.String(), "/path?a=b"; e != g {
95 t.Errorf("expected URL %q; got %q", e, g)