Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V0.5 #8

Merged
merged 5 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ jobs:

- name: Test
run: go test -v ./...

- name: Lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
71 changes: 56 additions & 15 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"html/template"
"net/http"
)

Expand Down Expand Up @@ -76,6 +77,61 @@ func (r Response) Write(w http.ResponseWriter) error {
return nil
}

// RenderHTML renders an HTML document fragment along with the defined HTMX headers.
func (r Response) RenderHTML(w http.ResponseWriter, html template.HTML) (int, error) {
err := r.Write(w)
if err != nil {
return 0, err
}

return w.Write([]byte(html))
}

// RenderTempl renders a Templ component along with the defined HTMX headers.
func (r Response) RenderTempl(ctx context.Context, w http.ResponseWriter, c templComponent) error {
err := r.Write(w)
if err != nil {
return err
}

err = c.Render(ctx, w)
if err != nil {
return err
}

return nil
}

// MustWrite applies the defined HTMX headers to a given response writer, otherwise it panics.
//
// Under the hood this uses [Response.Write].
func (r Response) MustWrite(w http.ResponseWriter) {
err := r.Write(w)
if err != nil {
panic(err)
}
}

// MustRenderHTML renders an HTML document fragment along with the defined HTMX headers, otherwise it panics.
//
// Under the hood this uses [Response.RenderHTML].
func (r Response) MustRenderHTML(w http.ResponseWriter, html template.HTML) {
_, err := r.RenderHTML(w, html)
if err != nil {
panic(err)
}
}

// MustRenderTempl renders a Templ component along with the defined HTMX headers, otherwise it panics.
//
// Under the hood this uses [Response.RenderTempl].
func (r Response) MustRenderTempl(ctx context.Context, w http.ResponseWriter, c templComponent) {
err := r.RenderTempl(ctx, w, c)
if err != nil {
panic(err)
}
}

// Headers returns a copied map of the headers. Any modifications to the
// returned headers will not affect the headers in this struct.
func (r Response) Headers() (map[string]string, error) {
Expand Down Expand Up @@ -111,18 +167,3 @@ func (r Response) Headers() (map[string]string, error) {

return m, nil
}

// RenderTempl renders a Templ component along with the defined HTMX headers.
func (r Response) RenderTempl(ctx context.Context, w http.ResponseWriter, c templComponent) error {
err := r.Write(w)
if err != nil {
return err
}

err = c.Render(ctx, w)
if err != nil {
return err
}

return nil
}
102 changes: 102 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package htmx

import (
"html/template"
"net/http"
"testing"
)

func TestWrite(t *testing.T) {
w := newMockResponseWriter()

err := NewResponse().
StatusCode(StatusStopPolling).
Location("/profiles").
Redirect("/pull").
PushURL("/push").
Refresh(true).
ReplaceURL("/water").
Retarget("#world").
Reselect("#hello").
AddTrigger(Trigger("myEvent")).
Reswap(SwapInnerHTML.ShowOn("#swappy", Top)).
Write(w)
if err != nil {
t.Errorf("an error occurred writing a response: %v", err)
}

if w.statusCode != StatusStopPolling {
t.Errorf("wrong error code. want=%v, got=%v", StatusStopPolling, w.statusCode)
}

expectedHeaders := map[string]string{
HeaderTrigger: "myEvent",
HeaderLocation: "/profiles",
HeaderRedirect: "/pull",
HeaderPushURL: "/push",
HeaderRefresh: "true",
HeaderReplaceUrl: "/water",
HeaderRetarget: "#world",
HeaderReselect: "#hello",
HeaderReswap: "innerHTML show:#swappy:top",
}

for k, v := range expectedHeaders {
got := w.header.Get(k)
if got != v {
t.Errorf("wrong value for header %q. got=%q, want=%q", k, got, v)
}
}
}

func TestRenderHTML(t *testing.T) {
text := `hello world!`

w := newMockResponseWriter()

_, err := NewResponse().Location("/conversation/message").RenderHTML(w, template.HTML(text))
if err != nil {
t.Errorf("an error occurred writing HTML: %v", err)
}

if got, want := w.Header().Get(HeaderLocation), "/conversation/message"; got != want {
t.Errorf("wrong value for header %q. got=%q, want=%q", HeaderLocation, got, want)
}

if string(w.body) != text {
t.Errorf("wrong response body. got=%q, want=%q", string(w.body), text)
}
}

func TestMustRenderHTML(t *testing.T) {
text := `hello world!`

w := newMockResponseWriter()

NewResponse().MustRenderHTML(w, template.HTML(text))
}

type mockResponseWriter struct {
body []byte
statusCode int
header http.Header
}

func newMockResponseWriter() *mockResponseWriter {
return &mockResponseWriter{
header: http.Header{},
}
}

func (mrw *mockResponseWriter) Header() http.Header {
return mrw.header
}

func (mrw *mockResponseWriter) Write(b []byte) (int, error) {
mrw.body = append(mrw.body, b...)
return 0, nil
}

func (mrw *mockResponseWriter) WriteHeader(statusCode int) {
mrw.statusCode = statusCode
}
Loading