114 lines
2.1 KiB
Go
114 lines
2.1 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"flag"
|
|
"fmt"
|
|
pb "git.loafle.net/overflow/overflow_api_service/grpc"
|
|
"git.loafle.net/overflow/overflow_gateway_service/gateway"
|
|
"github.com/golang/glog"
|
|
"golang.org/x/net/context"
|
|
"google.golang.org/grpc"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func startRpcServerTest() <-chan error {
|
|
ch := make(chan error, 2)
|
|
go func() {
|
|
if err := RunRpc(); err != nil {
|
|
ch <- fmt.Errorf("cannot run grpc service: %v", err)
|
|
}
|
|
}()
|
|
|
|
go func() {
|
|
if err := RunGwRpc(); err != nil {
|
|
ch <- fmt.Errorf("cannot run gateway service: %v", err)
|
|
}
|
|
}()
|
|
|
|
return ch
|
|
}
|
|
|
|
func callGatewayRpc() string {
|
|
time.Sleep(2 * time.Second)
|
|
|
|
url := "http://localhost:8080/v1/overflow/services"
|
|
glog.Infoln("URL : ", url)
|
|
|
|
var jsonStr = []byte(`{"serviceName":"Member","methodName":"Regist","param":{"email":"geek@loafle.com","password":"qwer5795"}}`)
|
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
|
|
|
|
if err != nil {
|
|
glog.Error(err)
|
|
}
|
|
|
|
req.Header.Set("X-Custom-Header", "myvalue")
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
glog.Error(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
log.Println("response Status:", resp.Status)
|
|
log.Println("response Headers:", resp.Header)
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|
log.Println("response Body:", string(body))
|
|
|
|
return string(body)
|
|
}
|
|
|
|
func TestRpcServer(t *testing.T) {
|
|
|
|
flag.Parse()
|
|
defer glog.Flush()
|
|
|
|
gateway.InitServices()
|
|
|
|
errch := startRpcServerTest()
|
|
|
|
chStr := make(chan string, 1)
|
|
|
|
go func() {
|
|
chStr <- callGatewayRpc()
|
|
}()
|
|
|
|
select {
|
|
case err := <-errch:
|
|
log.Println(err)
|
|
os.Exit(1)
|
|
case status := <-chStr:
|
|
log.Println(status)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func TestRPC(t *testing.T) {
|
|
|
|
conn, err := grpc.Dial(":9090", grpc.WithInsecure())
|
|
|
|
if err != nil {
|
|
t.Log(err)
|
|
}
|
|
|
|
client := pb.NewOverflowGatewayClient(conn)
|
|
|
|
si := pb.ServiceInput{}
|
|
si.MethodName = "create"
|
|
si.ServiceName = "member"
|
|
si.Param = make(map[string]string)
|
|
si.Param["1"] = "2222"
|
|
|
|
so, err := client.ExecServices(context.Background(), &si)
|
|
|
|
t.Log(so.ResultStr)
|
|
|
|
}
|