github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/server/mucp/extractor_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/server/mucp/extractor_test.go
    16  
    17  package mucp
    18  
    19  import (
    20  	"context"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/tickoalcantara12/micro/v3/service/registry"
    25  )
    26  
    27  type testHandler struct{}
    28  
    29  type testRequest struct{}
    30  
    31  type testResponse struct{}
    32  
    33  func (t *testHandler) Test(ctx context.Context, req *testRequest, rsp *testResponse) error {
    34  	return nil
    35  }
    36  
    37  func TestExtractEndpoint(t *testing.T) {
    38  	handler := &testHandler{}
    39  	typ := reflect.TypeOf(handler)
    40  
    41  	var endpoints []*registry.Endpoint
    42  
    43  	for m := 0; m < typ.NumMethod(); m++ {
    44  		if e := extractEndpoint(typ.Method(m)); e != nil {
    45  			endpoints = append(endpoints, e)
    46  		}
    47  	}
    48  
    49  	if i := len(endpoints); i != 1 {
    50  		t.Errorf("Expected 1 endpoint, have %d", i)
    51  	}
    52  
    53  	if endpoints[0].Name != "Test" {
    54  		t.Errorf("Expected handler Test, got %s", endpoints[0].Name)
    55  	}
    56  
    57  	if endpoints[0].Request == nil {
    58  		t.Error("Expected non nil request")
    59  	}
    60  
    61  	if endpoints[0].Response == nil {
    62  		t.Error("Expected non nil request")
    63  	}
    64  
    65  	if endpoints[0].Request.Name != "testRequest" {
    66  		t.Errorf("Expected testRequest got %s", endpoints[0].Request.Name)
    67  	}
    68  
    69  	if endpoints[0].Response.Name != "testResponse" {
    70  		t.Errorf("Expected testResponse got %s", endpoints[0].Response.Name)
    71  	}
    72  
    73  	if endpoints[0].Request.Type != "testRequest" {
    74  		t.Errorf("Expected testRequest type got %s", endpoints[0].Request.Type)
    75  	}
    76  
    77  	if endpoints[0].Response.Type != "testResponse" {
    78  		t.Errorf("Expected testResponse type got %s", endpoints[0].Response.Type)
    79  	}
    80  
    81  }