forked from mrlauer/gofcgisrv
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server_test.go
60 lines (54 loc) · 1.16 KB
/
server_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
package gofcgisrv
import (
"bytes"
"io"
"net"
"net/http"
"net/http/fcgi"
"net/http/httptest"
"strings"
"testing"
)
func serve(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "FCGI!\n")
if r.Body != nil {
io.Copy(w, r.Body)
r.Body.Close()
}
}
func startFCGIApp(t *testing.T, addr string) (net.Listener, error) {
l, err := net.Listen("tcp", addr)
if err != nil {
t.Fatal(err)
}
go fcgi.Serve(l, http.HandlerFunc(serve))
return l, nil
}
func TestFCGI(t *testing.T) {
addr := "127.0.0.1:9000"
l, err := startFCGIApp(t, addr)
if err != nil {
t.Fatal(err)
}
defer l.Close()
// Now start an http server.
s := NewFCGI("tcp", addr)
http.Handle("/", s)
server := httptest.NewServer(nil)
defer server.Close()
url := server.URL
resp, err := http.Post(url+"/", "text/plain", strings.NewReader("This is a string!\n"))
if err != nil {
t.Error(err)
}
if resp.StatusCode != 200 {
t.Errorf("Response had status code %d\n", resp.StatusCode)
}
buffer := bytes.NewBuffer(nil)
io.Copy(buffer, resp.Body)
resp.Body.Close()
body := string(buffer.Bytes())
if body != "FCGI!\nThis is a string!\n" {
t.Errorf("Response was %s\n", body)
}
}