requestwrite_test.go 8.84 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2010 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 (
	"bytes"
9
	"fmt"
10
	"io"
Brad Fitzpatrick's avatar
Brad Fitzpatrick committed
11
	"io/ioutil"
12 13
	"os"
	"strings"
14 15 16 17
	"testing"
)

type reqWriteTest struct {
Russ Cox's avatar
Russ Cox committed
18
	Req      Request
19
	Body     interface{} // optional []byte or func() io.ReadCloser to populate Req.Body
Russ Cox's avatar
Russ Cox committed
20 21
	Raw      string
	RawProxy string
22 23 24 25
}

var reqWriteTests = []reqWriteTest{
	// HTTP/1.1 => chunked coding; no body; no trailer
Robert Griesemer's avatar
Robert Griesemer committed
26
	{
27 28 29 30
		Request{
			Method: "GET",
			RawURL: "http://www.techcrunch.com/",
			URL: &URL{
31 32 33 34 35 36 37 38 39
				Raw:          "http://www.techcrunch.com/",
				Scheme:       "http",
				RawPath:      "http://www.techcrunch.com/",
				RawAuthority: "www.techcrunch.com",
				RawUserinfo:  "",
				Host:         "www.techcrunch.com",
				Path:         "/",
				RawQuery:     "",
				Fragment:     "",
40
			},
41
			Proto:      "HTTP/1.1",
42 43
			ProtoMajor: 1,
			ProtoMinor: 1,
44 45 46 47 48 49 50
			Header: Header{
				"Accept":           {"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"},
				"Accept-Charset":   {"ISO-8859-1,utf-8;q=0.7,*;q=0.7"},
				"Accept-Encoding":  {"gzip,deflate"},
				"Accept-Language":  {"en-us,en;q=0.5"},
				"Keep-Alive":       {"300"},
				"Proxy-Connection": {"keep-alive"},
51
				"User-Agent":       {"Fake"},
52
			},
53 54 55 56
			Body:  nil,
			Close: false,
			Host:  "www.techcrunch.com",
			Form:  map[string][]string{},
57 58
		},

Russ Cox's avatar
Russ Cox committed
59 60
		nil,

61
		"GET http://www.techcrunch.com/ HTTP/1.1\r\n" +
62 63
			"Host: www.techcrunch.com\r\n" +
			"User-Agent: Fake\r\n" +
64
			"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
65 66 67 68 69
			"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" +
			"Accept-Encoding: gzip,deflate\r\n" +
			"Accept-Language: en-us,en;q=0.5\r\n" +
			"Keep-Alive: 300\r\n" +
			"Proxy-Connection: keep-alive\r\n\r\n",
Russ Cox's avatar
Russ Cox committed
70 71

		"GET http://www.techcrunch.com/ HTTP/1.1\r\n" +
72
			"Host: www.techcrunch.com\r\n" +
Russ Cox's avatar
Russ Cox committed
73 74 75 76 77 78 79
			"User-Agent: Fake\r\n" +
			"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
			"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" +
			"Accept-Encoding: gzip,deflate\r\n" +
			"Accept-Language: en-us,en;q=0.5\r\n" +
			"Keep-Alive: 300\r\n" +
			"Proxy-Connection: keep-alive\r\n\r\n",
80 81
	},
	// HTTP/1.1 => chunked coding; body; empty trailer
Robert Griesemer's avatar
Robert Griesemer committed
82
	{
83 84 85 86
		Request{
			Method: "GET",
			URL: &URL{
				Scheme: "http",
87 88
				Host:   "www.google.com",
				Path:   "/search",
89
			},
90 91
			ProtoMajor:       1,
			ProtoMinor:       1,
92
			Header:           Header{},
93
			TransferEncoding: []string{"chunked"},
94 95
		},

Russ Cox's avatar
Russ Cox committed
96 97
		[]byte("abcdef"),

98 99 100 101
		"GET /search HTTP/1.1\r\n" +
			"Host: www.google.com\r\n" +
			"User-Agent: Go http package\r\n" +
			"Transfer-Encoding: chunked\r\n\r\n" +
102
			chunk("abcdef") + chunk(""),
Russ Cox's avatar
Russ Cox committed
103 104

		"GET http://www.google.com/search HTTP/1.1\r\n" +
105
			"Host: www.google.com\r\n" +
Russ Cox's avatar
Russ Cox committed
106 107
			"User-Agent: Go http package\r\n" +
			"Transfer-Encoding: chunked\r\n\r\n" +
108
			chunk("abcdef") + chunk(""),
109
	},
110
	// HTTP/1.1 POST => chunked coding; body; empty trailer
Robert Griesemer's avatar
Robert Griesemer committed
111
	{
112 113 114 115
		Request{
			Method: "POST",
			URL: &URL{
				Scheme: "http",
116 117
				Host:   "www.google.com",
				Path:   "/search",
118
			},
119 120
			ProtoMajor:       1,
			ProtoMinor:       1,
121
			Header:           Header{},
122
			Close:            true,
123 124 125
			TransferEncoding: []string{"chunked"},
		},

Russ Cox's avatar
Russ Cox committed
126 127
		[]byte("abcdef"),

128 129 130 131 132
		"POST /search HTTP/1.1\r\n" +
			"Host: www.google.com\r\n" +
			"User-Agent: Go http package\r\n" +
			"Connection: close\r\n" +
			"Transfer-Encoding: chunked\r\n\r\n" +
133
			chunk("abcdef") + chunk(""),
Russ Cox's avatar
Russ Cox committed
134 135

		"POST http://www.google.com/search HTTP/1.1\r\n" +
136
			"Host: www.google.com\r\n" +
Russ Cox's avatar
Russ Cox committed
137 138 139
			"User-Agent: Go http package\r\n" +
			"Connection: close\r\n" +
			"Transfer-Encoding: chunked\r\n\r\n" +
140
			chunk("abcdef") + chunk(""),
141
	},
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

	// HTTP/1.1 POST with Content-Length, no chunking
	{
		Request{
			Method: "POST",
			URL: &URL{
				Scheme: "http",
				Host:   "www.google.com",
				Path:   "/search",
			},
			ProtoMajor:    1,
			ProtoMinor:    1,
			Header:        Header{},
			Close:         true,
			ContentLength: 6,
		},

		[]byte("abcdef"),

		"POST /search HTTP/1.1\r\n" +
			"Host: www.google.com\r\n" +
			"User-Agent: Go http package\r\n" +
			"Connection: close\r\n" +
			"Content-Length: 6\r\n" +
			"\r\n" +
			"abcdef",

		"POST http://www.google.com/search HTTP/1.1\r\n" +
170
			"Host: www.google.com\r\n" +
171 172 173 174 175 176 177
			"User-Agent: Go http package\r\n" +
			"Connection: close\r\n" +
			"Content-Length: 6\r\n" +
			"\r\n" +
			"abcdef",
	},

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	// HTTP/1.1 POST with Content-Length in headers
	{
		Request{
			Method: "POST",
			RawURL: "http://example.com/",
			Host:   "example.com",
			Header: Header{
				"Content-Length": []string{"10"}, // ignored
			},
			ContentLength: 6,
		},

		[]byte("abcdef"),

		"POST http://example.com/ HTTP/1.1\r\n" +
			"Host: example.com\r\n" +
			"User-Agent: Go http package\r\n" +
			"Content-Length: 6\r\n" +
			"\r\n" +
			"abcdef",

		"POST http://example.com/ HTTP/1.1\r\n" +
			"Host: example.com\r\n" +
			"User-Agent: Go http package\r\n" +
			"Content-Length: 6\r\n" +
			"\r\n" +
			"abcdef",
	},

207
	// default to HTTP/1.1
Robert Griesemer's avatar
Robert Griesemer committed
208
	{
209 210 211
		Request{
			Method: "GET",
			RawURL: "/search",
212
			Host:   "www.google.com",
213 214
		},

Russ Cox's avatar
Russ Cox committed
215 216
		nil,

217 218 219 220
		"GET /search HTTP/1.1\r\n" +
			"Host: www.google.com\r\n" +
			"User-Agent: Go http package\r\n" +
			"\r\n",
Russ Cox's avatar
Russ Cox committed
221 222 223

		// Looks weird but RawURL overrides what WriteProxy would choose.
		"GET /search HTTP/1.1\r\n" +
224
			"Host: www.google.com\r\n" +
Russ Cox's avatar
Russ Cox committed
225 226
			"User-Agent: Go http package\r\n" +
			"\r\n",
227
	},
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

	// Request with a 0 ContentLength and a 0 byte body.
	{
		Request{
			Method:        "POST",
			RawURL:        "/",
			Host:          "example.com",
			ProtoMajor:    1,
			ProtoMinor:    1,
			ContentLength: 0, // as if unset by user
		},

		func() io.ReadCloser { return ioutil.NopCloser(io.LimitReader(strings.NewReader("xx"), 0)) },

		"POST / HTTP/1.1\r\n" +
			"Host: example.com\r\n" +
			"User-Agent: Go http package\r\n" +
			"\r\n",

		"POST / HTTP/1.1\r\n" +
			"Host: example.com\r\n" +
			"User-Agent: Go http package\r\n" +
			"\r\n",
	},

	// Request with a 0 ContentLength and a 1 byte body.
	{
		Request{
			Method:        "POST",
			RawURL:        "/",
			Host:          "example.com",
			ProtoMajor:    1,
			ProtoMinor:    1,
			ContentLength: 0, // as if unset by user
		},

		func() io.ReadCloser { return ioutil.NopCloser(io.LimitReader(strings.NewReader("xx"), 1)) },

		"POST / HTTP/1.1\r\n" +
			"Host: example.com\r\n" +
			"User-Agent: Go http package\r\n" +
			"Transfer-Encoding: chunked\r\n\r\n" +
			chunk("x") + chunk(""),

		"POST / HTTP/1.1\r\n" +
			"Host: example.com\r\n" +
			"User-Agent: Go http package\r\n" +
			"Transfer-Encoding: chunked\r\n\r\n" +
			chunk("x") + chunk(""),
	},
278 279 280 281 282
}

