github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/proxy/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/proxy/http/http_test.go
    16  
    17  package http
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"net"
    23  	"net/http"
    24  	"testing"
    25  
    26  	"github.com/tickoalcantara12/micro/v3/service/client"
    27  	cmucp "github.com/tickoalcantara12/micro/v3/service/client/mucp"
    28  	"github.com/tickoalcantara12/micro/v3/service/registry/memory"
    29  	"github.com/tickoalcantara12/micro/v3/service/router"
    30  	"github.com/tickoalcantara12/micro/v3/service/router/registry"
    31  	"github.com/tickoalcantara12/micro/v3/service/server"
    32  	"github.com/tickoalcantara12/micro/v3/service/server/mucp"
    33  )
    34  
    35  type testHandler struct{}
    36  
    37  func (t *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    38  	w.Write([]byte(`{"hello": "world"}`))
    39  }
    40  
    41  func TestHTTPProxy(t *testing.T) {
    42  	c, err := net.Listen("tcp", "localhost:0")
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	defer c.Close()
    47  	addr := c.Addr().String()
    48  
    49  	url := fmt.Sprintf("http://%s", addr)
    50  
    51  	testCases := []struct {
    52  		// http endpoint to call e.g /foo/bar
    53  		httpEp string
    54  		// rpc endpoint called e.g Foo.Bar
    55  		rpcEp string
    56  		// should be an error
    57  		err bool
    58  	}{
    59  		{"/", "Foo.Bar", false},
    60  		{"/", "Foo.Baz", false},
    61  		{"/helloworld", "Hello.World", true},
    62  	}
    63  
    64  	// handler
    65  	http.Handle("/", new(testHandler))
    66  
    67  	// new proxy
    68  	p := NewSingleHostProxy(url)
    69  
    70  	ctx, cancel := context.WithCancel(context.Background())
    71  	defer cancel()
    72  
    73  	reg := memory.NewRegistry()
    74  	rtr := registry.NewRouter(
    75  		router.Registry(reg),
    76  	)
    77  
    78  	// new micro service
    79  	service := mucp.NewServer(
    80  		server.Context(ctx),
    81  		server.Name("foobar"),
    82  		server.Registry(reg),
    83  		server.WithRouter(p),
    84  	)
    85  
    86  	service.Start()
    87  	defer service.Stop()
    88  
    89  	// run service
    90  	// server
    91  	go http.Serve(c, nil)
    92  
    93  	cl := cmucp.NewClient(
    94  		client.Router(rtr),
    95  	)
    96  
    97  	for _, test := range testCases {
    98  		req := cl.NewRequest("foobar", test.rpcEp, map[string]string{"foo": "bar"}, client.WithContentType("application/json"))
    99  		var rsp map[string]string
   100  		err := cl.Call(ctx, req, &rsp)
   101  		if err != nil && test.err == false {
   102  			t.Fatal(err)
   103  		}
   104  		if v := rsp["hello"]; v != "world" {
   105  			t.Fatalf("Expected hello world got %s from %s", v, test.rpcEp)
   106  		}
   107  	}
   108  }