forked from spacemeshos/go-spacemesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.go
129 lines (98 loc) · 3.58 KB
/
api_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
package api
import (
"fmt"
"github.com/golang/protobuf/jsonpb"
config "github.com/spacemeshos/go-spacemesh/api/config"
pb "github.com/spacemeshos/go-spacemesh/api/pb"
"github.com/spacemeshos/go-spacemesh/assert"
"github.com/spacemeshos/go-spacemesh/crypto"
"golang.org/x/net/context"
"google.golang.org/grpc"
"io/ioutil"
"net/http"
"strconv"
"strings"
"testing"
"time"
)
func TestServersConfig(t *testing.T) {
config.ConfigValues.GrpcServerPort = int(crypto.GetRandomUserPort())
config.ConfigValues.JSONServerPort = int(crypto.GetRandomUserPort())
grpcService := NewGrpcService()
jsonService := NewJSONHTTPServer()
assert.Equal(t, grpcService.Port, uint(config.ConfigValues.GrpcServerPort), "Expected same port")
assert.Equal(t, jsonService.Port, uint(config.ConfigValues.JSONServerPort), "Expected same port")
}
func TestGrpcApi(t *testing.T) {
config.ConfigValues.GrpcServerPort = int(crypto.GetRandomUserPort())
config.ConfigValues.JSONServerPort = int(crypto.GetRandomUserPort())
const message = "Hello World"
grpcService := NewGrpcService()
// start a server
grpcService.StartService(nil)
// start a client
addr := "localhost:" + strconv.Itoa(int(config.ConfigValues.GrpcServerPort))
// Set up a connection to the server.
conn, err := grpc.Dial(addr, grpc.WithInsecure())
if err != nil {
t.Fatalf("did not connect. %v", err)
}
defer conn.Close()
c := pb.NewSpaceMeshServiceClient(conn)
// call echo and validate result
r, err := c.Echo(context.Background(), &pb.SimpleMessage{Value: message})
if err != nil {
t.Fatalf("could not greet. %v", err)
}
assert.Equal(t, message, r.Value, "Expected message to be echoed")
// stop the server
grpcService.StopService()
}
func TestJsonApi(t *testing.T) {
config.ConfigValues.GrpcServerPort = int(crypto.GetRandomUserPort())
config.ConfigValues.JSONServerPort = int(crypto.GetRandomUserPort())
grpcService := NewGrpcService()
jsonService := NewJSONHTTPServer()
jsonStatus := make(chan bool, 2)
grpcStatus := make(chan bool, 2)
// start grp and json server
grpcService.StartService(grpcStatus)
<-grpcStatus
jsonService.StartService(jsonStatus)
<-jsonStatus
const message = "hello world!"
const contentType = "application/json"
// generate request payload (api input params)
reqParams := pb.SimpleMessage{Value: message}
var m jsonpb.Marshaler
payload, err := m.MarshalToString(&reqParams)
assert.NoErr(t, err, "failed to marshal to string")
// Without this running this on Travis CI might generate a connection refused error
// because the server may not be ready to accept connections just yet.
time.Sleep(3 * time.Second)
url := fmt.Sprintf("http://127.0.0.1:%d/v1/example/echo", config.ConfigValues.JSONServerPort)
resp, err := http.Post(url, contentType, strings.NewReader(payload))
assert.NoErr(t, err, "failed to http post to api endpoint")
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
assert.NoErr(t, err, "failed to read response body")
if got, want := resp.StatusCode, http.StatusOK; got != want {
t.Errorf("resp.StatusCode = %d; want %d", got, want)
}
var msg pb.SimpleMessage
if err := jsonpb.UnmarshalString(string(buf), &msg); err != nil {
t.Errorf("jsonpb.UnmarshalString(%s, &msg) failed with %v; want success", buf, err)
return
}
if got, want := msg.Value, message; got != want {
t.Errorf("msg.Value = %q; want %q", got, want)
}
if value := resp.Header.Get("Content-Type"); value != contentType {
t.Errorf("Content-Type was %s, wanted %s", value, contentType)
}
// stop the services
jsonService.StopService()
<-jsonStatus
grpcService.StopService()
<-grpcStatus
}