summaryrefslogtreecommitdiff
path: root/internal/api/util/negotiate_test.go
diff options
context:
space:
mode:
authorLibravatar Daenney <daenney@users.noreply.github.com>2023-05-16 15:08:45 +0200
committerLibravatar GitHub <noreply@github.com>2023-05-16 15:08:45 +0200
commite91cabb704b1cfc1cae438f5be8600e83dc5578e (patch)
treead0fd473a7327fc602c63acfaf57b68c0efc2ef6 /internal/api/util/negotiate_test.go
parent[bugfix] Fix proxy_pass in named location (#1794) (diff)
downloadgotosocial-e91cabb704b1cfc1cae438f5be8600e83dc5578e.tar.xz
[bugfix] Fix NegotiateFormat with multiple accept headers (#1797)
* [bugfix] Fix NegotiateAccept with multi accept There's a bug in Gin's NegotiateFormat that doesn't handle the presence of multilpe accept headers. This lifts the code from the PR @tsmethurst sent a year ago to Gin into our codebase to fix the issue. * [bugfix] Concat accept header in webfinger Some implementations bug out when there's multiple accept headers, including Gin (see 7050112af1ccc935ec542cb41fa8b07f7357539d). But things seem to work reliably with a single accept header with multiple parts. Fixes: #1793
Diffstat (limited to 'internal/api/util/negotiate_test.go')
-rw-r--r--internal/api/util/negotiate_test.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/internal/api/util/negotiate_test.go b/internal/api/util/negotiate_test.go
new file mode 100644
index 000000000..a8b28b55f
--- /dev/null
+++ b/internal/api/util/negotiate_test.go
@@ -0,0 +1,65 @@
+package util
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/gin-gonic/gin"
+)
+
+type testMIMES []MIME
+
+func (tm testMIMES) String(t *testing.T) string {
+ t.Helper()
+
+ res := tm.StringS(t)
+ return strings.Join(res, ",")
+}
+
+func (tm testMIMES) StringS(t *testing.T) []string {
+ t.Helper()
+
+ res := make([]string, 0, len(tm))
+ for _, m := range tm {
+ res = append(res, string(m))
+ }
+ return res
+}
+
+func TestNegotiateFormat(t *testing.T) {
+ tests := []struct {
+ incoming []string
+ offered testMIMES
+ format string
+ }{
+ {incoming: testMIMES{AppJSON}.StringS(t), offered: testMIMES{AppJRDJSON, AppJSON}, format: "application/json"},
+ {incoming: testMIMES{AppJRDJSON}.StringS(t), offered: testMIMES{AppJRDJSON, AppJSON}, format: "application/jrd+json"},
+ {incoming: testMIMES{AppJRDJSON, AppJSON}.StringS(t), offered: testMIMES{AppJRDJSON}, format: "application/jrd+json"},
+ {incoming: testMIMES{AppJRDJSON, AppJSON}.StringS(t), offered: testMIMES{AppJSON}, format: "application/json"},
+ {incoming: testMIMES{"text/html,application/xhtml+xml,application/xml;q=0.9;q=0.8"}.StringS(t), offered: testMIMES{AppJSON, AppXML}, format: "application/xml"},
+ {incoming: testMIMES{"text/html,application/xhtml+xml,application/xml;q=0.9;q=0.8"}.StringS(t), offered: testMIMES{TextHTML, AppXML}, format: "text/html"},
+ }
+
+ for _, tt := range tests {
+ name := "incoming:" + strings.Join(tt.incoming, ",") + " offered:" + tt.offered.String(t)
+ t.Run(name, func(t *testing.T) {
+ tt := tt
+ t.Parallel()
+
+ c, _ := gin.CreateTestContext(httptest.NewRecorder())
+ c.Request = &http.Request{
+ Header: make(http.Header),
+ }
+ for _, header := range tt.incoming {
+ c.Request.Header.Add("accept", header)
+ }
+
+ format := NegotiateFormat(c, tt.offered.StringS(t)...)
+ if tt.format != format {
+ t.Fatalf("expected format: '%s', got format: '%s'", tt.format, format)
+ }
+ })
+ }
+}