Added tests for different content types

This commit is contained in:
Eduard Urbach 2024-03-16 16:55:53 +01:00
parent e16221c971
commit 9b51e01244
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
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)
}

80
send/send_test.go Normal file
View File

@ -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, "<html></html>")
})
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, "<xml></xml>")
})
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: "<html></html>", 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: "<xml></xml>", 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)
})
}
}