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