Live video on the AT Protocol
1package api 2 3import ( 4 "context" 5 "net/http" 6 "net/http/httptest" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "stream.place/streamplace/pkg/config" 11 "stream.place/streamplace/pkg/model" 12 "stream.place/streamplace/pkg/notifications" 13) 14 15func TestRedirectHandler(t *testing.T) { 16 tests := []struct { 17 name string 18 httpAddr string 19 httpsAddr string 20 requestURL string 21 expectedURL string 22 }{ 23 { 24 name: "default https port", 25 httpAddr: "0.0.0.0:80", 26 httpsAddr: "0.0.0.0:443", 27 requestURL: "http://example.com/", 28 expectedURL: "https://example.com/", 29 }, 30 { 31 name: "non-default https port", 32 httpAddr: "0.0.0.0:80", 33 httpsAddr: "0.0.0.0:8443", 34 requestURL: "http://example.com/", 35 expectedURL: "https://example.com:8443/", 36 }, 37 { 38 name: "non-default http port", 39 httpAddr: "0.0.0.0:8080", 40 httpsAddr: "0.0.0.0:443", 41 requestURL: "http://example.com:8080/", 42 expectedURL: "https://example.com/", 43 }, 44 { 45 name: "non-default both", 46 httpAddr: "0.0.0.0:8080", 47 httpsAddr: "0.0.0.0:8443", 48 requestURL: "http://example.com:8080/", 49 expectedURL: "https://example.com:8443/", 50 }, 51 } 52 53 for _, tt := range tests { 54 t.Run(tt.name, func(t *testing.T) { 55 cli := &config.CLI{HTTPAddr: tt.httpAddr, HTTPSAddr: tt.httpsAddr} 56 mod := &model.DBModel{} 57 a := StreamplaceAPI{CLI: cli, Model: mod} 58 59 handler, err := a.RedirectHandler(context.Background()) 60 assert.NoError(t, err, "RedirectHandler should not return an error") 61 62 req := httptest.NewRequest("GET", tt.requestURL, nil) 63 rr := httptest.NewRecorder() 64 65 handler.ServeHTTP(rr, req) 66 67 result := rr.Result() 68 assert.Equal(t, http.StatusTemporaryRedirect, result.StatusCode, "handler returned wrong status code") 69 70 redirectURL, err := result.Location() 71 assert.NoError(t, err, "Failed to get redirect location") 72 73 assert.Equal(t, tt.expectedURL, redirectURL.String(), "handler returned unexpected redirect URL") 74 }) 75 } 76} 77 78type MockFirebase struct { 79} 80 81func (m *MockFirebase) Blast(ctx context.Context, nots []string, nb *notifications.NotificationBlast) error { 82 return nil 83}