-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpcclient_test.go
75 lines (70 loc) · 1.97 KB
/
rpcclient_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
package xrpc
import (
"context"
"fmt"
pb "github.com/mix-go/xrpc/api"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"io"
"log"
"net/http"
"os"
"strings"
"testing"
)
func TestNewGrpcClient(t *testing.T) {
conn, err := NewGrpcClient("127.0.0.1:50000")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
client := pb.NewAppMessagesClient(conn)
ctx, _ := context.WithTimeout(context.Background(), CallTimeout)
resp, err := client.Send(ctx, &pb.SendRequest{
Text: "123456789",
})
fmt.Println(resp, err)
}
func TestNewGrpcTLSClient(t *testing.T) {
dir, _ := os.Getwd()
tlsConf, err := LoadClientTLSConfig(dir+"/certificates/ca.pem", dir+"/certificates/client.pem", dir+"/certificates/client.key")
if err != nil {
log.Fatal(err)
}
conn, err := NewGrpcClient("127.0.0.1:50000", grpc.WithTransportCredentials(credentials.NewTLS(tlsConf)))
if err != nil {
log.Fatal(err)
}
defer conn.Close()
client := pb.NewAppMessagesClient(conn)
ctx, _ := context.WithTimeout(context.Background(), CallTimeout)
resp, err := client.Send(ctx, &pb.SendRequest{
Text: "123456789",
})
fmt.Println(resp, err)
}
func TestNewGatewayClient(t *testing.T) {
client := &http.Client{}
resp, err := client.Post("http://127.0.0.1:50001/v1/send_message", "application/json", strings.NewReader(`{"order_number":"123456789"}`))
if err != nil {
log.Fatal(err)
}
b, _ := io.ReadAll(resp.Body)
fmt.Println(string(b), err)
}
func TestNewGatewayTLSClient(t *testing.T) {
dir, _ := os.Getwd()
tlsConf, err := LoadClientTLSConfig(dir+"/certificates/ca.pem", dir+"/certificates/client.pem", dir+"/certificates/client.key")
if err != nil {
log.Fatal(err)
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConf,
},
}
defer client.CloseIdleConnections()
resp, err := client.Post("https://127.0.0.1:50001/v1/send_message", "application/json", strings.NewReader(`{"order_number":"123456789"}`))
b, _ := io.ReadAll(resp.Body)
fmt.Println(string(b), err)
}