File: httpsuite.go

package info (click to toggle)
golang-github-joyent-gocommon 0.0~git20161202.b787089-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 188 kB
  • sloc: makefile: 2
file content (47 lines) | stat: -rw-r--r-- 1,065 bytes parent folder | download | duplicates (2)
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
package testing

// This package provides an HTTPSuite infrastructure that lets you bring up an
// HTTP server. The server will handle requests based on whatever Handlers are
// attached to HTTPSuite.Mux. This Mux is reset after every test case, and the
// server is shut down at the end of the test suite.

import (
	"github.com/julienschmidt/httprouter"
	gc "launchpad.net/gocheck"
	"net/http"
	"net/http/httptest"
)

var _ = gc.Suite(&HTTPSuite{})

type HTTPSuite struct {
	Server     *httptest.Server
	Mux        *httprouter.Router
	oldHandler http.Handler
	UseTLS     bool
}

func (s *HTTPSuite) SetUpSuite(c *gc.C) {
	if s.UseTLS {
		s.Server = httptest.NewTLSServer(nil)
	} else {
		s.Server = httptest.NewServer(nil)
	}
}

func (s *HTTPSuite) SetUpTest(c *gc.C) {
	s.oldHandler = s.Server.Config.Handler
	s.Mux = httprouter.New()
	s.Server.Config.Handler = s.Mux
}

func (s *HTTPSuite) TearDownTest(c *gc.C) {
	s.Mux = nil
	s.Server.Config.Handler = s.oldHandler
}

func (s *HTTPSuite) TearDownSuite(c *gc.C) {
	if s.Server != nil {
		s.Server.Close()
	}
}