Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package buffalo

import (
"cmp"
"database/sql"
_ "embed"
"encoding/json"
Expand All @@ -12,7 +13,6 @@ import (
"slices"
"strings"

"github.com/gobuffalo/buffalo/internal/defaults"
"github.com/gobuffalo/buffalo/internal/httpx"
"github.com/gobuffalo/events"
"github.com/gobuffalo/plush/v5"
Expand Down Expand Up @@ -191,7 +191,7 @@ const defaultErrorCT = "text/html; charset=utf-8"

func defaultErrorHandler(status int, origErr error, c Context) error {
env := c.Value("env")
requestCT := defaults.String(httpx.ContentType(c.Request()), defaultErrorCT)
requestCT := cmp.Or(httpx.ContentType(c.Request()), defaultErrorCT)

var defaultErrorResponse *ErrorResponse

Expand Down
36 changes: 0 additions & 36 deletions internal/defaults/defaults.go

This file was deleted.

52 changes: 0 additions & 52 deletions internal/defaults/defaults_test.go

This file was deleted.

32 changes: 0 additions & 32 deletions internal/fakesmtp/connection.go

This file was deleted.

50 changes: 33 additions & 17 deletions internal/httpx/content_type.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
// Package httpx provides HTTP utility functions.
package httpx

import (
"mime"
"net/http"
"strings"

"github.com/gobuffalo/buffalo/internal/defaults"
)

// ContentType extracts the content type from the request.
// It checks Content-Type header first, then falls back to Accept header.
// Returns the media type without parameters (e.g., "application/json").
// Empty string is returned if only wildcard "*/*" is present.
func ContentType(req *http.Request) string {
ct := defaults.String(req.Header.Get("Content-Type"), req.Header.Get("Accept"))
ct = strings.TrimSpace(ct)
var cts []string
if strings.Contains(ct, ",") {
cts = strings.Split(ct, ",")
} else {
cts = strings.Split(ct, ";")
}
for _, c := range cts {
c = strings.TrimSpace(c)
if strings.HasPrefix(c, "*/*") {
continue
if ct := req.Header.Get("Content-Type"); ct != "" {
mediatype, _, err := mime.ParseMediaType(ct)
if err == nil && mediatype != "" {
return mediatype
}
return strings.ToLower(c)
}
if ct == "*/*" {

accept := req.Header.Get("Accept")
if accept == "" {
return ""
}
return ct

for part := range strings.SplitSeq(accept, ",") {
part = strings.TrimSpace(part)
if part == "" {
continue
}

mediatype, _, err := mime.ParseMediaType(part)
if err != nil {
continue
}

if mediatype == "*/*" || mediatype == "*" {
continue
}

return mediatype
}

return ""
}
49 changes: 35 additions & 14 deletions internal/httpx/content_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,44 @@ func Test_ContentType(t *testing.T) {
r := require.New(t)

table := []struct {
Header string
Value string
Expected string
name string
header string
value string
expected string
}{
{"content-type", "a", "a"},
{"Content-Type", "c,d", "c"},
{"Content-Type", "e;f", "e"},
{"Content-Type", "", ""},
{"Accept", "", ""},
{"Accept", "*/*", ""},
{"Accept", "*/*;q=0.5, text/javascript, application/javascript, application/ecmascript, application/x-ecmascript", "text/javascript"},
{"accept", "text/javascript,application/javascript,application/ecmascript,application/x-ecmascript", "text/javascript"},
{"simple content-type", "content-type", "application/json", "application/json"},
{"content-type with charset", "Content-Type", "application/json; charset=utf-8", "application/json"},
{"accept single value", "Accept", "text/html", "text/html"},
{"accept with quality", "Accept", "text/html;q=0.9", "text/html"},
{"accept multiple values", "Accept", "application/json, text/html", "application/json"},
{"accept skips wildcard", "Accept", "*/*, application/json", "application/json"},
{"accept with quality values", "Accept", "text/plain;q=0.5, application/json;q=0.9", "text/plain"},
{"empty content-type", "Content-Type", "", ""},
{"empty accept", "Accept", "", ""},
{"wildcard only", "Accept", "*/*", ""},
{"complex accept header", "Accept", "*/*;q=0.5, text/javascript, application/javascript", "text/javascript"},
{"lowercase header name", "accept", "application/json", "application/json"},
{"uppercase media type", "Accept", "Application/JSON", "application/json"},
}

for _, tt := range table {
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set(tt.Header, tt.Value)
r.Equal(tt.Expected, ContentType(req))
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set(tt.header, tt.value)
r.Equal(tt.expected, ContentType(req))
})
}
}

func Test_ContentType_Priority(t *testing.T) {
r := require.New(t)

req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "text/html")
r.Equal("application/json", ContentType(req))

req = httptest.NewRequest("GET", "/", nil)
req.Header.Set("Accept", "text/html")
r.Equal("text/html", ContentType(req))
}
5 changes: 2 additions & 3 deletions mail/smtp_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ import (
"bytes"
"testing"

"github.com/gobuffalo/buffalo/internal/fakesmtp"
"github.com/gobuffalo/buffalo/mail"
"github.com/gobuffalo/buffalo/render"
"github.com/stretchr/testify/require"
)

var sender mail.Sender
var rend *render.Engine
var smtpServer *fakesmtp.Server
var smtpServer *fakeSMTPServer

const smtpPort = "2002"

func init() {
rend = render.New(render.Options{})
smtpServer, _ = fakesmtp.New(smtpPort)
smtpServer, _ = newFakeSMTPServer(smtpPort)
sender, _ = mail.NewSMTPSender("127.0.0.1", smtpPort, "username", "password")

go smtpServer.Start(smtpPort)
Expand Down
Loading
Loading