-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptions_test.go
More file actions
109 lines (101 loc) · 2.49 KB
/
options_test.go
File metadata and controls
109 lines (101 loc) · 2.49 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
package sse
import (
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestOptions(t *testing.T) {
tests := []struct {
name string
option Option
expected func(c *config) bool
}{
{
name: "WithHeartbeatInterval",
option: WithHeartbeatInterval(30 * time.Second),
expected: func(c *config) bool {
return c.heartbeatInterval == 30*time.Second
},
},
{
name: "WithHeartbeatInterval_Zero",
option: WithHeartbeatInterval(0),
expected: func(c *config) bool {
return c.heartbeatInterval == 0
},
},
{
name: "WithHeaders",
option: WithHeaders(http.Header{"X-Test": []string{"test-value"}}),
expected: func(c *config) bool {
return c.headers.Get("X-Test") == "test-value"
},
},
{
name: "WithHeartbeatComment",
option: WithHeartbeatComment("test-comment"),
expected: func(c *config) bool {
return c.heartbeatComment == "test-comment"
},
},
{
name: "WithStatus",
option: WithStatus(http.StatusAccepted),
expected: func(c *config) bool {
return c.status == http.StatusAccepted
},
},
{
name: "WithRetryDelay",
option: WithRetryDelay(5 * time.Second),
expected: func(c *config) bool {
return c.retryDelay == 5*time.Second
},
},
{
name: "WithRetryDelay_Zero",
option: WithRetryDelay(0),
expected: func(c *config) bool {
return c.retryDelay == 0
},
},
{
name: "WithWriteTimeout",
option: WithWriteTimeout(10 * time.Second),
expected: func(c *config) bool {
return c.writeTimeout == 10*time.Second
},
},
{
name: "WithCloseMessage",
option: WithCloseMessage(&Event{
Data: "closing connection",
}),
expected: func(c *config) bool {
return c.closeMessage != nil && c.closeMessage.Data == "closing connection"
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Start with default config
c := defaultConfig()
// Apply the option under test
tt.option(&c)
// Verify the option modified the config as expected
assert.True(t, tt.expected(&c), "Option did not set config as expected")
})
}
}
func TestDefaultConfig(t *testing.T) {
c := defaultConfig()
// Verify default values
assert.Equal(t, 15*time.Second, c.heartbeatInterval)
assert.Equal(t, "keep-alive", c.heartbeatComment)
assert.Equal(t, http.StatusOK, c.status)
assert.Equal(t, 3*time.Second, c.retryDelay)
assert.Equal(t, 5*time.Second, c.writeTimeout)
assert.Nil(t, c.closeMessage)
assert.NotNil(t, c.headers)
}