...

Source file src/github.com/googleapis/gax-go/v2/header_test.go

Documentation: github.com/googleapis/gax-go/v2

     1  // Copyright 2018, Google Inc.
     2  // All rights reserved.
     3  //
     4  // Redistribution and use in source and binary forms, with or without
     5  // modification, are permitted provided that the following conditions are
     6  // met:
     7  //
     8  //     * Redistributions of source code must retain the above copyright
     9  // notice, this list of conditions and the following disclaimer.
    10  //     * Redistributions in binary form must reproduce the above
    11  // copyright notice, this list of conditions and the following disclaimer
    12  // in the documentation and/or other materials provided with the
    13  // distribution.
    14  //     * Neither the name of Google Inc. nor the names of its
    15  // contributors may be used to endorse or promote products derived from
    16  // this software without specific prior written permission.
    17  //
    18  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    19  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    20  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    21  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    22  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    23  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    24  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    25  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    26  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    27  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    28  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    29  
    30  package gax
    31  
    32  import (
    33  	"context"
    34  	"net/http"
    35  	"testing"
    36  
    37  	"github.com/google/go-cmp/cmp"
    38  	"github.com/googleapis/gax-go/v2/callctx"
    39  	"google.golang.org/grpc/metadata"
    40  )
    41  
    42  func TestXGoogHeader(t *testing.T) {
    43  	for _, tst := range []struct {
    44  		kv   []string
    45  		want string
    46  	}{
    47  		{nil, ""},
    48  		{[]string{"abc", "def"}, "abc/def"},
    49  		{[]string{"abc", "def", "xyz", "123", "foo", ""}, "abc/def xyz/123 foo/"},
    50  	} {
    51  		got := XGoogHeader(tst.kv...)
    52  		if got != tst.want {
    53  			t.Errorf("Header(%q) = %q, want %q", tst.kv, got, tst.want)
    54  		}
    55  	}
    56  }
    57  
    58  func TestGoVersion(t *testing.T) {
    59  	testVersion := func(v string) func() string {
    60  		return func() string {
    61  			return v
    62  		}
    63  	}
    64  	for _, tst := range []struct {
    65  		v    func() string
    66  		want string
    67  	}{
    68  		{
    69  			testVersion("go1.19"),
    70  			"1.19.0",
    71  		},
    72  		{
    73  			testVersion("go1.21-20230317-RC01"),
    74  			"1.21.0-20230317-RC01",
    75  		},
    76  		{
    77  			testVersion("devel +abc1234"),
    78  			"abc1234",
    79  		},
    80  		{
    81  			testVersion("this should be unknown"),
    82  			versionUnknown,
    83  		},
    84  		{
    85  			testVersion("go1.21-20230101-RC01 cl/1234567 +abc1234"),
    86  			"1.21.0-20230101-RC01",
    87  		},
    88  	} {
    89  		version = tst.v
    90  		got := goVersion()
    91  		if diff := cmp.Diff(got, tst.want); diff != "" {
    92  			t.Errorf("got(-),want(+):\n%s", diff)
    93  		}
    94  	}
    95  }
    96  
    97  func TestInsertMetadataIntoOutgoingContext(t *testing.T) {
    98  	for _, tst := range []struct {
    99  		// User-provided metadata set in context
   100  		userMd metadata.MD
   101  		// User-provided headers set in context
   102  		userHeaders []string
   103  		// Client-provided headers passed to func
   104  		clientHeaders []string
   105  		want          metadata.MD
   106  	}{
   107  		{
   108  			userMd: metadata.Pairs("key_1", "val_1", "key_2", "val_21"),
   109  			want:   metadata.Pairs("key_1", "val_1", "key_2", "val_21"),
   110  		},
   111  		{
   112  			userHeaders: []string{"key_2", "val_22"},
   113  			want:        metadata.Pairs("key_2", "val_22"),
   114  		},
   115  		{
   116  			clientHeaders: []string{"key_2", "val_23", "key_2", "val_24"},
   117  			want:          metadata.Pairs("key_2", "val_23", "key_2", "val_24"),
   118  		},
   119  		{
   120  			userMd:        metadata.Pairs("key_1", "val_1", "key_2", "val_21"),
   121  			userHeaders:   []string{"key_2", "val_22"},
   122  			clientHeaders: []string{"key_2", "val_23", "key_2", "val_24"},
   123  			want:          metadata.Pairs("key_1", "val_1", "key_2", "val_21", "key_2", "val_22", "key_2", "val_23", "key_2", "val_24"),
   124  		},
   125  	} {
   126  		ctx := context.Background()
   127  		if tst.userMd != nil {
   128  			ctx = metadata.NewOutgoingContext(ctx, tst.userMd)
   129  		}
   130  		ctx = callctx.SetHeaders(ctx, tst.userHeaders...)
   131  
   132  		ctx = InsertMetadataIntoOutgoingContext(ctx, tst.clientHeaders...)
   133  
   134  		got, _ := metadata.FromOutgoingContext(ctx)
   135  		if diff := cmp.Diff(tst.want, got); diff != "" {
   136  			t.Errorf("InsertMetadata(ctx, %q) mismatch (-want +got):\n%s", tst.clientHeaders, diff)
   137  		}
   138  	}
   139  }
   140  
   141  func TestBuildHeaders(t *testing.T) {
   142  	// User-provided metadata set in context
   143  	existingMd := metadata.Pairs("key_1", "val_1", "key_2", "val_21")
   144  	ctx := metadata.NewOutgoingContext(context.Background(), existingMd)
   145  	// User-provided headers set in context
   146  	ctx = callctx.SetHeaders(ctx, "key_2", "val_22")
   147  	// Client-provided headers
   148  	keyvals := []string{"key_2", "val_23", "key_2", "val_24"}
   149  
   150  	got := BuildHeaders(ctx, keyvals...)
   151  
   152  	want := http.Header{"key_1": []string{"val_1"}, "key_2": []string{"val_21", "val_22", "val_23", "val_24"}}
   153  	if diff := cmp.Diff(want, got); diff != "" {
   154  		t.Errorf("InsertMetadata(ctx, %q) mismatch (-want +got):\n%s", keyvals, diff)
   155  	}
   156  }
   157  

View as plain text