@@ -1,6 +1,8 @@
1package pagewriter
1package pagewriter
4 "io/ioutil"
6 "io/ioutil"
6 "net/http/httptest"
8 "net/http/httptest"
@@ -8,6 +10,7 @@ import (
8 "path/filepath"
10 "path/filepath"
10 . "github.com/onsi/ginkgo"
12 . "github.com/onsi/ginkgo"
13+ . "github.com/onsi/ginkgo/extensions/table"
11 . "github.com/onsi/gomega"
14 . "github.com/onsi/gomega"
@@ -135,4 +138,144 @@ var _ = Describe("Writer", func() {
142+ Context("WriterFuncs", func() {
143+ type writerFuncsTableInput struct {
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)
156+ Expect(rw.Result().StatusCode).To(Equal(in.expectedStatus))
158+ body, err := ioutil.ReadAll(rw.Result().Body)
159+ Expect(err).ToNot(HaveOccurred())
160+ Expect(string(body)).To(Equal(in.expectedBody))
162+ Entry("With no override", writerFuncsTableInput{
163+ writer: &WriterFuncs{},
164+ expectedStatus: 200,
165+ expectedBody: "Sign In",
167+ Entry("With an override function", writerFuncsTableInput{
168+ writer: &WriterFuncs{
169+ SignInPageFunc: func(rw http.ResponseWriter, req *http.Request, redirectURL string) {
171+ rw.Write([]byte(fmt.Sprintf("%s %s", req.URL.Path, redirectURL)))
174+ expectedStatus: 202,
175+ expectedBody: "/sign-in <redirectURL>",
179+ DescribeTable("WriteErrorPage",
180+ func(in writerFuncsTableInput) {
181+ rw := httptest.NewRecorder()
182+ in.writer.WriteErrorPage(rw, ErrorPageOpts{
183+ Status: http.StatusInternalServerError,
184+ RedirectURL: "<redirectURL>",
186+ AppError: "application error",
189+ Expect(rw.Result().StatusCode).To(Equal(in.expectedStatus))
191+ body, err := ioutil.ReadAll(rw.Result().Body)
192+ Expect(err).ToNot(HaveOccurred())
193+ Expect(string(body)).To(Equal(in.expectedBody))
195+ Entry("With no override", writerFuncsTableInput{
196+ writer: &WriterFuncs{},
197+ expectedStatus: 500,
198+ expectedBody: "500 - application error",
200+ Entry("With an override function", writerFuncsTableInput{
201+ writer: &WriterFuncs{
202+ ErrorPageFunc: func(rw http.ResponseWriter, opts ErrorPageOpts) {
204+ rw.Write([]byte(fmt.Sprintf("%s %s", opts.RequestID, opts.RedirectURL)))
207+ expectedStatus: 503,
208+ expectedBody: "12345 <redirectURL>",
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)
219+ Expect(rw.Result().StatusCode).To(Equal(in.expectedStatus))
221+ body, err := ioutil.ReadAll(rw.Result().Body)
222+ Expect(err).ToNot(HaveOccurred())
223+ Expect(string(body)).To(Equal(in.expectedBody))
225+ Entry("With no override", writerFuncsTableInput{
226+ writer: &WriterFuncs{},
227+ expectedStatus: 502,
228+ expectedBody: "502 - proxy error",
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) {
234+ rw.Write([]byte(fmt.Sprintf("%s %v", req.URL.Path, proxyErr)))
237+ expectedStatus: 503,
238+ expectedBody: "/proxy proxy error",
240+ Entry("With an override function for the error page", writerFuncsTableInput{
241+ writer: &WriterFuncs{
242+ ErrorPageFunc: func(rw http.ResponseWriter, opts ErrorPageOpts) {
244+ rw.Write([]byte("Internal Server Error"))
247+ expectedStatus: 500,
248+ expectedBody: "Internal Server Error",
252+ DescribeTable("WriteRobotsTxt",
253+ func(in writerFuncsTableInput) {
254+ rw := httptest.NewRecorder()
255+ req := httptest.NewRequest("", "/robots.txt", nil)
256+ in.writer.WriteRobotsTxt(rw, req)
258+ Expect(rw.Result().StatusCode).To(Equal(in.expectedStatus))
260+ body, err := ioutil.ReadAll(rw.Result().Body)
261+ Expect(err).ToNot(HaveOccurred())
262+ Expect(string(body)).To(Equal(in.expectedBody))
264+ Entry("With no override", writerFuncsTableInput{
265+ writer: &WriterFuncs{},
266+ expectedStatus: 200,
267+ expectedBody: "Allow: *",
269+ Entry("With an override function", writerFuncsTableInput{
270+ writer: &WriterFuncs{
271+ RobotsTxtfunc: func(rw http.ResponseWriter, req *http.Request) {
273+ rw.Write([]byte("Disallow: *"))
276+ expectedStatus: 202,
277+ expectedBody: "Disallow: *",