@@ -0,0 +1,153 @@
12+ middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
13+ . "github.com/onsi/ginkgo"
14+ . "github.com/onsi/gomega"
17+var _ = Describe("Static Pages", func() {
19+ const customRobots = "I AM A ROBOT!!!"
20+ var errorPage *errorPageWriter
21+ var request *http.Request
24+ errorTmpl, err := template.New("").Parse("{{.Title}}")
25+ Expect(err).ToNot(HaveOccurred())
26+ errorPage = &errorPageWriter{
30+ customDir, err = ioutil.TempDir("", "oauth2-proxy-static-pages-test")
31+ Expect(err).ToNot(HaveOccurred())
33+ robotsTxtFile := filepath.Join(customDir, robotsTxtName)
34+ Expect(ioutil.WriteFile(robotsTxtFile, []byte(customRobots), 0400)).To(Succeed())
36+ request = httptest.NewRequest("", "http://127.0.0.1/", nil)
37+ request = middlewareapi.AddRequestScope(request, &middlewareapi.RequestScope{
38+ RequestID: testRequestID,
43+ Expect(os.RemoveAll(customDir)).To(Succeed())
46+ Context("Static Page Writer", func() {
47+ Context("With custom content", func() {
48+ var pageWriter *staticPageWriter
52+ pageWriter, err = newStaticPageWriter(customDir, errorPage)
53+ Expect(err).ToNot(HaveOccurred())
56+ Context("WriterRobotsTxt", func() {
57+ It("Should write the custom robots txt", func() {
58+ recorder := httptest.NewRecorder()
59+ pageWriter.WriteRobotsTxt(recorder, request)
61+ body, err := ioutil.ReadAll(recorder.Result().Body)
62+ Expect(err).ToNot(HaveOccurred())
63+ Expect(string(body)).To(Equal(customRobots))
65+ Expect(recorder.Result().StatusCode).To(Equal(http.StatusOK))
70+ Context("Without custom content", func() {
71+ var pageWriter *staticPageWriter
75+ pageWriter, err = newStaticPageWriter("", errorPage)
76+ Expect(err).ToNot(HaveOccurred())
79+ Context("WriterRobotsTxt", func() {
80+ It("Should write the custom robots txt", func() {
81+ recorder := httptest.NewRecorder()
82+ pageWriter.WriteRobotsTxt(recorder, request)
84+ body, err := ioutil.ReadAll(recorder.Result().Body)
85+ Expect(err).ToNot(HaveOccurred())
86+ Expect(string(body)).To(Equal(string(defaultRobotsTxt)))
88+ Expect(recorder.Result().StatusCode).To(Equal(http.StatusOK))
91+ It("Should serve an error if it cannot write the page", func() {
92+ recorder := &testBadResponseWriter{
93+ ResponseRecorder: httptest.NewRecorder(),
95+ pageWriter.WriteRobotsTxt(recorder, request)
97+ body, err := ioutil.ReadAll(recorder.Result().Body)
98+ Expect(err).ToNot(HaveOccurred())
99+ Expect(string(body)).To(Equal(string("Internal Server Error")))
101+ Expect(recorder.Result().StatusCode).To(Equal(http.StatusInternalServerError))
107+ Context("loadStaticPages", func() {
108+ Context("With custom content", func() {
109+ Context("And a custom robots txt", func() {
110+ It("Loads the custom content", func() {
111+ pages, err := loadStaticPages(customDir)
112+ Expect(err).ToNot(HaveOccurred())
113+ Expect(pages).To(HaveLen(1))
114+ Expect(pages).To(HaveKeyWithValue(robotsTxtName, []byte(customRobots)))
118+ Context("And no custom robots txt", func() {
119+ It("returns the default content", func() {
120+ robotsTxtFile := filepath.Join(customDir, robotsTxtName)
121+ Expect(os.Remove(robotsTxtFile)).To(Succeed())
123+ pages, err := loadStaticPages(customDir)
124+ Expect(err).ToNot(HaveOccurred())
125+ Expect(pages).To(HaveLen(1))
126+ Expect(pages).To(HaveKeyWithValue(robotsTxtName, defaultRobotsTxt))
131+ Context("Without custom content", func() {
132+ It("Loads the default content", func() {
133+ pages, err := loadStaticPages("")
134+ Expect(err).ToNot(HaveOccurred())
135+ Expect(pages).To(HaveLen(1))
136+ Expect(pages).To(HaveKeyWithValue(robotsTxtName, defaultRobotsTxt))
142+type testBadResponseWriter struct {
143+ *httptest.ResponseRecorder
144+ firstWriteCalled bool
147+func (b *testBadResponseWriter) Write(buf []byte) (int, error) {
148+ if !b.firstWriteCalled {
149+ b.firstWriteCalled = true
150+ return 0, errors.New("write closed")
152+ return b.ResponseRecorder.Write(buf)