fix endpoint extract (#1061)

pull/1064/head
Tony Chen 4 years ago committed by GitHub
parent 6ee0607f03
commit 7f72b72ddb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      internal/host/host.go
  2. 2
      transport/grpc/server.go
  3. 16
      transport/grpc/server_test.go
  4. 2
      transport/http/server.go
  5. 13
      transport/http/server_test.go

@ -1,6 +1,7 @@
package host
import (
"fmt"
"net"
"strconv"
)
@ -27,6 +28,8 @@ func Extract(hostPort string, lis net.Listener) (string, error) {
if lis != nil {
if p, ok := Port(lis); ok {
port = strconv.Itoa(p)
} else {
return "", fmt.Errorf("failed to extract port: %v", lis.Addr())
}
}
if len(addr) > 0 && (addr != "0.0.0.0" && addr != "[::]" && addr != "::") {

@ -138,7 +138,7 @@ func (s *Server) Endpoint() (*url.URL, error) {
s.err = err
return
}
addr, err := host.Extract(s.address, s.lis)
addr, err := host.Extract(s.address, lis)
if err != nil {
lis.Close()
s.err = err

@ -2,11 +2,9 @@ package grpc
import (
"context"
"fmt"
"strings"
"testing"
"time"
"github.com/go-kratos/kratos/v2/internal/host"
)
type testKey struct{}
@ -15,7 +13,8 @@ func TestServer(t *testing.T) {
ctx := context.Background()
ctx = context.WithValue(ctx, testKey{}, "test")
srv := NewServer()
if e, err := srv.Endpoint(); err != nil || e == nil {
if e, err := srv.Endpoint(); err != nil || e == nil || strings.HasSuffix(e.Host, ":0") {
t.Fatal(e, err)
}
@ -31,13 +30,12 @@ func TestServer(t *testing.T) {
}
func testClient(t *testing.T, srv *Server) {
port, ok := host.Port(srv.lis)
if !ok {
t.Fatalf("extract port error: %v", srv.lis)
u, err := srv.Endpoint()
if err != nil {
t.Fatal(err)
}
endpoint := fmt.Sprintf("127.0.0.1:%d", port)
// new a gRPC client
conn, err := DialInsecure(context.Background(), WithEndpoint(endpoint))
conn, err := DialInsecure(context.Background(), WithEndpoint(u.Host))
if err != nil {
t.Fatal(err)
}

@ -190,7 +190,7 @@ func (s *Server) Endpoint() (*url.URL, error) {
s.err = err
return
}
addr, err := host.Extract(s.address, s.lis)
addr, err := host.Extract(s.address, lis)
if err != nil {
lis.Close()
s.err = err

@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"time"
@ -33,7 +34,7 @@ func TestServer(t *testing.T) {
srv := NewServer()
srv.HandleFunc("/index", fn)
if e, err := srv.Endpoint(); err != nil || e == nil {
if e, err := srv.Endpoint(); err != nil || e == nil || strings.HasSuffix(e.Host, ":0") {
t.Fatal(e, err)
}
@ -58,17 +59,17 @@ func testClient(t *testing.T, srv *Server) {
{"PATCH", "/index"},
{"DELETE", "/index"},
}
port, ok := host.Port(srv.lis)
if !ok {
t.Fatalf("extract port error: %v", srv.lis)
e, err := srv.Endpoint()
if err != nil {
t.Fatal(err)
}
client, err := NewClient(context.Background(), WithEndpoint(fmt.Sprintf("127.0.0.1:%d", port)))
client, err := NewClient(context.Background(), WithEndpoint(e.Host))
if err != nil {
t.Fatal(err)
}
for _, test := range tests {
var res testData
url := fmt.Sprintf("http://127.0.0.1:%d%s", port, test.path)
url := fmt.Sprintf(e.String() + test.path)
req, err := http.NewRequest(test.method, url, nil)
if err != nil {
t.Fatal(err)

Loading…
Cancel
Save