-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstream_test.go
More file actions
78 lines (66 loc) · 1.77 KB
/
Copy pathstream_test.go
File metadata and controls
78 lines (66 loc) · 1.77 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
//go:build js && webtests
package stream
import (
"io"
"strings"
"syscall/js"
"testing"
)
func TestReadableStream(t *testing.T) {
t.Run("Read entire stream", func(t *testing.T) {
mockStream := newMockStream("Hello, World!")
reader := NewReadableStream(mockStream)
result, err := io.ReadAll(reader)
if err != nil {
t.Fatalf("Failed to read from BodyReader: %v", err)
}
if string(result) != "Hello, World!" {
t.Errorf("Expected 'Hello, World!', got '%s'", string(result))
}
})
t.Run("Read in chunks", func(t *testing.T) {
mockStream := newMockStream("Hello, World!")
reader := NewReadableStream(mockStream)
result := make([]byte, 0, 13)
buf := make([]byte, 5)
for {
n, err := reader.Read(buf)
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("Failed to read chunk: %v", err)
}
result = append(result, buf[:n]...)
}
if string(result) != "Hello, World!" {
t.Errorf("Expected 'Hello, World!', got '%s'", string(result))
}
})
t.Run("Close stream", func(t *testing.T) {
mockStream := newMockStream("Hello, World!")
reader := NewReadableStream(mockStream)
err := reader.Close()
if err != nil {
t.Fatalf("Failed to close BodyReader: %v", err)
}
_, err = reader.Read(make([]byte, 1))
if err != io.EOF {
t.Errorf("Expected EOF after closing, got: %v", err)
}
})
}
func newMockStream(content string) js.Value {
return js.Global().Get("ReadableStream").New(map[string]interface{}{
"start": js.FuncOf(func(this js.Value, args []js.Value) interface{} {
controller := args[0]
go func() {
for _, chunk := range strings.Split(content, "") {
controller.Call("enqueue", js.Global().Get("TextEncoder").New().Call("encode", chunk))
}
controller.Call("close")
}()
return nil
}),
})
}