func TestRequestWrite(t *testing.T) {
	for i := range reqWriteTests {
		tt := &reqWriteTests[i]
283 284 285 286 287 288 289 290 291

		setBody := func() {
			switch b := tt.Body.(type) {
			case []byte:
				tt.Req.Body = ioutil.NopCloser(bytes.NewBuffer(b))
			case func() io.ReadCloser:
				tt.Req.Body = b()
			}
		}
Russ Cox's avatar
Russ Cox committed
292
		if tt.Body != nil {
293
			setBody()
Russ Cox's avatar
Russ Cox committed
294
		}
295 296 297
		if tt.Req.Header == nil {
			tt.Req.Header = make(Header)
		}
298 299 300 301 302 303 304 305 306 307 308
		var braw bytes.Buffer
		err := tt.Req.Write(&braw)
		if err != nil {
			t.Errorf("error writing #%d: %s", i, err)
			continue
		}
		sraw := braw.String()
		if sraw != tt.Raw {
			t.Errorf("Test %d, expecting:\n%s\nGot:\n%s\n", i, tt.Raw, sraw)
			continue
		}
Russ Cox's avatar
Russ Cox committed
309 310

		if tt.Body != nil {
311
			setBody()
Russ Cox's avatar
Russ Cox committed
312 313 314 315 316 317 318 319 320 321 322 323
		}
		var praw bytes.Buffer
		err = tt.Req.WriteProxy(&praw)
		if err != nil {
			t.Errorf("error writing #%d: %s", i, err)
			continue
		}
		sraw = praw.String()
		if sraw != tt.RawProxy {
			t.Errorf("Test Proxy %d, expecting:\n%s\nGot:\n%s\n", i, tt.RawProxy, sraw)
			continue
		}
324 325
	}
}
326 327 328 329 330 331 332 333 334 335 336 337 338

