@@ -1,6 +1,8 @@
11package pagewriter
22
33import (
4+ "errors"
5+ "fmt"
46 "io/ioutil"
57 "net/http"
68 "net/http/httptest"
@@ -8,6 +10,7 @@ import (
810 "path/filepath"
911
1012 . "github.com/onsi/ginkgo"
13+ . "github.com/onsi/ginkgo/extensions/table"
1114 . "github.com/onsi/gomega"
1215)
1316
@@ -135,4 +138,144 @@ var _ = Describe("Writer", func() {
135138 })
136139 })
137140 })
141+
142+ Context("WriterFuncs", func() {
143+ type writerFuncsTableInput struct {
144+ writer Writer
145+ expectedStatus int
146+ expectedBody string
147+ }
148+
149+ DescribeTable("WriteSignInPage",
150+ func(in writerFuncsTableInput) {
151+ rw := httptest.NewRecorder()
152+ req := httptest.NewRequest("", "/sign-in", nil)
153+ redirectURL := "<redirectURL>"
154+ in.writer.WriteSignInPage(rw, req, redirectURL)
155+
156+ Expect(rw.Result().StatusCode).To(Equal(in.expectedStatus))
157+
158+ body, err := ioutil.ReadAll(rw.Result().Body)
159+ Expect(err).ToNot(HaveOccurred())
160+ Expect(string(body)).To(Equal(in.expectedBody))
161+ },
162+ Entry("With no override", writerFuncsTableInput{
163+ writer: &WriterFuncs{},
164+ expectedStatus: 200,
165+ expectedBody: "Sign In",
166+ }),
167+ Entry("With an override function", writerFuncsTableInput{
168+ writer: &WriterFuncs{
169+ SignInPageFunc: func(rw http.ResponseWriter, req *http.Request, redirectURL string) {
170+ rw.WriteHeader(202)
171+ rw.Write([]byte(fmt.Sprintf("%s %s", req.URL.Path, redirectURL)))
172+ },
173+ },
174+ expectedStatus: 202,
175+ expectedBody: "/sign-in <redirectURL>",
176+ }),
177+ )
178+
179+ DescribeTable("WriteErrorPage",
180+ func(in writerFuncsTableInput) {
181+ rw := httptest.NewRecorder()
182+ in.writer.WriteErrorPage(rw, ErrorPageOpts{
183+ Status: http.StatusInternalServerError,
184+ RedirectURL: "<redirectURL>",
185+ RequestID: "12345",
186+ AppError: "application error",
187+ })
188+
189+ Expect(rw.Result().StatusCode).To(Equal(in.expectedStatus))
190+
191+ body, err := ioutil.ReadAll(rw.Result().Body)
192+ Expect(err).ToNot(HaveOccurred())
193+ Expect(string(body)).To(Equal(in.expectedBody))
194+ },
195+ Entry("With no override", writerFuncsTableInput{
196+ writer: &WriterFuncs{},
197+ expectedStatus: 500,
198+ expectedBody: "500 - application error",
199+ }),
200+ Entry("With an override function", writerFuncsTableInput{
201+ writer: &WriterFuncs{
202+ ErrorPageFunc: func(rw http.ResponseWriter, opts ErrorPageOpts) {
203+ rw.WriteHeader(503)
204+ rw.Write([]byte(fmt.Sprintf("%s %s", opts.RequestID, opts.RedirectURL)))
205+ },
206+ },
207+ expectedStatus: 503,
208+ expectedBody: "12345 <redirectURL>",
209+ }),
210+ )
211+
212+ DescribeTable("ProxyErrorHandler",
213+ func(in writerFuncsTableInput) {
214+ rw := httptest.NewRecorder()
215+ req := httptest.NewRequest("", "/proxy", nil)
216+ err := errors.New("proxy error")
217+ in.writer.ProxyErrorHandler(rw, req, err)
218+
219+ Expect(rw.Result().StatusCode).To(Equal(in.expectedStatus))
220+
221+ body, err := ioutil.ReadAll(rw.Result().Body)
222+ Expect(err).ToNot(HaveOccurred())
223+ Expect(string(body)).To(Equal(in.expectedBody))
224+ },
225+ Entry("With no override", writerFuncsTableInput{
226+ writer: &WriterFuncs{},
227+ expectedStatus: 502,
228+ expectedBody: "502 - proxy error",
229+ }),
230+ Entry("With an override function for the proxy handler", writerFuncsTableInput{
231+ writer: &WriterFuncs{
232+ ProxyErrorFunc: func(rw http.ResponseWriter, req *http.Request, proxyErr error) {
233+ rw.WriteHeader(503)
234+ rw.Write([]byte(fmt.Sprintf("%s %v", req.URL.Path, proxyErr)))
235+ },
236+ },
237+ expectedStatus: 503,
238+ expectedBody: "/proxy proxy error",
239+ }),
240+ Entry("With an override function for the error page", writerFuncsTableInput{
241+ writer: &WriterFuncs{
242+ ErrorPageFunc: func(rw http.ResponseWriter, opts ErrorPageOpts) {
243+ rw.WriteHeader(500)
244+ rw.Write([]byte("Internal Server Error"))
245+ },
246+ },
247+ expectedStatus: 500,
248+ expectedBody: "Internal Server Error",
249+ }),
250+ )
251+
252+ DescribeTable("WriteRobotsTxt",
253+ func(in writerFuncsTableInput) {
254+ rw := httptest.NewRecorder()
255+ req := httptest.NewRequest("", "/robots.txt", nil)
256+ in.writer.WriteRobotsTxt(rw, req)
257+
258+ Expect(rw.Result().StatusCode).To(Equal(in.expectedStatus))
259+
260+ body, err := ioutil.ReadAll(rw.Result().Body)
261+ Expect(err).ToNot(HaveOccurred())
262+ Expect(string(body)).To(Equal(in.expectedBody))
263+ },
264+ Entry("With no override", writerFuncsTableInput{
265+ writer: &WriterFuncs{},
266+ expectedStatus: 200,
267+ expectedBody: "Allow: *",
268+ }),
269+ Entry("With an override function", writerFuncsTableInput{
270+ writer: &WriterFuncs{
271+ RobotsTxtfunc: func(rw http.ResponseWriter, req *http.Request) {
272+ rw.WriteHeader(202)
273+ rw.Write([]byte("Disallow: *"))
274+ },
275+ },
276+ expectedStatus: 202,
277+ expectedBody: "Disallow: *",
278+ }),
279+ )
280+ })
138281})