@@ -0,0 +1,153 @@
1+package pagewriter
2+
3+import (
4+ "errors"
5+ "html/template"
6+ "io/ioutil"
7+ "net/http"
8+ "net/http/httptest"
9+ "os"
10+ "path/filepath"
11+
12+ middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
13+ . "github.com/onsi/ginkgo"
14+ . "github.com/onsi/gomega"
15+)
16+
17+var _ = Describe("Static Pages", func() {
18+ var customDir string
19+ const customRobots = "I AM A ROBOT!!!"
20+ var errorPage *errorPageWriter
21+ var request *http.Request
22+
23+ BeforeEach(func() {
24+ errorTmpl, err := template.New("").Parse("{{.Title}}")
25+ Expect(err).ToNot(HaveOccurred())
26+ errorPage = &errorPageWriter{
27+ template: errorTmpl,
28+ }
29+
30+ customDir, err = ioutil.TempDir("", "oauth2-proxy-static-pages-test")
31+ Expect(err).ToNot(HaveOccurred())
32+
33+ robotsTxtFile := filepath.Join(customDir, robotsTxtName)
34+ Expect(ioutil.WriteFile(robotsTxtFile, []byte(customRobots), 0400)).To(Succeed())
35+
36+ request = httptest.NewRequest("", "http://127.0.0.1/", nil)
37+ request = middlewareapi.AddRequestScope(request, &middlewareapi.RequestScope{
38+ RequestID: testRequestID,
39+ })
40+ })
41+
42+ AfterEach(func() {
43+ Expect(os.RemoveAll(customDir)).To(Succeed())
44+ })
45+
46+ Context("Static Page Writer", func() {
47+ Context("With custom content", func() {
48+ var pageWriter *staticPageWriter
49+
50+ BeforeEach(func() {
51+ var err error
52+ pageWriter, err = newStaticPageWriter(customDir, errorPage)
53+ Expect(err).ToNot(HaveOccurred())
54+ })
55+
56+ Context("WriterRobotsTxt", func() {
57+ It("Should write the custom robots txt", func() {
58+ recorder := httptest.NewRecorder()
59+ pageWriter.WriteRobotsTxt(recorder, request)
60+
61+ body, err := ioutil.ReadAll(recorder.Result().Body)
62+ Expect(err).ToNot(HaveOccurred())
63+ Expect(string(body)).To(Equal(customRobots))
64+
65+ Expect(recorder.Result().StatusCode).To(Equal(http.StatusOK))
66+ })
67+ })
68+ })
69+
70+ Context("Without custom content", func() {
71+ var pageWriter *staticPageWriter
72+
73+ BeforeEach(func() {
74+ var err error
75+ pageWriter, err = newStaticPageWriter("", errorPage)
76+ Expect(err).ToNot(HaveOccurred())
77+ })
78+
79+ Context("WriterRobotsTxt", func() {
80+ It("Should write the custom robots txt", func() {
81+ recorder := httptest.NewRecorder()
82+ pageWriter.WriteRobotsTxt(recorder, request)
83+
84+ body, err := ioutil.ReadAll(recorder.Result().Body)
85+ Expect(err).ToNot(HaveOccurred())
86+ Expect(string(body)).To(Equal(string(defaultRobotsTxt)))
87+
88+ Expect(recorder.Result().StatusCode).To(Equal(http.StatusOK))
89+ })
90+
91+ It("Should serve an error if it cannot write the page", func() {
92+ recorder := &testBadResponseWriter{
93+ ResponseRecorder: httptest.NewRecorder(),
94+ }
95+ pageWriter.WriteRobotsTxt(recorder, request)
96+
97+ body, err := ioutil.ReadAll(recorder.Result().Body)
98+ Expect(err).ToNot(HaveOccurred())
99+ Expect(string(body)).To(Equal(string("Internal Server Error")))
100+
101+ Expect(recorder.Result().StatusCode).To(Equal(http.StatusInternalServerError))
102+ })
103+ })
104+ })
105+ })
106+
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)))
115+ })
116+ })
117+
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())
122+
123+ pages, err := loadStaticPages(customDir)
124+ Expect(err).ToNot(HaveOccurred())
125+ Expect(pages).To(HaveLen(1))
126+ Expect(pages).To(HaveKeyWithValue(robotsTxtName, defaultRobotsTxt))
127+ })
128+ })
129+ })
130+
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))
137+ })
138+ })
139+ })
140+})
141+
142+type testBadResponseWriter struct {
143+ *httptest.ResponseRecorder
144+ firstWriteCalled bool
145+}
146+
147+func (b *testBadResponseWriter) Write(buf []byte) (int, error) {
148+ if !b.firstWriteCalled {
149+ b.firstWriteCalled = true
150+ return 0, errors.New("write closed")
151+ }
152+ return b.ResponseRecorder.Write(buf)
153+}