Skip to content

Commit 412c76b

Browse files
committed
Add /erised/webpage route
1 parent ab78746 commit 412c76b

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

cmd/erised/serverRoutes.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ func (s *server) routes() {
2222
go s.mux.HandleFunc("/erised/info", s.handleInfo())
2323
go s.mux.HandleFunc("/erised/ip", s.handleIP())
2424
go s.mux.HandleFunc("/erised/shutdown", s.handleShutdown())
25+
go s.mux.HandleFunc("/erised/webpage", s.handleWebPage())
26+
go s.mux.HandleFunc("/erised/webpage/{path...}", s.handleWebPage())
2527
log.Debug().Msg("leaving routes")
2628
}
2729

@@ -247,3 +249,30 @@ func (s *server) handleShutdown() http.HandlerFunc {
247249
log.Debug().Msg("leaving handleShutdown")
248250
}
249251
}
252+
253+
func (s *server) handleWebPage() http.HandlerFunc {
254+
log.Debug().Msg("entering handleWebPage")
255+
256+
return func(res http.ResponseWriter, req *http.Request) {
257+
log.Info().
258+
Str("protocol", req.Proto).
259+
Str("remoteAddress", req.RemoteAddr).
260+
Str("method", req.Method).
261+
Str("host", req.Host).
262+
Str("path", req.RequestURI).
263+
Msg("handleWebPage")
264+
265+
res.Header().Set("Content-Type", "text/html")
266+
267+
data := "<!DOCTYPE html><html><head><title>Erised Webpage</title></head>"
268+
data += "<body>"
269+
data += "<center><h1>Host: " + req.Host + "</h1></center>"
270+
data += "<center><h1>Method: " + req.Method + "</h1></center>"
271+
data += "<center><h1>Protocol: " + req.Proto + "</h1></center>"
272+
data += "<center><h1>Request Path: " + req.RequestURI + "</h1></center>"
273+
data += "<hr><center><a href=\"https://github.com/EAddario/erised\">Erised: A nimble http server to test arbitrary REST API responses.</a></center>"
274+
data += "</body></html>"
275+
s.respond(res, encodingHTML, 0, data)
276+
log.Debug().Msg("leaving handleWebPage")
277+
}
278+
}

cmd/erised/serverRoutes_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,35 @@ func TestErisedShutdownRoute(t *testing.T) {
154154
})
155155
}
156156

157+
func TestErisedWebPageRoute(t *testing.T) {
158+
zerolog.SetGlobalLevel(zerolog.Disabled)
159+
g := goblin.Goblin(t)
160+
RegisterFailHandler(func(m string, _ ...int) { g.Fail(m) })
161+
svr := server{}
162+
163+
g.Describe("Test erised/webpage", func() {
164+
g.It("Should return StatusOK", func() {
165+
res := httptest.NewRecorder()
166+
req := httptest.NewRequest(http.MethodGet, "http://localhost:8080/erised/webpage", nil)
167+
svr.handleWebPage().ServeHTTP(res, req)
168+
169+
Ω(res.Code).Should(Equal(http.StatusOK))
170+
Ω(res.Body.String()).ShouldNot(BeEmpty())
171+
Ω(res.Header().Get("Content-Type")).Should(Equal("text/html"))
172+
})
173+
174+
g.It("Should also return StatusOK", func() {
175+
res := httptest.NewRecorder()
176+
req := httptest.NewRequest(http.MethodPost, "http://localhost:8080/erised/webpage/any/path", nil)
177+
svr.handleWebPage().ServeHTTP(res, req)
178+
179+
Ω(res.Code).Should(Equal(http.StatusOK))
180+
Ω(res.Body.String()).ShouldNot(BeEmpty())
181+
Ω(res.Header().Get("Content-Type")).Should(Equal("text/html"))
182+
})
183+
})
184+
}
185+
157186
func TestErisedLandingRoute(t *testing.T) {
158187
zerolog.SetGlobalLevel(zerolog.Disabled)
159188
g := goblin.Goblin(t)

0 commit comments

Comments
 (0)