github.com/btccom/go-micro/v2@v2.9.3/api/handler/http/http_test.go (about)

     1  package http
     2  
     3  import (
     4  	"net"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"testing"
     8  
     9  	"github.com/btccom/go-micro/v2/api/handler"
    10  	"github.com/btccom/go-micro/v2/api/resolver"
    11  	"github.com/btccom/go-micro/v2/api/resolver/vpath"
    12  	"github.com/btccom/go-micro/v2/api/router"
    13  	regRouter "github.com/btccom/go-micro/v2/api/router/registry"
    14  	"github.com/btccom/go-micro/v2/registry"
    15  	"github.com/btccom/go-micro/v2/registry/memory"
    16  )
    17  
    18  func testHttp(t *testing.T, path, service, ns string) {
    19  	r := memory.NewRegistry()
    20  
    21  	l, err := net.Listen("tcp", "127.0.0.1:0")
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	defer l.Close()
    26  
    27  	s := &registry.Service{
    28  		Name: service,
    29  		Nodes: []*registry.Node{
    30  			{
    31  				Id:      service + "-1",
    32  				Address: l.Addr().String(),
    33  			},
    34  		},
    35  	}
    36  
    37  	r.Register(s)
    38  	defer r.Deregister(s)
    39  
    40  	// setup the test handler
    41  	m := http.NewServeMux()
    42  	m.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
    43  		w.Write([]byte(`you got served`))
    44  	})
    45  
    46  	// start http test serve
    47  	go http.Serve(l, m)
    48  
    49  	// create new request and writer
    50  	w := httptest.NewRecorder()
    51  	req, err := http.NewRequest("POST", path, nil)
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  
    56  	// initialise the handler
    57  	rt := regRouter.NewRouter(
    58  		router.WithHandler("http"),
    59  		router.WithRegistry(r),
    60  		router.WithResolver(vpath.NewResolver(
    61  			resolver.WithNamespace(resolver.StaticNamespace(ns)),
    62  		)),
    63  	)
    64  
    65  	p := NewHandler(handler.WithRouter(rt))
    66  
    67  	// execute the handler
    68  	p.ServeHTTP(w, req)
    69  
    70  	if w.Code != 200 {
    71  		t.Fatalf("Expected 200 response got %d %s", w.Code, w.Body.String())
    72  	}
    73  
    74  	if w.Body.String() != "you got served" {
    75  		t.Fatalf("Expected body: you got served. Got: %s", w.Body.String())
    76  	}
    77  }
    78  
    79  func TestHttpHandler(t *testing.T) {
    80  	testData := []struct {
    81  		path      string
    82  		service   string
    83  		namespace string
    84  	}{
    85  		{
    86  			"/test/foo",
    87  			"go.micro.api.test",
    88  			"go.micro.api",
    89  		},
    90  		{
    91  			"/test/foo/baz",
    92  			"go.micro.api.test",
    93  			"go.micro.api",
    94  		},
    95  		{
    96  			"/v1/foo",
    97  			"go.micro.api.v1.foo",
    98  			"go.micro.api",
    99  		},
   100  		{
   101  			"/v1/foo/bar",
   102  			"go.micro.api.v1.foo",
   103  			"go.micro.api",
   104  		},
   105  		{
   106  			"/v2/baz",
   107  			"go.micro.api.v2.baz",
   108  			"go.micro.api",
   109  		},
   110  		{
   111  			"/v2/baz/bar",
   112  			"go.micro.api.v2.baz",
   113  			"go.micro.api",
   114  		},
   115  		{
   116  			"/v2/baz/bar",
   117  			"v2.baz",
   118  			"",
   119  		},
   120  	}
   121  
   122  	for _, d := range testData {
   123  		t.Run(d.service, func(t *testing.T) {
   124  			testHttp(t, d.path, d.service, d.namespace)
   125  		})
   126  	}
   127  }