Example #1
0
func NewHandler(backends domain.Backends, logger lager.Logger, apiConfig config.API, staticDir string) http.Handler {
	mux := http.NewServeMux()

	mux.Handle("/", http.FileServer(http.Dir(staticDir)))

	mux.Handle("/v0/backends", BackendsIndex(backends))

	return middleware.Chain{
		middleware.NewPanicRecovery(logger),
		middleware.NewLogger(logger, "/v0"),
		middleware.NewHttpsEnforcer(apiConfig.ForceHttps),
		middleware.NewBasicAuth(apiConfig.Username, apiConfig.Password),
	}.Wrap(mux)
}
	var (
		request           *http.Request
		writer            *httptest.ResponseRecorder
		fakeHandler       *fakes.FakeHandler
		wrappedMiddleware http.Handler
		forceHttps        bool
	)

	BeforeEach(func() {
		forceHttps = true
	})

	JustBeforeEach(func() {
		fakeHandler = &fakes.FakeHandler{}
		writer = httptest.NewRecorder()
		enforcer := middleware.NewHttpsEnforcer(forceHttps)

		wrappedMiddleware = enforcer.Wrap(fakeHandler)
	})

	Context("With https header", func() {
		BeforeEach(func() {
			request, _ = http.NewRequest("GET", "https://localhost/foo/bar", nil)
			request.Header.Set("X-Forwarded-Proto", "https")
		})

		It("calls next middleware", func() {
			wrappedMiddleware.ServeHTTP(writer, request)

			Expect(fakeHandler.ServeHTTPCallCount()).To(Equal(1))
		})