~rogpeppe/goose/state-of-the-world

« back to all changes in this revision

Viewing changes to testing/httpsuite/httpsuite.go

  • Committer: John Arbash Meinel
  • Date: 2012-11-01 10:33:57 UTC
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: john@arbash-meinel.com-20121101103357-5z7k2ihe9dtnt1zf
Introduce testing infrastructure.

I imagine we are going to be using a lot of HTTPSuite tests.
Pull it out into something that we can easily reuse.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package httpsuite
 
2
 
 
3
// This package provides an HTTPSuite infrastructure that lets you bring up an
 
4
// HTTP server. The server will handle requests based on whatever Handlers are
 
5
// attached to HTTPSuite.Mux. This Mux is reset after every test case, and the
 
6
// server is shut down at the end of the test suite.
 
7
 
 
8
import (
 
9
        . "launchpad.net/gocheck"
 
10
        "net/http"
 
11
        "net/http/httptest"
 
12
)
 
13
 
 
14
var _ = Suite(&HTTPSuite{})
 
15
 
 
16
type HTTPSuite struct {
 
17
        Server     *httptest.Server
 
18
        Mux        *http.ServeMux
 
19
        oldHandler http.Handler
 
20
}
 
21
 
 
22
func (s *HTTPSuite) SetUpSuite(c *C) {
 
23
        // fmt.Printf("Starting New Server\n")
 
24
        s.Server = httptest.NewServer(nil)
 
25
}
 
26
 
 
27
func (s *HTTPSuite) SetUpTest(c *C) {
 
28
        s.oldHandler = s.Server.Config.Handler
 
29
        s.Mux = http.NewServeMux()
 
30
        s.Server.Config.Handler = s.Mux
 
31
}
 
32
 
 
33
func (s *HTTPSuite) TearDownTest(c *C) {
 
34
        s.Mux = nil
 
35
        s.Server.Config.Handler = s.oldHandler
 
36
}
 
37
 
 
38
func (s *HTTPSuite) TearDownSuite(c *C) {
 
39
        if s.Server != nil {
 
40
                // fmt.Printf("Stopping Server\n")
 
41
                s.Server.Close()
 
42
        }
 
43
}