forked from mrlauer/gofcgisrv
-
Notifications
You must be signed in to change notification settings - Fork 3
/
streams_test.go
71 lines (64 loc) · 1.5 KB
/
streams_test.go
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
package gofcgisrv
import (
"bytes"
"io"
"io/ioutil"
"strings"
"testing"
"time"
)
func TestStreamReader(t *testing.T) {
sr := newStreamReader()
io.WriteString(sr, "abc")
data := make([]byte, 1024)
n, err := sr.Read(data)
if err != nil {
t.Errorf("Read error %v", err)
}
if string(data[:n]) != "abc" {
t.Errorf("Read %s, not %s", data[:n], "abc")
}
ch := make(chan string)
go func() {
n, err := sr.Read(data)
if err != nil {
t.Error(err)
}
ch <- string(data[:n])
}()
// Let the goroutine execute a bit
time.Sleep(time.Millisecond)
go func() {
sr.Write([]byte("ABCD"))
}()
str := <-ch
if str != "ABCD" {
t.Errorf("Read %s, not %s", str, "ABCD")
}
ss := []string{"foo", "bar", strings.Repeat("xyzå", 100)}
for _, s := range ss {
sr.Write([]byte(s))
}
go sr.Close()
ssj := strings.Join(ss, "")
read, err := ioutil.ReadAll(sr)
if err != nil || string(read) != ssj {
t.Errorf("Read %s with error %v", read, err)
}
}
func TestStreamWriter(t *testing.T) {
var buffer bytes.Buffer
sw := newStreamWriter(&buffer, fcgiStdout, 3)
io.WriteString(sw, "Foo!")
io.WriteString(sw, "This is data")
io.WriteString(sw, "\000\001abc")
sw.Close()
expected := "\001\006\000\003\000\004\004\000Foo!\000\000\000\000" +
"\001\006\000\003\000\x0c\004\000This is data\000\000\000\000" +
"\001\006\000\003\000\x05\003\000\000\001abc\000\000\000" +
"\001\006\000\003\000\x00\000\000"
str := string(buffer.Bytes())
if str != expected {
t.Errorf("Got\n%q\nnot\n%q", str, expected)
}
}