fix endpoint extract (#1061)

pull/1064/head
Tony Chen 3 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 package host
import ( import (
"fmt"
"net" "net"
"strconv" "strconv"
) )
@ -27,6 +28,8 @@ func Extract(hostPort string, lis net.Listener) (string, error) {
if lis != nil { if lis != nil {
if p, ok := Port(lis); ok { if p, ok := Port(lis); ok {
port = strconv.Itoa(p) 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 != "::") { 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 s.err = err
return return
} }
addr, err := host.Extract(s.address, s.lis) addr, err := host.Extract(s.address, lis)
if err != nil { if err != nil {
lis.Close() lis.Close()
s.err = err s.err = err

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

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

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings"
"testing" "testing"
"time" "time"
@ -33,7 +34,7 @@ func TestServer(t *testing.T) {
srv := NewServer() srv := NewServer()
srv.HandleFunc("/index", fn) 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) t.Fatal(e, err)
} }
@ -58,17 +59,17 @@ func testClient(t *testing.T, srv *Server) {
{"PATCH", "/index"}, {"PATCH", "/index"},
{"DELETE", "/index"}, {"DELETE", "/index"},
} }
port, ok := host.Port(srv.lis) e, err := srv.Endpoint()
if !ok { if err != nil {
t.Fatalf("extract port error: %v", srv.lis) 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
for _, test := range tests { for _, test := range tests {
var res testData 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) req, err := http.NewRequest(test.method, url, nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

Loading…
Cancel
Save