Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
3.2 kB · 117 lines
Go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118package api
import ( "context" "net/http" "net/http/httptest" "runtime" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "stream.place/streamplace/pkg/config" "stream.place/streamplace/pkg/log" "stream.place/streamplace/pkg/model" "stream.place/streamplace/pkg/notifications")
func TestRedirectHandler(t *testing.T) { tests := []struct { name string httpAddr string httpsAddr string requestURL string expectedURL string }{ { name: "default https port", httpAddr: "0.0.0.0:80", httpsAddr: "0.0.0.0:443", requestURL: "http://example.com/", expectedURL: "https://example.com/", }, { name: "non-default https port", httpAddr: "0.0.0.0:80", httpsAddr: "0.0.0.0:8443", requestURL: "http://example.com/", expectedURL: "https://example.com:8443/", }, { name: "non-default http port", httpAddr: "0.0.0.0:8080", httpsAddr: "0.0.0.0:443", requestURL: "http://example.com:8080/", expectedURL: "https://example.com/", }, { name: "non-default both", httpAddr: "0.0.0.0:8080", httpsAddr: "0.0.0.0:8443", requestURL: "http://example.com:8080/", expectedURL: "https://example.com:8443/", }, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { cli := &config.CLI{HTTPAddr: tt.httpAddr, HTTPSAddr: tt.httpsAddr} mod := &model.DBModel{} a := StreamplaceAPI{CLI: cli, Model: mod}
handler, err := a.RedirectHandler(context.Background()) assert.NoError(t, err, "RedirectHandler should not return an error")
req := httptest.NewRequest("GET", tt.requestURL, nil) rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
result := rr.Result() assert.Equal(t, http.StatusTemporaryRedirect, result.StatusCode, "handler returned wrong status code")
redirectURL, err := result.Location() assert.NoError(t, err, "Failed to get redirect location")
assert.Equal(t, tt.expectedURL, redirectURL.String(), "handler returned unexpected redirect URL") }) }}
type MockFirebase struct {}
func (m *MockFirebase) Blast(ctx context.Context, targets []notifications.NotificationTarget, nb *notifications.NotificationBlast) error { return nil}
func TestContextMiddleware(t *testing.T) { cli := &config.CLI{HTTPAddr: "0.0.0.0:80", HTTPSAddr: "0.0.0.0:443"} mod := &model.DBModel{} a := StreamplaceAPI{CLI: cli, Model: mod}
runtime.GC()
requestID := "" lastContext := context.Background() handler := a.ContextMiddleware(context.Background())(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { requestID = log.GetValue(r.Context(), "requestID") lastContext = r.Context() }))
rr := httptest.NewRecorder() req := httptest.NewRequest("GET", "/", nil) for i := 0; i < 1000000; i++ { handler.ServeHTTP(rr, req) require.NotEmpty(t, requestID, "requestID should not be empty") } var m1, m2 runtime.MemStats runtime.GC() runtime.ReadMemStats(&m1) runtime.KeepAlive(lastContext) lastContext = context.Background() runtime.GC() runtime.ReadMemStats(&m2) memoryRatio := float64(m1.Alloc) / float64(m2.Alloc) require.Less(t, memoryRatio, 5.0, "memory allocated should be less than 5x")}