package web_tests import ( "testing" web "git.sharkk.net/Go/Web" "git.sharkk.net/Go/Web/send" ) func TestContentTypes(t *testing.T) { s := web.NewServer() s.Get("/css", func(ctx web.Context) error { return send.CSS(ctx, "body{}") }) s.Get("/csv", func(ctx web.Context) error { return send.CSV(ctx, "ID;Name\n") }) s.Get("/html", func(ctx web.Context) error { return send.HTML(ctx, "") }) s.Get("/js", func(ctx web.Context) error { return send.JS(ctx, "console.log(42)") }) s.Get("/json", func(ctx web.Context) error { return send.JSON(ctx, struct{ Name string }{Name: "User 1"}) }) s.Get("/text", func(ctx web.Context) error { return send.Text(ctx, "Hello") }) s.Get("/xml", func(ctx web.Context) error { return send.XML(ctx, "") }) tests := []struct { Method string URL string Body string Status int Response string ContentType string }{ {Method: "GET", URL: "/css", Status: 200, Response: "body{}", ContentType: "text/css"}, {Method: "GET", URL: "/csv", Status: 200, Response: "ID;Name\n", ContentType: "text/csv"}, {Method: "GET", URL: "/html", Status: 200, Response: "", ContentType: "text/html"}, {Method: "GET", URL: "/js", Status: 200, Response: "console.log(42)", ContentType: "text/javascript"}, {Method: "GET", URL: "/json", Status: 200, Response: "{\"Name\":\"User 1\"}\n", ContentType: "application/json"}, {Method: "GET", URL: "/text", Status: 200, Response: "Hello", ContentType: "text/plain"}, {Method: "GET", URL: "/xml", Status: 200, Response: "", ContentType: "text/xml"}, } for _, test := range tests { t.Run(test.URL, func(t *testing.T) { response := s.Request(test.Method, "http://example.com"+test.URL, nil, nil) if response.Status() != test.Status { t.Errorf("Expected status %d, got %d", test.Status, response.Status()) } if response.Header("Content-Type") != test.ContentType { t.Errorf("Expected content type %s, got %s", test.ContentType, response.Header("Content-Type")) } if string(response.Body()) != test.Response { t.Errorf("Expected response %s, got %s", test.Response, string(response.Body())) } }) } }