Added tests for different content types

This commit is contained in:
Eduard Urbach 2024-03-16 16:55:53 +01:00
parent debe00b9a9
commit 120cfe8e4e
Signed by: eduard
GPG key ID: 49226B848C78F6C8
2 changed files with 104 additions and 7 deletions

View file

@ -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)
}