Live video on the AT Protocol
1package api
2
3import (
4 "context"
5 "net/http"
6 "net/http/httptest"
7 "runtime"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 "stream.place/streamplace/pkg/config"
13 "stream.place/streamplace/pkg/log"
14 "stream.place/streamplace/pkg/model"
15 "stream.place/streamplace/pkg/notifications"
16)
17
18func TestRedirectHandler(t *testing.T) {
19 tests := []struct {
20 name string
21 httpAddr string
22 httpsAddr string
23 requestURL string
24 expectedURL string
25 }{
26 {
27 name: "default https port",
28 httpAddr: "0.0.0.0:80",
29 httpsAddr: "0.0.0.0:443",
30 requestURL: "http://example.com/",
31 expectedURL: "https://example.com/",
32 },
33 {
34 name: "non-default https port",
35 httpAddr: "0.0.0.0:80",
36 httpsAddr: "0.0.0.0:8443",
37 requestURL: "http://example.com/",
38 expectedURL: "https://example.com:8443/",
39 },
40 {
41 name: "non-default http port",
42 httpAddr: "0.0.0.0:8080",
43 httpsAddr: "0.0.0.0:443",
44 requestURL: "http://example.com:8080/",
45 expectedURL: "https://example.com/",
46 },
47 {
48 name: "non-default both",
49 httpAddr: "0.0.0.0:8080",
50 httpsAddr: "0.0.0.0:8443",
51 requestURL: "http://example.com:8080/",
52 expectedURL: "https://example.com:8443/",
53 },
54 }
55
56 for _, tt := range tests {
57 t.Run(tt.name, func(t *testing.T) {
58 cli := &config.CLI{HTTPAddr: tt.httpAddr, HTTPSAddr: tt.httpsAddr}
59 mod := &model.DBModel{}
60 a := StreamplaceAPI{CLI: cli, Model: mod}
61
62 handler, err := a.RedirectHandler(context.Background())
63 assert.NoError(t, err, "RedirectHandler should not return an error")
64
65 req := httptest.NewRequest("GET", tt.requestURL, nil)
66 rr := httptest.NewRecorder()
67
68 handler.ServeHTTP(rr, req)
69
70 result := rr.Result()
71 assert.Equal(t, http.StatusTemporaryRedirect, result.StatusCode, "handler returned wrong status code")
72
73 redirectURL, err := result.Location()
74 assert.NoError(t, err, "Failed to get redirect location")
75
76 assert.Equal(t, tt.expectedURL, redirectURL.String(), "handler returned unexpected redirect URL")
77 })
78 }
79}
80
81type MockFirebase struct {
82}
83
84func (m *MockFirebase) Blast(ctx context.Context, nots []string, nb *notifications.NotificationBlast) error {
85 return nil
86}
87
88func TestContextMiddleware(t *testing.T) {
89 cli := &config.CLI{HTTPAddr: "0.0.0.0:80", HTTPSAddr: "0.0.0.0:443"}
90 mod := &model.DBModel{}
91 a := StreamplaceAPI{CLI: cli, Model: mod}
92
93 runtime.GC()
94
95 requestID := ""
96 lastContext := context.Background()
97 handler := a.ContextMiddleware(context.Background())(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
98 requestID = log.GetValue(r.Context(), "requestID")
99 lastContext = r.Context()
100 }))
101
102 rr := httptest.NewRecorder()
103 req := httptest.NewRequest("GET", "/", nil)
104 for i := 0; i < 1000000; i++ {
105 handler.ServeHTTP(rr, req)
106 require.NotEmpty(t, requestID, "requestID should not be empty")
107 }
108 var m1, m2 runtime.MemStats
109 runtime.GC()
110 runtime.ReadMemStats(&m1)
111 runtime.KeepAlive(lastContext)
112 lastContext = context.Background()
113 runtime.GC()
114 runtime.ReadMemStats(&m2)
115 memoryRatio := float64(m1.Alloc) / float64(m2.Alloc)
116 require.Less(t, memoryRatio, 5.0, "memory allocated should be less than 5x")
117}