gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/server/extractor_test.go (about)

     1  package server
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"gitee.com/liuxuezhan/go-micro-v1.18.0/registry"
     9  )
    10  
    11  type testHandler struct{}
    12  
    13  type testRequest struct{}
    14  
    15  type testResponse struct{}
    16  
    17  func (t *testHandler) Test(ctx context.Context, req *testRequest, rsp *testResponse) error {
    18  	return nil
    19  }
    20  
    21  func TestExtractEndpoint(t *testing.T) {
    22  	handler := &testHandler{}
    23  	typ := reflect.TypeOf(handler)
    24  
    25  	var endpoints []*registry.Endpoint
    26  
    27  	for m := 0; m < typ.NumMethod(); m++ {
    28  		if e := extractEndpoint(typ.Method(m)); e != nil {
    29  			endpoints = append(endpoints, e)
    30  		}
    31  	}
    32  
    33  	if i := len(endpoints); i != 1 {
    34  		t.Errorf("Expected 1 endpoint, have %d", i)
    35  	}
    36  
    37  	if endpoints[0].Name != "Test" {
    38  		t.Errorf("Expected handler Test, got %s", endpoints[0].Name)
    39  	}
    40  
    41  	if endpoints[0].Request == nil {
    42  		t.Error("Expected non nil request")
    43  	}
    44  
    45  	if endpoints[0].Response == nil {
    46  		t.Error("Expected non nil request")
    47  	}
    48  
    49  	if endpoints[0].Request.Name != "testRequest" {
    50  		t.Errorf("Expected testRequest got %s", endpoints[0].Request.Name)
    51  	}
    52  
    53  	if endpoints[0].Response.Name != "testResponse" {
    54  		t.Errorf("Expected testResponse got %s", endpoints[0].Response.Name)
    55  	}
    56  
    57  	if endpoints[0].Request.Type != "testRequest" {
    58  		t.Errorf("Expected testRequest type got %s", endpoints[0].Request.Type)
    59  	}
    60  
    61  	if endpoints[0].Response.Type != "testResponse" {
    62  		t.Errorf("Expected testResponse type got %s", endpoints[0].Response.Type)
    63  	}
    64  
    65  }