Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
66 lines
2.1 KiB
Go
66 lines
2.1 KiB
Go
package upstream
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"videoweb/app/appg"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestGetPublicJSONDoesNotForwardAuthorization(t *testing.T) {
|
|
var receivedAuthorization string
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
receivedAuthorization = r.Header.Get("Authorization")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"code": 200})
|
|
}))
|
|
defer server.Close()
|
|
|
|
previousServerAddr := appg.Conf.ServerAddr
|
|
appg.Conf.ServerAddr = server.URL
|
|
t.Cleanup(func() { appg.Conf.ServerAddr = previousServerAddr })
|
|
|
|
request := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
request.AddCookie(&http.Cookie{Name: "token", Value: "expired-token"})
|
|
context, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
context.Request = request
|
|
|
|
var response map[string]any
|
|
if err := GetPublicJSON(context, "/public-config", &response); err != nil {
|
|
t.Fatalf("GetPublicJSON() error = %v", err)
|
|
}
|
|
if receivedAuthorization != "" {
|
|
t.Fatalf("Authorization = %q, want empty", receivedAuthorization)
|
|
}
|
|
}
|
|
|
|
func TestGetJSONStillForwardsAuthorization(t *testing.T) {
|
|
var receivedAuthorization string
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
receivedAuthorization = r.Header.Get("Authorization")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"code": 200})
|
|
}))
|
|
defer server.Close()
|
|
|
|
previousServerAddr := appg.Conf.ServerAddr
|
|
appg.Conf.ServerAddr = server.URL
|
|
t.Cleanup(func() { appg.Conf.ServerAddr = previousServerAddr })
|
|
|
|
request := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
request.AddCookie(&http.Cookie{Name: "token", Value: "valid-token"})
|
|
context, _ := gin.CreateTestContext(httptest.NewRecorder())
|
|
context.Request = request
|
|
|
|
var response map[string]any
|
|
if err := GetJSON(context, "/private-data", &response); err != nil {
|
|
t.Fatalf("GetJSON() error = %v", err)
|
|
}
|
|
if receivedAuthorization != "valid-token" {
|
|
t.Fatalf("Authorization = %q, want %q", receivedAuthorization, "valid-token")
|
|
}
|
|
}
|