Exemple #1
0
func NewWebRunner(
	port uint,
	logger lager.Logger,
	handler http.Handler,
	tlsConfig *tls.Config,
	forceHTTPS bool,
	redirectPort uint,
	username string,
	password string,
	cookieHandler *securecookie.SecureCookie,
) ifrit.Runner {

	m := middleware.Chain{
		middleware.NewPanicRecovery(logger),
		middleware.NewLogger(logger),
	}

	if forceHTTPS {
		m = append(m, middleware.NewHTTPSEnforcer(redirectPort))
	} else if username != "" && password != "" {
		m = append(m, middleware.NewAuth(username, password, logger, cookieHandler))
	}

	return &webRunner{
		port:      port,
		logger:    logger,
		handler:   m.Wrap(handler),
		tlsConfig: tlsConfig,
	}
}
)

var _ = Describe("HttpsEnforcer", func() {
	const httpsPort = uint(34567)

	var (
		request           *http.Request
		writer            *httptest.ResponseRecorder
		fakeHandler       *fakes.FakeHandler
		wrappedMiddleware http.Handler
	)

	BeforeEach(func() {
		fakeHandler = &fakes.FakeHandler{}
		writer = httptest.NewRecorder()
		enforcer := middleware.NewHTTPSEnforcer(httpsPort)

		wrappedMiddleware = enforcer.Wrap(fakeHandler)
	})

	Context("when the URL is valid", func() {
		BeforeEach(func() {
			var err error
			request, err = http.NewRequest("GET", "http://localhost/foo/bar", nil)
			Expect(err).NotTo(HaveOccurred())
		})

		It("does not call next middleware", func() {
			wrappedMiddleware.ServeHTTP(writer, request)

			Expect(fakeHandler.ServeHTTPCallCount()).To(BeZero())