github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/util/ctx/ctx_test.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/internal/ctx/ctx_test.go
    14  
    15  package ctx
    16  
    17  import (
    18  	"net/http"
    19  	"testing"
    20  
    21  	"github.com/tickoalcantara12/micro/v3/service/context/metadata"
    22  )
    23  
    24  func TestRequestToContext(t *testing.T) {
    25  	testData := []struct {
    26  		request *http.Request
    27  		expect  metadata.Metadata
    28  	}{
    29  		{
    30  			&http.Request{
    31  				Header: http.Header{
    32  					"Foo1": []string{"bar"},
    33  					"Foo2": []string{"bar", "baz"},
    34  				},
    35  			},
    36  			metadata.Metadata{
    37  				"Foo1": "bar",
    38  				"Foo2": "bar,baz",
    39  			},
    40  		},
    41  	}
    42  
    43  	for _, d := range testData {
    44  		ctx := FromRequest(d.request)
    45  		md, ok := metadata.FromContext(ctx)
    46  		if !ok {
    47  			t.Fatalf("Expected metadata for request %+v", d.request)
    48  		}
    49  		for k, v := range d.expect {
    50  			if val := md[k]; val != v {
    51  				t.Fatalf("Expected %s for key %s for expected md %+v, got md %+v", v, k, d.expect, md)
    52  			}
    53  		}
    54  	}
    55  }