Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / http / requestwrite_test.go
blob3ceabe4ee7a32f8eb280d3622289aa514ef80e6a
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.
5 package http
7 import (
8 "bytes"
9 "testing"
12 type reqWriteTest struct {
13 Req Request
14 Raw string
17 var reqWriteTests = []reqWriteTest{
18 // HTTP/1.1 => chunked coding; no body; no trailer
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: "",
34 Proto: "HTTP/1.1",
35 ProtoMajor: 1,
36 ProtoMinor: 1,
37 Header: map[string]string{
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",
45 Body: nil,
46 Close: false,
47 Host: "www.techcrunch.com",
48 Referer: "",
49 UserAgent: "Fake",
50 Form: map[string][]string{},
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-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" +
57 "Accept-Encoding: gzip,deflate\r\n" +
58 "Accept-Language: en-us,en;q=0.5\r\n" +
59 "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
60 "Keep-Alive: 300\r\n" +
61 "Proxy-Connection: keep-alive\r\n\r\n",
63 // HTTP/1.1 => chunked coding; body; empty trailer
65 Request{
66 Method: "GET",
67 URL: &URL{
68 Scheme: "http",
69 Host: "www.google.com",
70 Path: "/search",
72 ProtoMajor: 1,
73 ProtoMinor: 1,
74 Header: map[string]string{},
75 Body: nopCloser{bytes.NewBufferString("abcdef")},
76 TransferEncoding: []string{"chunked"},
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",
85 // HTTP/1.1 POST => chunked coding; body; empty trailer
87 Request{
88 Method: "POST",
89 URL: &URL{
90 Scheme: "http",
91 Host: "www.google.com",
92 Path: "/search",
94 ProtoMajor: 1,
95 ProtoMinor: 1,
96 Header: map[string]string{},
97 Close: true,
98 Body: nopCloser{bytes.NewBufferString("abcdef")},
99 TransferEncoding: []string{"chunked"},
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",
109 // default to HTTP/1.1
111 Request{
112 Method: "GET",
113 RawURL: "/search",
114 Host: "www.google.com",
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",
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
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