コード例 #1
0
ファイル: main.go プロジェクト: rightscale/go-boilerplate
// SetupMainMux returns a goji Mux initialized with the middleware and a health check
// handler. The mux is a net/http.Handler and thus has a ServeHTTP
// method which can be convient to call in tests without the actual network stuff and
// goroutine that SetupMainServer adds
func SetupMainMux() *web.Mux {
	mx := web.New()
	gojiutil.AddCommon15(mx, log15.Root())
	mx.Use(gojiutil.ParamsLogger(false)) // useful for debugging
	mx.Get("/health-check", healthCheckHandler)
	mx.NotFound(handleNotFound)
	return mx
}
コード例 #2
0
)

// This demo file shows two ways to test the handlers, it is not suggested to use both in a real
// project, the two methods are provided here as a sample.

// Tests that simply create a mux and exercise that by calling the Mux's ServeHTTP function
// This is great for isolated tests, it becomes difficult when the middleware higher in the stack
// is needed or other concurrent goroutines and other handlers al also needed for higher-level tests
var _ = Describe("Mux-based settings tests", func() {

	var mx *web.Mux // mux with the handlers we're testing

	BeforeEach(func() {
		settings = make(map[string]string)
		mx = NewMux()
		gojiutil.AddCommon15(mx, log15.Root())
		mx.Use(gojiutil.ParamsLogger(true)) // useful for troubleshooting
	})

	It("gets what it sets", func() {
		// set a value
		req, _ := http.NewRequest("PUT", "http://example.com/settings/hello?value=world",
			bytes.NewReader([]byte{}))
		resp := httptest.NewRecorder()
		mx.ServeHTTP(resp, req)
		Ω(resp.Code).Should(Equal(200))
		Ω(settings["hello"]).Should(Equal("world"))

		// get the value back
		req, _ = http.NewRequest("GET", "http://example.com/settings/hello", nil)
		resp = httptest.NewRecorder()