-
Notifications
You must be signed in to change notification settings - Fork 21
/
server_test.go
191 lines (172 loc) · 4.02 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
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
// Copyright 2013-14, Amahi. All rights reserved.
// Use of this source code is governed by the
// license that can be found in the LICENSE file.
// friendly API tests
package spdy
import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"testing"
"time"
)
const SERVER_CERTFILE = "cert/serverTLS/server.pem"
const SERVER_KEYFILE = "cert/serverTLS/server.key"
const CLIENT_CERTFILE = "cert/clientTLS/client.pem"
const CLIENT_KEYFILE = "cert/clientTLS/client.key"
func init() {
SetLog(ioutil.Discard)
}
//handler for requests
func ServerHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}
func StartServer(server *Server, done chan<- error) {
err := server.ListenAndServe()
if err != nil {
done <- err
}
done <- errors.New("")
}
func testclient(done chan<- error) {
//make client
client, err := NewClient("localhost:4040")
if err != nil {
done <- err
}
for i := 0; i < 100; i++ {
//now send requests and test
req, err := http.NewRequest("GET", "http://localhost:4040/banana", nil)
if err != nil {
done <- err
}
res, err := client.Do(req)
if err != nil {
done <- err
}
data := make([]byte, int(res.ContentLength))
_, err = res.Body.(io.Reader).Read(data)
if string(data) != "Hi there, I love banana!" {
done <- err
}
res.Body.Close()
//another request
req, err = http.NewRequest("POST", "http://localhost:4040/monkeys", bytes.NewBufferString("hello=world"))
if err != nil {
done <- err
}
res, err = client.Do(req)
if err != nil {
done <- err
}
data = make([]byte, int(res.ContentLength))
_, err = res.Body.(io.Reader).Read(data)
if string(data) != "Hi there, I love monkeys!" {
done <- err
}
res.Body.Close()
}
//close client
err = client.Close()
if err != nil {
done <- err
}
done <- errors.New("")
}
//simple server with many clients sending requests to it
func TestSimpleServerClient(t *testing.T) {
//make server
mux := http.NewServeMux()
mux.HandleFunc("/", ServerHandler)
server := &Server{
Addr: "localhost:4040",
Handler: mux,
}
serverdone := make(chan error)
go StartServer(server, serverdone)
time.Sleep(100 * time.Millisecond)
cldone1 := make(chan error)
cldone2 := make(chan error)
cldone3 := make(chan error)
cldone4 := make(chan error)
cldone5 := make(chan error)
go testclient(cldone1)
go testclient(cldone2)
go testclient(cldone3)
go testclient(cldone4)
go testclient(cldone5)
err := <-cldone1
if err.Error() != "" {
t.Fatal(err.Error())
}
err = <-cldone2
if err.Error() != "" {
t.Fatal(err.Error())
}
err = <-cldone3
if err.Error() != "" {
t.Fatal(err.Error())
}
err = <-cldone4
if err.Error() != "" {
t.Fatal(err.Error())
}
err = <-cldone5
if err.Error() != "" {
t.Fatal(err.Error())
}
server.Close()
time.Sleep(100 * time.Millisecond)
err = <-serverdone
}
func TestTLSServerSpdyOnly(t *testing.T) {
//make server
mux := http.NewServeMux()
mux.HandleFunc("/", ServerHandler)
server := &Server{
Addr: "localhost:4040",
Handler: mux,
}
go server.ListenAndServeTLSSpdyOnly(SERVER_CERTFILE, SERVER_KEYFILE)
time.Sleep(400 * time.Millisecond)
//client
cert, err := tls.LoadX509KeyPair(CLIENT_CERTFILE, CLIENT_KEYFILE)
if err != nil {
t.Fatal(err.Error())
}
config := tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true}
conn, err := tls.Dial("tcp", "127.0.0.1:4040", &config)
if err != nil {
t.Fatal(err.Error())
}
client, err := NewClientConn(conn)
if err != nil {
t.Fatal(err.Error())
}
req, err := http.NewRequest("GET", "http://localhost:4040/banana", nil)
if err != nil {
t.Fatal(err.Error())
}
res, err := client.Do(req)
if err != nil {
t.Fatal(err.Error())
}
data := make([]byte, int(res.ContentLength))
_, err = res.Body.(io.Reader).Read(data)
if string(data) != "Hi there, I love banana!" {
t.Fatal("Unexpected Data")
}
res.Body.Close()
//close client
err = client.Close()
if err != nil {
t.Fatal(err.Error())
}
//server close
server.Close()
time.Sleep(100 * time.Millisecond)
}