diff --git a/send/send.go b/send/send.go
index 146c98a..484dbe9 100644
--- a/send/send.go
+++ b/send/send.go
@@ -6,27 +6,44 @@ import (
"git.akyoto.dev/go/server"
)
-func Text(ctx server.Context, body string) error {
- ctx.Response().SetHeader("Content-Type", "text/plain")
- return ctx.String(body)
-}
-
+// CSS sends the body with the content type set to `text/css`.
func CSS(ctx server.Context, body string) error {
ctx.Response().SetHeader("Content-Type", "text/css")
return ctx.String(body)
}
+// CSV sends the body with the content type set to `text/csv`.
+func CSV(ctx server.Context, body string) error {
+ ctx.Response().SetHeader("Content-Type", "text/csv")
+ return ctx.String(body)
+}
+
+// HTML sends the body with the content type set to `text/html`.
+func HTML(ctx server.Context, body string) error {
+ ctx.Response().SetHeader("Content-Type", "text/html")
+ return ctx.String(body)
+}
+
+// JS sends the body with the content type set to `text/javascript`.
func JS(ctx server.Context, body string) error {
ctx.Response().SetHeader("Content-Type", "text/javascript")
return ctx.String(body)
}
+// JSON encodes the object in JSON format and sends it with the content type set to `application/json`.
func JSON(ctx server.Context, object any) error {
ctx.Response().SetHeader("Content-Type", "application/json")
return json.NewEncoder(ctx.Response()).Encode(object)
}
-func HTML(ctx server.Context, body string) error {
- ctx.Response().SetHeader("Content-Type", "text/html")
+// Text sends the body with the content type set to `text/plain`.
+func Text(ctx server.Context, body string) error {
+ ctx.Response().SetHeader("Content-Type", "text/plain")
+ return ctx.String(body)
+}
+
+// XML sends the body with the content type set to `text/xml`.
+func XML(ctx server.Context, body string) error {
+ ctx.Response().SetHeader("Content-Type", "text/xml")
return ctx.String(body)
}
diff --git a/send/send_test.go b/send/send_test.go
new file mode 100644
index 0000000..a6af2fc
--- /dev/null
+++ b/send/send_test.go
@@ -0,0 +1,80 @@
+package send_test
+
+import (
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "git.akyoto.dev/go/assert"
+ "git.akyoto.dev/go/server"
+ "git.akyoto.dev/go/server/send"
+)
+
+func TestContentTypes(t *testing.T) {
+ s := server.New()
+
+ s.Get("/css", func(ctx server.Context) error {
+ return send.CSS(ctx, "body{}")
+ })
+
+ s.Get("/csv", func(ctx server.Context) error {
+ return send.CSV(ctx, "ID;Name\n")
+ })
+
+ s.Get("/html", func(ctx server.Context) error {
+ return send.HTML(ctx, "")
+ })
+
+ s.Get("/js", func(ctx server.Context) error {
+ return send.JS(ctx, "console.log(42)")
+ })
+
+ s.Get("/json", func(ctx server.Context) error {
+ return send.JSON(ctx, struct{ Name string }{Name: "User 1"})
+ })
+
+ s.Get("/text", func(ctx server.Context) error {
+ return send.Text(ctx, "Hello")
+ })
+
+ s.Get("/xml", func(ctx server.Context) error {
+ return send.XML(ctx, "")
+ })
+
+ tests := []struct {
+ Method string
+ URL string
+ Body string
+ Status int
+ Response string
+ ContentType string
+ }{
+ {Method: "GET", URL: "/css", Body: "", Status: http.StatusOK, Response: "body{}", ContentType: "text/css"},
+ {Method: "GET", URL: "/csv", Body: "", Status: http.StatusOK, Response: "ID;Name\n", ContentType: "text/csv"},
+ {Method: "GET", URL: "/html", Body: "", Status: http.StatusOK, Response: "", ContentType: "text/html"},
+ {Method: "GET", URL: "/js", Body: "", Status: http.StatusOK, Response: "console.log(42)", ContentType: "text/javascript"},
+ {Method: "GET", URL: "/json", Body: "", Status: http.StatusOK, Response: "{\"Name\":\"User 1\"}\n", ContentType: "application/json"},
+ {Method: "GET", URL: "/text", Body: "", Status: http.StatusOK, Response: "Hello", ContentType: "text/plain"},
+ {Method: "GET", URL: "/xml", Body: "", Status: http.StatusOK, Response: "", ContentType: "text/xml"},
+ }
+
+ for _, test := range tests {
+ t.Run("example.com"+test.URL, func(t *testing.T) {
+ request := httptest.NewRequest(test.Method, "http://example.com"+test.URL, strings.NewReader(test.Body))
+ response := httptest.NewRecorder()
+ s.ServeHTTP(response, request)
+
+ result := response.Result()
+ assert.Equal(t, result.StatusCode, test.Status)
+
+ contentType := result.Header.Get("Content-Type")
+ assert.Equal(t, contentType, test.ContentType)
+
+ body, err := io.ReadAll(result.Body)
+ assert.Nil(t, err)
+ assert.Equal(t, string(body), test.Response)
+ })
+ }
+}