github.com/annwntech/go-micro/v2@v2.9.5/helper/helper_test.go (about)

     1  package helper
     2  
     3  import (
     4  	"net/http"
     5  	"testing"
     6  
     7  	"github.com/annwntech/go-micro/v2/metadata"
     8  )
     9  
    10  func TestRequestToContext(t *testing.T) {
    11  	testData := []struct {
    12  		request *http.Request
    13  		expect  metadata.Metadata
    14  	}{
    15  		{
    16  			&http.Request{
    17  				Header: http.Header{
    18  					"Foo1": []string{"bar"},
    19  					"Foo2": []string{"bar", "baz"},
    20  				},
    21  			},
    22  			metadata.Metadata{
    23  				"Foo1": "bar",
    24  				"Foo2": "bar,baz",
    25  			},
    26  		},
    27  	}
    28  
    29  	for _, d := range testData {
    30  		ctx := RequestToContext(d.request)
    31  		md, ok := metadata.FromContext(ctx)
    32  		if !ok {
    33  			t.Fatalf("Expected metadata for request %+v", d.request)
    34  		}
    35  		for k, v := range d.expect {
    36  			if val := md[k]; val != v {
    37  				t.Fatalf("Expected %s for key %s for expected md %+v, got md %+v", v, k, d.expect, md)
    38  			}
    39  		}
    40  	}
    41  }