github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/api/handler/http/http_test.go (about)

     1  // Copyright 2020 Asim Aslam
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // Original source: github.com/micro/go-micro/v3/api/handler/http/http_test.go
    16  
    17  package http
    18  
    19  import (
    20  	"net"
    21  	"net/http"
    22  	"net/http/httptest"
    23  	"testing"
    24  
    25  	"github.com/tickoalcantara12/micro/v3/service/api/handler"
    26  	"github.com/tickoalcantara12/micro/v3/service/api/resolver"
    27  	"github.com/tickoalcantara12/micro/v3/service/api/resolver/vpath"
    28  	"github.com/tickoalcantara12/micro/v3/service/api/router"
    29  	regRouter "github.com/tickoalcantara12/micro/v3/service/api/router/registry"
    30  	"github.com/tickoalcantara12/micro/v3/service/registry"
    31  	"github.com/tickoalcantara12/micro/v3/service/registry/memory"
    32  )
    33  
    34  func testHttp(t *testing.T, path, service, ns string) {
    35  	r := memory.NewRegistry()
    36  
    37  	l, err := net.Listen("tcp", "127.0.0.1:0")
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	defer l.Close()
    42  
    43  	s := &registry.Service{
    44  		Name: service,
    45  		Nodes: []*registry.Node{
    46  			{
    47  				Id:      service + "-1",
    48  				Address: l.Addr().String(),
    49  			},
    50  		},
    51  	}
    52  
    53  	r.Register(s)
    54  	defer r.Deregister(s)
    55  
    56  	// setup the test handler
    57  	m := http.NewServeMux()
    58  	m.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
    59  		w.Write([]byte(`you got served`))
    60  	})
    61  
    62  	// start http test serve
    63  	go http.Serve(l, m)
    64  
    65  	// create new request and writer
    66  	w := httptest.NewRecorder()
    67  	req, err := http.NewRequest("POST", path, nil)
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  
    72  	// initialise the handler
    73  	rt := regRouter.NewRouter(
    74  		router.WithHandler("http"),
    75  		router.WithRegistry(r),
    76  		router.WithResolver(vpath.NewResolver(
    77  			resolver.WithServicePrefix(ns),
    78  		)),
    79  	)
    80  
    81  	p := NewHandler(handler.WithRouter(rt))
    82  
    83  	// execute the handler
    84  	p.ServeHTTP(w, req)
    85  
    86  	if w.Code != 200 {
    87  		t.Fatalf("Expected 200 response got %d %s", w.Code, w.Body.String())
    88  	}
    89  
    90  	if w.Body.String() != "you got served" {
    91  		t.Fatalf("Expected body: you got served. Got: %s", w.Body.String())
    92  	}
    93  }
    94  
    95  func TestHttpHandler(t *testing.T) {
    96  	testData := []struct {
    97  		path      string
    98  		service   string
    99  		namespace string
   100  	}{
   101  		{
   102  			"/test/foo",
   103  			"go.micro.api.test",
   104  			"go.micro.api",
   105  		},
   106  		{
   107  			"/test/foo/baz",
   108  			"go.micro.api.test",
   109  			"go.micro.api",
   110  		},
   111  		{
   112  			"/v1/foo",
   113  			"go.micro.api.v1.foo",
   114  			"go.micro.api",
   115  		},
   116  		{
   117  			"/v1/foo/bar",
   118  			"go.micro.api.v1.foo",
   119  			"go.micro.api",
   120  		},
   121  		{
   122  			"/v2/baz",
   123  			"go.micro.api.v2.baz",
   124  			"go.micro.api",
   125  		},
   126  		{
   127  			"/v2/baz/bar",
   128  			"go.micro.api.v2.baz",
   129  			"go.micro.api",
   130  		},
   131  		{
   132  			"/v2/baz/bar",
   133  			"v2.baz",
   134  			"",
   135  		},
   136  	}
   137  
   138  	for _, d := range testData {
   139  		t.Run(d.service, func(t *testing.T) {
   140  			testHttp(t, d.path, d.service, d.namespace)
   141  		})
   142  	}
   143  }