-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
168 lines (143 loc) · 4.71 KB
/
http_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
package rpc25519
import (
"strings"
cv "github.com/glycerine/goconvey/convey"
"testing"
)
// Same as 006 in cli_test.go but sets cfg.HTTPConnectRequired = true
func Test024_RoundTrip_Using_NetRPC_API_TCP_and_http_CONNECT(t *testing.T) {
cv.Convey("http CONNECT + tcp hijack => basic TCP with rpc25519 using the net/rpc API: register a callback on the server, and have the client call it.", t, func() {
for i := 0; i < 2; i++ {
cfg := NewConfig()
cfg.HTTPConnectRequired = true
switch i {
case 0:
cfg.TCPonly_no_TLS = true
case 1:
cfg.TCPonly_no_TLS = false
}
path := GetPrivateCertificateAuthDir() + sep + "psk.binary"
panicOn(setupPSK(path))
cfg.PreSharedKeyPath = path
//cfg.ReadTimeout = 2 * time.Second
//cfg.WriteTimeout = 2 * time.Second
cfg.ServerAddr = "127.0.0.1:0"
srv := NewServer("srv_test024", cfg)
serverAddr, err := srv.Start()
panicOn(err)
defer srv.Close()
vv("server Start() returned serverAddr = '%v'", serverAddr)
// net/rpc API on server
srv.Register(new(Arith))
srv.Register(new(Embed))
srv.RegisterName("net.rpc.Arith", new(Arith))
srv.Register(BuiltinTypes{})
cfg.ClientDialToHostPort = serverAddr.String()
client, err := NewClient("test024", cfg)
panicOn(err)
err = client.Start()
panicOn(err)
defer client.Close()
// net/rpc API on client, ported from attic/net_server_test.go
var args *Args
_ = args
var reply *Reply
// Synchronous calls
args = &Args{7, 8}
reply = new(Reply)
err = client.Call("Arith.Add", args, reply, nil)
if err != nil {
t.Errorf("Add: expected no error but got string %q", err.Error())
}
if reply.C != args.A+args.B {
t.Errorf("Add: expected %d got %d", reply.C, args.A+args.B)
}
vv("good 024, got back reply '%#v'", reply)
// Methods exported from unexported embedded structs
args = &Args{7, 0}
reply = new(Reply)
err = client.Call("Embed.Exported", args, reply, nil)
if err != nil {
t.Errorf("Add: expected no error but got string %q", err.Error())
}
if reply.C != args.A+args.B {
t.Errorf("Add: expected %d got %d", reply.C, args.A+args.B)
}
// Nonexistent method
args = &Args{7, 0}
reply = new(Reply)
err = client.Call("Arith.BadOperation", args, reply, nil)
// expect an error
if err == nil {
t.Error("BadOperation: expected error")
} else if !strings.HasPrefix(err.Error(), "rpc: can't find method ") {
t.Errorf("BadOperation: expected can't find method error; got %q", err)
}
vv("good 024: past nonexistent method")
// Unknown service
args = &Args{7, 8}
reply = new(Reply)
err = client.Call("Arith.Unknown", args, reply, nil)
if err == nil {
t.Error("expected error calling unknown service")
} else if !strings.Contains(err.Error(), "method") {
t.Error("expected error about method; got", err)
}
vv("good 024: past unknown service")
// Out of order.
args = &Args{7, 8}
mulReply := new(Reply)
mulCall := client.Go("Arith.Mul", args, mulReply, nil, nil)
addReply := new(Reply)
addCall := client.Go("Arith.Add", args, addReply, nil, nil)
addCall = <-addCall.Done
if addCall.Error != nil {
t.Errorf("Add: expected no error but got string %q", addCall.Error.Error())
}
if addReply.C != args.A+args.B {
t.Errorf("Add: expected %d got %d", addReply.C, args.A+args.B)
}
mulCall = <-mulCall.Done
if mulCall.Error != nil {
t.Errorf("Mul: expected no error but got string %q", mulCall.Error.Error())
}
if mulReply.C != args.A*args.B {
t.Errorf("Mul: expected %d got %d", mulReply.C, args.A*args.B)
}
vv("good 024: past out of order")
// Error test
args = &Args{7, 0}
reply = new(Reply)
err = client.Call("Arith.Div", args, reply, nil)
// expect an error: zero divide
if err == nil {
t.Error("Div: expected error")
} else if err.Error() != "divide by zero" {
t.Error("Div: expected divide by zero error; got", err)
}
vv("good 024: past error test")
args = &Args{7, 8}
reply = new(Reply)
err = client.Call("Arith.Mul", args, reply, nil)
if err != nil {
t.Errorf("Mul: expected no error but got string %q", err.Error())
}
if reply.C != args.A*args.B {
t.Errorf("Mul: expected %d got %d", reply.C, args.A*args.B)
}
vv("good 024: past Arith.Mul test")
// ServiceName contain "." character
args = &Args{7, 8}
reply = new(Reply)
err = client.Call("net.rpc.Arith.Add", args, reply, nil)
if err != nil {
t.Errorf("Add: expected no error but got string %q", err.Error())
}
if reply.C != args.A+args.B {
t.Errorf("Add: expected %d got %d", reply.C, args.A+args.B)
}
vv("good 024: past ServiceName with dot . test")
cv.So(true, cv.ShouldBeTrue)
}
})
}