diff options
author | Ondřej Surý <ondrej@sury.org> | 2011-04-20 15:44:41 +0200 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2011-04-20 15:44:41 +0200 |
commit | 50104cc32a498f7517a51c8dc93106c51c7a54b4 (patch) | |
tree | 47af80be259cc7c45d0eaec7d42e61fa38c8e4fb /src/pkg/http/httptest | |
parent | c072558b90f1bbedc2022b0f30c8b1ac4712538e (diff) | |
download | golang-upstream/2011.03.07.1.tar.gz |
Imported Upstream version 2011.03.07.1upstream/2011.03.07.1
Diffstat (limited to 'src/pkg/http/httptest')
-rw-r--r-- | src/pkg/http/httptest/Makefile | 12 | ||||
-rw-r--r-- | src/pkg/http/httptest/recorder.go | 79 | ||||
-rw-r--r-- | src/pkg/http/httptest/server.go | 42 |
3 files changed, 133 insertions, 0 deletions
diff --git a/src/pkg/http/httptest/Makefile b/src/pkg/http/httptest/Makefile new file mode 100644 index 000000000..eb35d8aec --- /dev/null +++ b/src/pkg/http/httptest/Makefile @@ -0,0 +1,12 @@ +# Copyright 2011 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. + +include ../../../Make.inc + +TARG=http/httptest +GOFILES=\ + recorder.go\ + server.go\ + +include ../../../Make.pkg diff --git a/src/pkg/http/httptest/recorder.go b/src/pkg/http/httptest/recorder.go new file mode 100644 index 000000000..ec7bde8aa --- /dev/null +++ b/src/pkg/http/httptest/recorder.go @@ -0,0 +1,79 @@ +// Copyright 2011 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. + +// The httptest package provides utilities for HTTP testing. +package httptest + +import ( + "bytes" + "http" + "os" +) + +// ResponseRecorder is an implementation of http.ResponseWriter that +// records its mutations for later inspection in tests. +type ResponseRecorder struct { + Code int // the HTTP response code from WriteHeader + Header http.Header // if non-nil, the headers to populate + Body *bytes.Buffer // if non-nil, the bytes.Buffer to append written data to + Flushed bool + + FakeRemoteAddr string // the fake RemoteAddr to return, or "" for DefaultRemoteAddr + FakeUsingTLS bool // whether to return true from the UsingTLS method +} + +// NewRecorder returns an initialized ResponseRecorder. +func NewRecorder() *ResponseRecorder { + return &ResponseRecorder{ + Header: http.Header(make(map[string][]string)), + Body: new(bytes.Buffer), + } +} + +// DefaultRemoteAddr is the default remote address to return in RemoteAddr if +// an explicit DefaultRemoteAddr isn't set on ResponseRecorder. +const DefaultRemoteAddr = "1.2.3.4" + +// RemoteAddr returns the value of rw.FakeRemoteAddr, if set, else +// returns DefaultRemoteAddr. +func (rw *ResponseRecorder) RemoteAddr() string { + if rw.FakeRemoteAddr != "" { + return rw.FakeRemoteAddr + } + return DefaultRemoteAddr +} + +// UsingTLS returns the fake value in rw.FakeUsingTLS +func (rw *ResponseRecorder) UsingTLS() bool { + return rw.FakeUsingTLS +} + +// SetHeader populates rw.Header, if non-nil. +func (rw *ResponseRecorder) SetHeader(k, v string) { + if rw.Header != nil { + if v == "" { + rw.Header.Del(k) + } else { + rw.Header.Set(k, v) + } + } +} + +// Write always succeeds and writes to rw.Body, if not nil. +func (rw *ResponseRecorder) Write(buf []byte) (int, os.Error) { + if rw.Body != nil { + rw.Body.Write(buf) + } + return len(buf), nil +} + +// WriteHeader sets rw.Code. +func (rw *ResponseRecorder) WriteHeader(code int) { + rw.Code = code +} + +// Flush sets rw.Flushed to true. +func (rw *ResponseRecorder) Flush() { + rw.Flushed = true +} diff --git a/src/pkg/http/httptest/server.go b/src/pkg/http/httptest/server.go new file mode 100644 index 000000000..86c9eb435 --- /dev/null +++ b/src/pkg/http/httptest/server.go @@ -0,0 +1,42 @@ +// Copyright 2011 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. + +// Implementation of Server + +package httptest + +import ( + "fmt" + "http" + "net" +) + +// A Server is an HTTP server listening on a system-chosen port on the +// local loopback interface, for use in end-to-end HTTP tests. +type Server struct { + URL string // base URL of form http://ipaddr:port with no trailing slash + Listener net.Listener +} + +// NewServer starts and returns a new Server. +// The caller should call Close when finished, to shut it down. +func NewServer(handler http.Handler) *Server { + ts := new(Server) + l, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + if l, err = net.Listen("tcp6", "[::1]:0"); err != nil { + panic(fmt.Sprintf("httptest: failed to listen on a port: %v", err)) + } + } + ts.Listener = l + ts.URL = "http://" + l.Addr().String() + server := &http.Server{Handler: handler} + go server.Serve(l) + return ts +} + +// Close shuts down the server. +func (s *Server) Close() { + s.Listener.Close() +} |