Added tests for different content types
This commit is contained in:
parent
174306702a
commit
797da600d3
31
send/send.go
31
send/send.go
@ -6,27 +6,44 @@ import (
|
|||||||
"git.akyoto.dev/go/server"
|
"git.akyoto.dev/go/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Text(ctx server.Context, body string) error {
|
// CSS sends the body with the content type set to `text/css`.
|
||||||
ctx.Response().SetHeader("Content-Type", "text/plain")
|
|
||||||
return ctx.String(body)
|
|
||||||
}
|
|
||||||
|
|
||||||
func CSS(ctx server.Context, body string) error {
|
func CSS(ctx server.Context, body string) error {
|
||||||
ctx.Response().SetHeader("Content-Type", "text/css")
|
ctx.Response().SetHeader("Content-Type", "text/css")
|
||||||
return ctx.String(body)
|
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 {
|
func JS(ctx server.Context, body string) error {
|
||||||
ctx.Response().SetHeader("Content-Type", "text/javascript")
|
ctx.Response().SetHeader("Content-Type", "text/javascript")
|
||||||
return ctx.String(body)
|
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 {
|
func JSON(ctx server.Context, object any) error {
|
||||||
ctx.Response().SetHeader("Content-Type", "application/json")
|
ctx.Response().SetHeader("Content-Type", "application/json")
|
||||||
return json.NewEncoder(ctx.Response()).Encode(object)
|
return json.NewEncoder(ctx.Response()).Encode(object)
|
||||||
}
|
}
|
||||||
|
|
||||||
func HTML(ctx server.Context, body string) error {
|
// Text sends the body with the content type set to `text/plain`.
|
||||||
ctx.Response().SetHeader("Content-Type", "text/html")
|
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)
|
return ctx.String(body)
|
||||||
}
|
}
|
||||||
|
80
send/send_test.go
Normal file
80
send/send_test.go
Normal 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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user