2019-06-03 20:44:43 +03:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
"github.com/micro/go-micro/v3/client"
|
2020-08-11 12:03:47 +03:00
|
|
|
cmucp "github.com/micro/go-micro/v3/client/mucp"
|
2020-07-27 15:22:00 +03:00
|
|
|
"github.com/micro/go-micro/v3/registry/memory"
|
2020-08-18 00:44:45 +03:00
|
|
|
"github.com/micro/go-micro/v3/router"
|
|
|
|
"github.com/micro/go-micro/v3/router/registry"
|
2020-07-27 15:22:00 +03:00
|
|
|
"github.com/micro/go-micro/v3/server"
|
2020-08-11 12:03:47 +03:00
|
|
|
"github.com/micro/go-micro/v3/server/mucp"
|
2019-06-03 20:44:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type testHandler struct{}
|
|
|
|
|
|
|
|
func (t *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte(`{"hello": "world"}`))
|
|
|
|
}
|
|
|
|
|
2019-06-07 15:42:39 +03:00
|
|
|
func TestHTTPProxy(t *testing.T) {
|
2019-06-03 20:44:43 +03:00
|
|
|
c, err := net.Listen("tcp", "localhost:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
addr := c.Addr().String()
|
|
|
|
|
|
|
|
url := fmt.Sprintf("http://%s", addr)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
// http endpoint to call e.g /foo/bar
|
|
|
|
httpEp string
|
|
|
|
// rpc endpoint called e.g Foo.Bar
|
|
|
|
rpcEp string
|
|
|
|
// should be an error
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{"/", "Foo.Bar", false},
|
|
|
|
{"/", "Foo.Baz", false},
|
|
|
|
{"/helloworld", "Hello.World", true},
|
|
|
|
}
|
|
|
|
|
|
|
|
// handler
|
|
|
|
http.Handle("/", new(testHandler))
|
|
|
|
|
|
|
|
// new proxy
|
2019-06-07 15:42:39 +03:00
|
|
|
p := NewSingleHostProxy(url)
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-08-11 12:03:47 +03:00
|
|
|
reg := memory.NewRegistry()
|
2020-08-18 00:44:45 +03:00
|
|
|
rtr := registry.NewRouter(
|
|
|
|
router.Registry(reg),
|
|
|
|
)
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
// new micro service
|
2020-08-11 12:03:47 +03:00
|
|
|
service := mucp.NewServer(
|
|
|
|
server.Context(ctx),
|
|
|
|
server.Name("foobar"),
|
|
|
|
server.Registry(reg),
|
2019-06-03 20:44:43 +03:00
|
|
|
server.WithRouter(p),
|
|
|
|
)
|
|
|
|
|
2020-08-11 12:03:47 +03:00
|
|
|
service.Start()
|
|
|
|
defer service.Stop()
|
|
|
|
|
2019-06-03 20:44:43 +03:00
|
|
|
// run service
|
|
|
|
// server
|
|
|
|
go http.Serve(c, nil)
|
|
|
|
|
2020-08-11 12:03:47 +03:00
|
|
|
cl := cmucp.NewClient(
|
2020-08-18 00:44:45 +03:00
|
|
|
client.Router(rtr),
|
2020-08-11 12:03:47 +03:00
|
|
|
)
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
for _, test := range testCases {
|
2020-08-11 12:03:47 +03:00
|
|
|
req := cl.NewRequest("foobar", test.rpcEp, map[string]string{"foo": "bar"}, client.WithContentType("application/json"))
|
2019-06-03 20:44:43 +03:00
|
|
|
var rsp map[string]string
|
2020-08-11 12:03:47 +03:00
|
|
|
err := cl.Call(ctx, req, &rsp)
|
2019-06-03 20:44:43 +03:00
|
|
|
if err != nil && test.err == false {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if v := rsp["hello"]; v != "world" {
|
|
|
|
t.Fatalf("Expected hello world got %s from %s", v, test.rpcEp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|