package server import ( "testing" "git.loafle.net/overflow/overflow_gateway_service/gateway" "flag" "github.com/golang/glog" "time" "fmt" "os" "net/http" "bytes" "io/ioutil" "log" ) 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) } }