forked from starfederation/datastar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestdata.go
More file actions
341 lines (276 loc) · 8.81 KB
/
testdata.go
File metadata and controls
341 lines (276 loc) · 8.81 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package sdktests
import (
"bytes"
"embed"
"flag"
"fmt"
"io"
"io/fs"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"sort"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/net/html"
)
//go:embed golden
var testData embed.FS
var serverURL string
func init() {
flag.StringVar(&serverURL, "server", "http://localhost:7331", "Test server URL")
}
// TestSSEGetEndpoints is an exported version of the GET endpoint tests
func TestSSEGetEndpoints(t *testing.T) {
runTestCases(t, testData, "golden/get", runGetTest)
}
// TestSSEPostEndpoints is an exported version of the POST endpoint tests
func TestSSEPostEndpoints(t *testing.T) {
runTestCases(t, testData, "golden/post", runPostTest)
}
func runTestCases(t *testing.T, embedFS embed.FS, casesDir string, runTest func(string, []byte) ([]byte, error)) {
entries, err := fs.ReadDir(embedFS, casesDir)
require.NoError(t, err, "Failed to read %s directory", casesDir)
// Get unique test case names
testCases := make(map[string]bool)
for _, entry := range entries {
if entry.IsDir() {
testCases[entry.Name()] = true
} else {
// Extract test case name from file path
dir := filepath.Dir(entry.Name())
if dir != "." && dir != casesDir {
testName := filepath.Base(dir)
testCases[testName] = true
}
}
}
// Run each test case
for testName := range testCases {
testName := testName // capture for closure
t.Run(testName, func(t *testing.T) {
inputPath := path.Join(casesDir, testName, "input.json")
outputPath := path.Join(casesDir, testName, "output.txt")
// Read input from embedded FS
inputData, err := embedFS.ReadFile(inputPath)
require.NoError(t, err, "Failed to read input")
// Read expected output from embedded FS
expectedData, err := embedFS.ReadFile(outputPath)
require.NoError(t, err, "Failed to read expected output")
// Run test
actualData, err := runTest(serverURL, inputData)
require.NoError(t, err, "Request failed")
// Compare
err = compareSSE(t, expectedData, actualData)
if err != nil {
// Save actual output for debugging
debugDir := filepath.Join("testdata", casesDir, testName)
os.MkdirAll(debugDir, 0755)
actualPath := filepath.Join(debugDir, "testOutput.txt")
os.WriteFile(actualPath, actualData, 0644)
t.Logf("Test case: %s", testName)
t.Logf("Actual output saved to: %s", actualPath)
}
})
}
}
func runGetTest(serverURL string, inputData []byte) ([]byte, error) {
u, err := url.Parse(serverURL + "/test")
if err != nil {
return nil, err
}
q := u.Query()
q.Set("datastar", string(inputData))
u.RawQuery = q.Encode()
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "text/event-stream")
req.Header.Set("datastar-request", "true")
return makeRequest(req)
}
func runPostTest(serverURL string, inputData []byte) ([]byte, error) {
req, err := http.NewRequest("POST", serverURL+"/test", bytes.NewReader(inputData))
if err != nil {
return nil, err
}
req.Header.Set("Accept", "text/event-stream")
req.Header.Set("datastar-request", "true")
req.Header.Set("Content-Type", "application/json")
return makeRequest(req)
}
func makeRequest(req *http.Request) ([]byte, error) {
client := &http.Client{
Timeout: 10 * time.Second,
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("server returned %d: %s", resp.StatusCode, body)
}
return io.ReadAll(resp.Body)
}
// SSE comparison functions
type SSEEvent struct {
Fields map[string][]string
}
func compareSSE(t *testing.T, expected, actual []byte) error {
expectedEvents, err := parseSSE(expected)
require.NoError(t, err, "Failed to parse expected SSE")
actualEvents, err := parseSSE(actual)
require.NoError(t, err, "Failed to parse actual SSE")
compareEvents(t, expectedEvents, actualEvents)
return nil
}
func parseSSE(data []byte) ([]SSEEvent, error) {
var events []SSEEvent
var currentEvent *SSEEvent
lines := strings.Split(string(data), "\n")
for _, line := range lines {
// Empty line marks end of event
if line == "" {
if currentEvent != nil && len(currentEvent.Fields) > 0 {
events = append(events, *currentEvent)
currentEvent = nil
}
continue
}
// Parse field
colonIndex := strings.Index(line, ":")
if colonIndex == -1 {
continue
}
if currentEvent == nil {
currentEvent = &SSEEvent{Fields: make(map[string][]string)}
}
fieldName := line[:colonIndex]
fieldValue := strings.TrimSpace(line[colonIndex+1:])
currentEvent.Fields[fieldName] = append(currentEvent.Fields[fieldName], fieldValue)
}
// Handle last event if file doesn't end with empty line
if currentEvent != nil && len(currentEvent.Fields) > 0 {
events = append(events, *currentEvent)
}
return events, nil
}
func compareEvents(t *testing.T, expected, actual []SSEEvent) {
require.Equal(t, len(expected), len(actual), "Event count mismatch")
for i := range expected {
compareEvent(t, i+1, &expected[i], &actual[i])
}
}
func compareEvent(t *testing.T, eventNum int, expected, actual *SSEEvent) {
// Compare all non-data fields first
for fieldName, expectedValues := range expected.Fields {
if fieldName == "data" {
continue
}
actualValues, ok := actual.Fields[fieldName]
assert.True(t, ok, "Event %d: missing field '%s' in actual", eventNum, fieldName)
assert.Equal(t, expectedValues, actualValues, "Event %d: mismatch in '%s' field", eventNum, fieldName)
}
// Check for extra fields in actual
for fieldName := range actual.Fields {
if fieldName == "data" {
continue
}
_, ok := expected.Fields[fieldName]
assert.True(t, ok, "Event %d: unexpected field '%s' in actual", eventNum, fieldName)
}
// Compare data fields with special handling
expectedData := expected.Fields["data"]
actualData := actual.Fields["data"]
if len(expectedData) == 0 && len(actualData) == 0 {
return
}
require.Equal(t, len(expectedData) > 0, len(actualData) > 0, "Event %d: data field presence mismatch", eventNum)
// Parse and group data fields
expectedGroups := parseDataFields(expectedData)
actualGroups := parseDataFields(actualData)
// Compare groups
compareDataGroups(t, eventNum, expectedGroups, actualGroups)
}
func parseDataFields(fields []string) map[string][]string {
groups := make(map[string][]string)
for _, field := range fields {
parts := strings.SplitN(field, " ", 2)
if len(parts) >= 1 {
subgroup := parts[0]
content := ""
if len(parts) > 1 {
content = parts[1]
}
groups[subgroup] = append(groups[subgroup], content)
}
}
return groups
}
func compareDataGroups(t *testing.T, eventNum int, expected, actual map[string][]string) {
// Check all expected groups exist in actual
for subgroup, expectedLines := range expected {
actualLines, ok := actual[subgroup]
assert.True(t, ok, "Event %d: missing data subgroup '%s' in actual", eventNum, subgroup)
// Special handling for "elements" subgroup - normalize HTML
if subgroup == "elements" {
expectedHTML := strings.Join(expectedLines, "\n")
actualHTML := strings.Join(actualLines, "\n")
normalizedExpected := normalizeHTML(expectedHTML)
normalizedActual := normalizeHTML(actualHTML)
assert.Equal(t, normalizedExpected, normalizedActual,
"Event %d: mismatch in data 'elements' content\nExpected:\n%s\nActual:\n%s",
eventNum, expectedHTML, actualHTML)
} else {
// For non-elements, exact match
assert.Equal(t, expectedLines, actualLines,
"Event %d: mismatch in data '%s' content", eventNum, subgroup)
}
}
// Check for extra subgroups in actual
for subgroup := range actual {
_, ok := expected[subgroup]
assert.True(t, ok, "Event %d: unexpected data subgroup '%s' in actual", eventNum, subgroup)
}
}
func normalizeHTML(htmlStr string) string {
doc, err := html.Parse(strings.NewReader(htmlStr))
if err != nil {
// If parsing fails, return original
return htmlStr
}
// Normalize attributes in all element nodes
var normalize func(*html.Node)
normalize = func(n *html.Node) {
if n.Type == html.ElementNode && n.Attr != nil && len(n.Attr) > 1 {
// Sort attributes by key
sort.Slice(n.Attr, func(i, j int) bool {
return n.Attr[i].Key < n.Attr[j].Key
})
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
normalize(c)
}
}
normalize(doc)
// Render back to string
var buf bytes.Buffer
html.Render(&buf, doc)
// The parser adds <html><head></head><body>...</body></html>, so extract just the body content
result := buf.String()
// Extract content between <body> and </body>
bodyStart := strings.Index(result, "<body>")
bodyEnd := strings.Index(result, "</body>")
if bodyStart != -1 && bodyEnd != -1 {
result = result[bodyStart+6 : bodyEnd]
}
return strings.TrimSpace(result)
}