OSDN Git Service

55ca745d58c34fddc84478c311ccd0bdd8619cff
[pf3gnuchains/gcc-fork.git] / libgo / go / http / requestwrite_test.go
1 // Copyright 2010 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.
4
5 package http
6
7 import (
8         "bytes"
9         "testing"
10 )
11
12 type reqWriteTest struct {
13         Req Request
14         Raw string
15 }
16
17 var reqWriteTests = []reqWriteTest{
18         // HTTP/1.1 => chunked coding; no body; no trailer
19         {
20                 Request{
21                         Method: "GET",
22                         RawURL: "http://www.techcrunch.com/",
23                         URL: &URL{
24                                 Raw:          "http://www.techcrunch.com/",
25                                 Scheme:       "http",
26                                 RawPath:      "http://www.techcrunch.com/",
27                                 RawAuthority: "www.techcrunch.com",
28                                 RawUserinfo:  "",
29                                 Host:         "www.techcrunch.com",
30                                 Path:         "/",
31                                 RawQuery:     "",
32                                 Fragment:     "",
33                         },
34                         Proto:      "HTTP/1.1",
35                         ProtoMajor: 1,
36                         ProtoMinor: 1,
37                         Header: Header{
38                                 "Accept":           {"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"},
39                                 "Accept-Charset":   {"ISO-8859-1,utf-8;q=0.7,*;q=0.7"},
40                                 "Accept-Encoding":  {"gzip,deflate"},
41                                 "Accept-Language":  {"en-us,en;q=0.5"},
42                                 "Keep-Alive":       {"300"},
43                                 "Proxy-Connection": {"keep-alive"},
44                         },
45                         Body:      nil,
46                         Close:     false,
47                         Host:      "www.techcrunch.com",
48                         Referer:   "",
49                         UserAgent: "Fake",
50                         Form:      map[string][]string{},
51                 },
52
53                 "GET http://www.techcrunch.com/ HTTP/1.1\r\n" +
54                         "Host: www.techcrunch.com\r\n" +
55                         "User-Agent: Fake\r\n" +
56                         "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
57                         "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" +
58                         "Accept-Encoding: gzip,deflate\r\n" +
59                         "Accept-Language: en-us,en;q=0.5\r\n" +
60                         "Keep-Alive: 300\r\n" +
61                         "Proxy-Connection: keep-alive\r\n\r\n",
62         },
63         // HTTP/1.1 => chunked coding; body; empty trailer
64         {
65                 Request{
66                         Method: "GET",
67                         URL: &URL{
68                                 Scheme: "http",
69                                 Host:   "www.google.com",
70                                 Path:   "/search",
71                         },
72                         ProtoMajor:       1,
73                         ProtoMinor:       1,
74                         Header:           map[string][]string{},
75                         Body:             nopCloser{bytes.NewBufferString("abcdef")},
76                         TransferEncoding: []string{"chunked"},
77                 },
78
79                 "GET /search HTTP/1.1\r\n" +
80                         "Host: www.google.com\r\n" +
81                         "User-Agent: Go http package\r\n" +
82                         "Transfer-Encoding: chunked\r\n\r\n" +
83                         "6\r\nabcdef\r\n0\r\n\r\n",
84         },
85         // HTTP/1.1 POST => chunked coding; body; empty trailer
86         {
87                 Request{
88                         Method: "POST",
89                         URL: &URL{
90                                 Scheme: "http",
91                                 Host:   "www.google.com",
92                                 Path:   "/search",
93                         },
94                         ProtoMajor:       1,
95                         ProtoMinor:       1,
96                         Header:           map[string][]string{},
97                         Close:            true,
98                         Body:             nopCloser{bytes.NewBufferString("abcdef")},
99                         TransferEncoding: []string{"chunked"},
100                 },
101
102                 "POST /search HTTP/1.1\r\n" +
103                         "Host: www.google.com\r\n" +
104                         "User-Agent: Go http package\r\n" +
105                         "Connection: close\r\n" +
106                         "Transfer-Encoding: chunked\r\n\r\n" +
107                         "6\r\nabcdef\r\n0\r\n\r\n",
108         },
109         // default to HTTP/1.1
110         {
111                 Request{
112                         Method: "GET",
113                         RawURL: "/search",
114                         Host:   "www.google.com",
115                 },
116
117                 "GET /search HTTP/1.1\r\n" +
118                         "Host: www.google.com\r\n" +
119                         "User-Agent: Go http package\r\n" +
120                         "\r\n",
121         },
122 }
123
124 func TestRequestWrite(t *testing.T) {
125         for i := range reqWriteTests {
126                 tt := &reqWriteTests[i]
127                 var braw bytes.Buffer
128                 err := tt.Req.Write(&braw)
129                 if err != nil {
130                         t.Errorf("error writing #%d: %s", i, err)
131                         continue
132                 }
133                 sraw := braw.String()
134                 if sraw != tt.Raw {
135                         t.Errorf("Test %d, expecting:\n%s\nGot:\n%s\n", i, tt.Raw, sraw)
136                         continue
137                 }
138         }
139 }