summaryrefslogtreecommitdiff
path: root/libgo/go/http/serve_test.go
blob: 053d6dca448ec9697400938d56901d643b7bfef5 (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
132
133
134
135
136
137
138
139
140
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
170
171
172
173
174
175
176
177
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// 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.

// End-to-end serving tests

package http

import (
	"bufio"
	"bytes"
	"io"
	"os"
	"net"
	"testing"
)

type dummyAddr string
type oneConnListener struct {
	conn net.Conn
}

func (l *oneConnListener) Accept() (c net.Conn, err os.Error) {
	c = l.conn
	if c == nil {
		err = os.EOF
		return
	}
	err = nil
	l.conn = nil
	return
}

func (l *oneConnListener) Close() os.Error {
	return nil
}

func (l *oneConnListener) Addr() net.Addr {
	return dummyAddr("test-address")
}

func (a dummyAddr) Network() string {
	return string(a)
}

func (a dummyAddr) String() string {
	return string(a)
}

type testConn struct {
	readBuf  bytes.Buffer
	writeBuf bytes.Buffer
}

func (c *testConn) Read(b []byte) (int, os.Error) {
	return c.readBuf.Read(b)
}

func (c *testConn) Write(b []byte) (int, os.Error) {
	return c.writeBuf.Write(b)
}

func (c *testConn) Close() os.Error {
	return nil
}

func (c *testConn) LocalAddr() net.Addr {
	return dummyAddr("local-addr")
}

func (c *testConn) RemoteAddr() net.Addr {
	return dummyAddr("remote-addr")
}

func (c *testConn) SetTimeout(nsec int64) os.Error {
	return nil
}

func (c *testConn) SetReadTimeout(nsec int64) os.Error {
	return nil
}

func (c *testConn) SetWriteTimeout(nsec int64) os.Error {
	return nil
}

func TestConsumingBodyOnNextConn(t *testing.T) {
	conn := new(testConn)
	for i := 0; i < 2; i++ {
		conn.readBuf.Write([]byte(
			"POST / HTTP/1.1\r\n" +
				"Host: test\r\n" +
				"Content-Length: 11\r\n" +
				"\r\n" +
				"foo=1&bar=1"))
	}

	reqNum := 0
	ch := make(chan *Request)
	servech := make(chan os.Error)
	listener := &oneConnListener{conn}
	handler := func(res ResponseWriter, req *Request) {
		reqNum++
		t.Logf("Got request #%d: %v", reqNum, req)
		ch <- req
	}

	go func() {
		servech <- Serve(listener, HandlerFunc(handler))
	}()

	var req *Request
	t.Log("Waiting for first request.")
	req = <-ch
	if req == nil {
		t.Fatal("Got nil first request.")
	}
	if req.Method != "POST" {
		t.Errorf("For request #1's method, got %q; expected %q",
			req.Method, "POST")
	}

	t.Log("Waiting for second request.")
	req = <-ch
	if req == nil {
		t.Fatal("Got nil first request.")
	}
	if req.Method != "POST" {
		t.Errorf("For request #2's method, got %q; expected %q",
			req.Method, "POST")
	}

	t.Log("Waiting for EOF.")
	if serveerr := <-servech; serveerr != os.EOF {
		t.Errorf("Serve returned %q; expected EOF", serveerr)
	}
}

type responseWriterMethodCall struct {
	method                 string
	headerKey, headerValue string // if method == "SetHeader"
	bytesWritten           []byte // if method == "Write"
	responseCode           int    // if method == "WriteHeader"
}

type recordingResponseWriter struct {
	log []*responseWriterMethodCall
}

func (rw *recordingResponseWriter) RemoteAddr() string {
	return "1.2.3.4"
}

func (rw *recordingResponseWriter) UsingTLS() bool {
	return false
}

func (rw *recordingResponseWriter) SetHeader(k, v string) {
	rw.log = append(rw.log, &responseWriterMethodCall{method: "SetHeader", headerKey: k, headerValue: v})
}

func (rw *recordingResponseWriter) Write(buf []byte) (int, os.Error) {
	rw.log = append(rw.log, &responseWriterMethodCall{method: "Write", bytesWritten: buf})
	return len(buf), nil
}

func (rw *recordingResponseWriter) WriteHeader(code int) {
	rw.log = append(rw.log, &responseWriterMethodCall{method: "WriteHeader", responseCode: code})
}

func (rw *recordingResponseWriter) Flush() {
	rw.log = append(rw.log, &responseWriterMethodCall{method: "Flush"})
}

func (rw *recordingResponseWriter) Hijack() (io.ReadWriteCloser, *bufio.ReadWriter, os.Error) {
	panic("Not supported")
}

// Tests for http://code.google.com/p/go/issues/detail?id=900
func TestMuxRedirectLeadingSlashes(t *testing.T) {
	paths := []string{"//foo.txt", "///foo.txt", "/../../foo.txt"}
	for _, path := range paths {
		req, err := ReadRequest(bufio.NewReader(bytes.NewBufferString("GET " + path + " HTTP/1.1\r\nHost: test\r\n\r\n")))
		if err != nil {
			t.Errorf("%s", err)
		}
		mux := NewServeMux()
		resp := new(recordingResponseWriter)
		resp.log = make([]*responseWriterMethodCall, 0)

		mux.ServeHTTP(resp, req)

		dumpLog := func() {
			t.Logf("For path %q:", path)
			for _, call := range resp.log {
				t.Logf("Got call: %s, header=%s, value=%s, buf=%q, code=%d", call.method,
					call.headerKey, call.headerValue, call.bytesWritten, call.responseCode)
			}
		}

		if len(resp.log) != 2 {
			dumpLog()
			t.Errorf("expected 2 calls to response writer; got %d", len(resp.log))
			return
		}

		if resp.log[0].method != "SetHeader" ||
			resp.log[0].headerKey != "Location" || resp.log[0].headerValue != "/foo.txt" {
			dumpLog()
			t.Errorf("Expected SetHeader of Location to /foo.txt")
			return
		}

		if resp.log[1].method != "WriteHeader" || resp.log[1].responseCode != StatusMovedPermanently {
			dumpLog()
			t.Errorf("Expected WriteHeader of StatusMovedPermanently")
			return
		}
	}
}