type closeChecker struct {
	io.Reader
	closed bool
}

func (rc *closeChecker) Close() os.Error {
	rc.closed = true
	return nil
}

// TestRequestWriteClosesBody tests that Request.Write does close its request.Body.
// It also indirectly tests NewRequest and that it doesn't wrap an existing Closer
339
// inside a NopCloser, and that it serializes it correctly.
340 341
func TestRequestWriteClosesBody(t *testing.T) {
	rc := &closeChecker{Reader: strings.NewReader("my body")}
342
	req, _ := NewRequest("POST", "http://foo.com/", rc)
343 344
	if req.ContentLength != 0 {
		t.Errorf("got req.ContentLength %d, want 0", req.ContentLength)
345
	}
346 347 348 349 350
	buf := new(bytes.Buffer)
	req.Write(buf)
	if !rc.closed {
		t.Error("body not closed after write")
	}
351 352 353 354 355 356 357 358 359 360 361 362 363
	expected := "POST / HTTP/1.1\r\n" +
		"Host: foo.com\r\n" +
		"User-Agent: Go http package\r\n" +
		"Transfer-Encoding: chunked\r\n\r\n" +
		// TODO: currently we don't buffer before chunking, so we get a
		// single "m" chunk before the other chunks, as this was the 1-byte
		// read from our MultiReader where we stiched the Body back together
		// after sniffing whether the Body was 0 bytes or not.
		chunk("m") +
		chunk("y body") +
		chunk("")
	if buf.String() != expected {
		t.Errorf("write:\n got: %s\nwant: %s", buf.String(), expected)
364 365 366
	}
}

367 368
func chunk(s string) string {
	return fmt.Sprintf("%x\r\n%s\r\n", len(s), s)
369
}