Repository navigation
Expand file tree
/
Copy pathmain_test.go
More file actions
137 lines (111 loc) · 3.48 KB
/
Copy pathmain_test.go
File metadata and controls
137 lines (111 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"context"
"io"
"net"
"net/http"
"testing"
"time"
"github.com/MAX-API-Next/MAX-API/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRunWithTimeoutCompletes(t *testing.T) {
assert.True(t, runWithTimeout(time.Second, func() {}))
}
func TestRunWithTimeoutTimesOut(t *testing.T) {
start := time.Now()
assert.False(t, runWithTimeout(10*time.Millisecond, func() {
time.Sleep(500 * time.Millisecond)
}))
assert.Less(t, time.Since(start), 250*time.Millisecond)
}
func TestRunWithTimeoutRecoversPanic(t *testing.T) {
assert.True(t, runWithTimeout(5*time.Second, func() {
panic("quota save failed")
}))
}
func TestRunWithContextRecoversPanic(t *testing.T) {
err := runWithContext(context.Background(), func(context.Context) error {
panic("quota save failed")
})
require.EqualError(t, err, "runWithContext: recovered panic: quota save failed")
}
func TestShutdownHTTPServerClosesActiveHandlersAfterTimeout(t *testing.T) {
handlerStarted := make(chan struct{})
handlerDone := make(chan struct{})
server := &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
close(handlerStarted)
<-r.Context().Done()
close(handlerDone)
}),
}
t.Cleanup(func() {
_ = server.Close()
})
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
serveDone := make(chan error, 1)
go func() {
serveDone <- server.Serve(listener)
}()
client := &http.Client{Timeout: time.Second}
clientDone := make(chan error, 1)
go func() {
resp, err := client.Get("http://" + listener.Addr().String())
if resp != nil {
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
}
clientDone <- err
}()
select {
case <-handlerStarted:
case <-time.After(time.Second):
t.Fatal("handler did not start")
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
shutdownHTTPServer(ctx, server)
select {
case <-handlerDone:
case <-time.After(250 * time.Millisecond):
t.Fatal("active handler was not force-closed after shutdown timeout")
}
select {
case err := <-serveDone:
assert.ErrorIs(t, err, http.ErrServerClosed)
case <-time.After(250 * time.Millisecond):
t.Fatal("server did not stop after forced close")
}
select {
case <-clientDone:
case <-time.After(time.Second):
t.Fatal("client request did not finish after forced close")
}
}
func TestConfigureSessionCookieSecureTreatsBlankAsUnset(t *testing.T) {
oldSecure := common.SessionCookieSecure
t.Cleanup(func() { common.SessionCookieSecure = oldSecure })
t.Setenv("SESSION_COOKIE_SECURE", "")
require.NoError(t, configureSessionCookieSecure("https://example.com"))
assert.True(t, common.SessionCookieSecure)
}
func TestConfigureSessionCookieSecurePreservesExplicitFalse(t *testing.T) {
oldSecure := common.SessionCookieSecure
t.Cleanup(func() { common.SessionCookieSecure = oldSecure })
t.Setenv("SESSION_COOKIE_SECURE", "false")
require.NoError(t, configureSessionCookieSecure("https://example.com"))
assert.False(t, common.SessionCookieSecure)
}
func TestConfigureSessionCookieSecureRejectsInvalidValue(t *testing.T) {
t.Setenv("SESSION_COOKIE_SECURE", "sometimes")
require.Error(t, configureSessionCookieSecure("https://example.com"))
}
func TestSessionCookieOptionsAllowOAuthTopLevelCallback(t *testing.T) {
options := sessionCookieOptions()
assert.Equal(t, http.SameSiteLaxMode, options.SameSite)
assert.True(t, options.HttpOnly)
assert.Equal(t, "/", options.Path)
}