2020-08-25 13:44:41 +03:00
|
|
|
// +build ignore
|
|
|
|
|
2019-06-03 18:44:43 +01:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/api/handler"
|
|
|
|
"github.com/unistack-org/micro/v3/api/resolver"
|
|
|
|
"github.com/unistack-org/micro/v3/api/resolver/vpath"
|
|
|
|
"github.com/unistack-org/micro/v3/api/router"
|
2021-01-29 13:17:32 +03:00
|
|
|
regRouter "github.com/unistack-org/micro/v3/api/router/register"
|
|
|
|
"github.com/unistack-org/micro/v3/register"
|
|
|
|
"github.com/unistack-org/micro/v3/register/memory"
|
2019-06-03 18:44:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func testHttp(t *testing.T, path, service, ns string) {
|
2021-01-29 13:17:32 +03:00
|
|
|
r := memory.NewRegister()
|
2019-06-03 18:44:43 +01:00
|
|
|
|
|
|
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
s := ®ister.Service{
|
2019-06-03 18:44:43 +01:00
|
|
|
Name: service,
|
2021-01-29 13:17:32 +03:00
|
|
|
Nodes: []*register.Node{
|
2019-11-01 15:07:53 +00:00
|
|
|
{
|
2019-06-03 18:44:43 +01:00
|
|
|
Id: service + "-1",
|
2019-07-08 08:01:42 +01:00
|
|
|
Address: l.Addr().String(),
|
2019-06-03 18:44:43 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Register(s)
|
|
|
|
defer r.Deregister(s)
|
|
|
|
|
|
|
|
// setup the test handler
|
|
|
|
m := http.NewServeMux()
|
|
|
|
m.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte(`you got served`))
|
|
|
|
})
|
|
|
|
|
|
|
|
// start http test serve
|
|
|
|
go http.Serve(l, m)
|
|
|
|
|
|
|
|
// create new request and writer
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
req, err := http.NewRequest("POST", path, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialise the handler
|
|
|
|
rt := regRouter.NewRouter(
|
|
|
|
router.WithHandler("http"),
|
2021-01-29 13:17:32 +03:00
|
|
|
router.WithRegister(r),
|
2020-04-09 10:32:08 +01:00
|
|
|
router.WithResolver(vpath.NewResolver(
|
2020-06-26 14:28:18 +01:00
|
|
|
resolver.WithServicePrefix(ns),
|
2020-04-09 10:28:38 +01:00
|
|
|
)),
|
2019-06-03 18:44:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
p := NewHandler(handler.WithRouter(rt))
|
|
|
|
|
|
|
|
// execute the handler
|
|
|
|
p.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if w.Code != 200 {
|
|
|
|
t.Fatalf("Expected 200 response got %d %s", w.Code, w.Body.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.Body.String() != "you got served" {
|
|
|
|
t.Fatalf("Expected body: you got served. Got: %s", w.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHttpHandler(t *testing.T) {
|
|
|
|
testData := []struct {
|
|
|
|
path string
|
|
|
|
service string
|
|
|
|
namespace string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"/test/foo",
|
|
|
|
"go.micro.api.test",
|
|
|
|
"go.micro.api",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/test/foo/baz",
|
|
|
|
"go.micro.api.test",
|
|
|
|
"go.micro.api",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/v1/foo",
|
|
|
|
"go.micro.api.v1.foo",
|
|
|
|
"go.micro.api",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/v1/foo/bar",
|
|
|
|
"go.micro.api.v1.foo",
|
|
|
|
"go.micro.api",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/v2/baz",
|
|
|
|
"go.micro.api.v2.baz",
|
|
|
|
"go.micro.api",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/v2/baz/bar",
|
|
|
|
"go.micro.api.v2.baz",
|
|
|
|
"go.micro.api",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/v2/baz/bar",
|
|
|
|
"v2.baz",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range testData {
|
2020-04-09 10:28:38 +01:00
|
|
|
t.Run(d.service, func(t *testing.T) {
|
|
|
|
testHttp(t, d.path, d.service, d.namespace)
|
|
|
|
})
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
}
|