From 2cb7e52f838fbf738f9bf76e2aee80a7a49c4848 Mon Sep 17 00:00:00 2001 From: Johan Lindh Date: Fri, 24 May 2024 08:49:58 +0200 Subject: [PATCH] move Template.ServeHTTP to it's own type --- handler.go | 18 ++++++++++++++++++ handler_test.go | 24 ++++++++++++++++++++++++ template.go | 14 ++------------ template_test.go | 19 ------------------- 4 files changed, 44 insertions(+), 31 deletions(-) create mode 100644 handler.go create mode 100644 handler_test.go diff --git a/handler.go b/handler.go new file mode 100644 index 0000000..869f0c8 --- /dev/null +++ b/handler.go @@ -0,0 +1,18 @@ +package jaws + +import "net/http" + +// Handler implements ServeHTTP with a jaws.Template +type Handler struct { + *Jaws + Template +} + +func (h Handler) ServeHTTP(wr http.ResponseWriter, r *http.Request) { + _ = h.Log(h.NewRequest(r).NewElement(h.Template).JawsRender(wr, nil)) +} + +// Handler returns a http.Handler using a jaws.Template +func (jw *Jaws) Handler(name string, dot any) http.Handler { + return Handler{Jaws: jw, Template: Template{Template: name, Dot: dot}} +} diff --git a/handler_test.go b/handler_test.go new file mode 100644 index 0000000..ea17503 --- /dev/null +++ b/handler_test.go @@ -0,0 +1,24 @@ +package jaws + +import ( + "bytes" + "net/http/httptest" + "testing" +) + +func TestHandler_ServeHTTP(t *testing.T) { + nextJid = 0 + rq := newTestRequest() + defer rq.Close() + + dot := 123 + h := rq.Jaws.Handler("testtemplate", dot) + var buf bytes.Buffer + var rr httptest.ResponseRecorder + rr.Body = &buf + r := httptest.NewRequest("GET", "/", nil) + h.ServeHTTP(&rr, r) + if got := buf.String(); got != `
123
` { + t.Error(got) + } +} diff --git a/template.go b/template.go index 4a99bd2..2e20bfe 100644 --- a/template.go +++ b/template.go @@ -4,21 +4,18 @@ import ( "fmt" "html/template" "io" - "net/http" "strings" "github.com/linkdata/jaws/what" ) type Template struct { - *Jaws Template string Dot any } var _ UI = Template{} // statically ensure interface is defined var _ EventHandler = Template{} // statically ensure interface is defined -var _ http.Handler = Template{} // statically ensure interface is defined func (t Template) String() string { return fmt.Sprintf("{%q, %s}", t.Template, TagString(t.Dot)) @@ -32,7 +29,7 @@ func (t Template) JawsRender(e *Element, wr io.Writer, params []any) error { e.Tag(tags...) e.handlers = append(e.handlers, handlers...) attrstr := template.HTMLAttr(strings.Join(attrs, " ")) // #nosec G203 - return t.Jaws.Lookup(t.Template).Execute(wr, With{ + return e.Request.Jaws.Lookup(t.Template).Execute(wr, With{ Element: e, RequestWriter: e.Request.Writer(wr), Dot: t.Dot, @@ -50,13 +47,6 @@ func (t Template) JawsEvent(e *Element, wht what.What, val string) error { return callEventHandler(t.Dot, e, wht, val) } -// ServeHTTP implements http.Handler. -// -// http.DefaultServeMux.Handle("/user", myJaws.NewTemplate("user.html", userData)) -func (t Template) ServeHTTP(wr http.ResponseWriter, r *http.Request) { - _ = t.Log(t.NewRequest(r).NewElement(t).JawsRender(wr, nil)) -} - func (jw *Jaws) NewTemplate(name string, dot any) Template { - return Template{Jaws: jw, Template: name, Dot: dot} + return Template{Template: name, Dot: dot} } diff --git a/template_test.go b/template_test.go index 0774377..d2f5838 100644 --- a/template_test.go +++ b/template_test.go @@ -1,8 +1,6 @@ package jaws import ( - "bytes" - "net/http/httptest" "testing" ) @@ -28,20 +26,3 @@ func TestTemplate_Calls_Dot_Updater(t *testing.T) { t.Error(dot.updateCalled) } } - -func TestTemplate_As_Handler(t *testing.T) { - nextJid = 0 - rq := newTestRequest() - defer rq.Close() - - dot := 123 - tmpl := rq.Jaws.NewTemplate("testtemplate", dot) - var buf bytes.Buffer - var rr httptest.ResponseRecorder - rr.Body = &buf - r := httptest.NewRequest("GET", "/", nil) - tmpl.ServeHTTP(&rr, r) - if got := buf.String(); got != `
123
` { - t.Error(got) - } -}