github.com/aavshr/aws-sdk-go@v1.41.3/private/protocol/rest/rest_test.go (about)

     1  //go:build go1.7
     2  // +build go1.7
     3  
     4  package rest_test
     5  
     6  import (
     7  	"bytes"
     8  	"io/ioutil"
     9  	"net/http"
    10  	"reflect"
    11  	"testing"
    12  
    13  	"github.com/aavshr/aws-sdk-go/aws"
    14  	"github.com/aavshr/aws-sdk-go/aws/client"
    15  	"github.com/aavshr/aws-sdk-go/aws/client/metadata"
    16  	"github.com/aavshr/aws-sdk-go/aws/request"
    17  	"github.com/aavshr/aws-sdk-go/aws/signer/v4"
    18  	"github.com/aavshr/aws-sdk-go/awstesting/unit"
    19  	"github.com/aavshr/aws-sdk-go/private/protocol/rest"
    20  )
    21  
    22  func TestUnsetHeaders(t *testing.T) {
    23  	cfg := &aws.Config{Region: aws.String("us-west-2")}
    24  	c := unit.Session.ClientConfig("testService", cfg)
    25  	svc := client.New(
    26  		*cfg,
    27  		metadata.ClientInfo{
    28  			ServiceName:   "testService",
    29  			SigningName:   c.SigningName,
    30  			SigningRegion: c.SigningRegion,
    31  			Endpoint:      c.Endpoint,
    32  			APIVersion:    "",
    33  		},
    34  		c.Handlers,
    35  	)
    36  
    37  	// Handlers
    38  	svc.Handlers.Sign.PushBackNamed(v4.SignRequestHandler)
    39  	svc.Handlers.Build.PushBackNamed(rest.BuildHandler)
    40  	svc.Handlers.Unmarshal.PushBackNamed(rest.UnmarshalHandler)
    41  	svc.Handlers.UnmarshalMeta.PushBackNamed(rest.UnmarshalMetaHandler)
    42  	op := &request.Operation{
    43  		Name:     "test-operation",
    44  		HTTPPath: "/",
    45  	}
    46  
    47  	input := &struct {
    48  		Foo aws.JSONValue `location:"header" locationName:"x-amz-foo" type:"jsonvalue"`
    49  		Bar aws.JSONValue `location:"header" locationName:"x-amz-bar" type:"jsonvalue"`
    50  	}{}
    51  
    52  	output := &struct {
    53  		Foo aws.JSONValue `location:"header" locationName:"x-amz-foo" type:"jsonvalue"`
    54  		Bar aws.JSONValue `location:"header" locationName:"x-amz-bar" type:"jsonvalue"`
    55  	}{}
    56  
    57  	req := svc.NewRequest(op, input, output)
    58  	req.HTTPResponse = &http.Response{StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBuffer(nil)), Header: http.Header{}}
    59  	req.HTTPResponse.Header.Set("X-Amz-Foo", "e30=")
    60  
    61  	// unmarshal response
    62  	rest.UnmarshalMeta(req)
    63  	rest.Unmarshal(req)
    64  	if req.Error != nil {
    65  		t.Fatal(req.Error)
    66  	}
    67  }
    68  
    69  func TestNormalizedHeaders(t *testing.T) {
    70  	cases := map[string]struct {
    71  		inputValues          map[string]*string
    72  		outputValues         http.Header
    73  		expectedInputHeaders http.Header
    74  		expectedOutput       map[string]*string
    75  		normalize            bool
    76  	}{
    77  		"non-normalized headers": {
    78  			inputValues: map[string]*string{
    79  				"baz": aws.String("bazValue"),
    80  				"BAR": aws.String("barValue"),
    81  			},
    82  			expectedInputHeaders: http.Header{
    83  				"X-Amz-Meta-Baz": []string{"bazValue"},
    84  				"X-Amz-Meta-Bar": []string{"barValue"},
    85  			},
    86  			outputValues: http.Header{
    87  				"X-Amz-Meta-Baz": []string{"bazValue"},
    88  				"X-Amz-Meta-Bar": []string{"barValue"},
    89  			},
    90  			expectedOutput: map[string]*string{
    91  				"Baz": aws.String("bazValue"),
    92  				"Bar": aws.String("barValue"),
    93  			},
    94  		},
    95  		"normalized headers": {
    96  			inputValues: map[string]*string{
    97  				"baz": aws.String("bazValue"),
    98  				"BAR": aws.String("barValue"),
    99  			},
   100  			expectedInputHeaders: http.Header{
   101  				"X-Amz-Meta-Baz": []string{"bazValue"},
   102  				"X-Amz-Meta-Bar": []string{"barValue"},
   103  			},
   104  			outputValues: http.Header{
   105  				"X-Amz-Meta-Baz": []string{"bazValue"},
   106  				"X-Amz-Meta-Bar": []string{"barValue"},
   107  			},
   108  			expectedOutput: map[string]*string{
   109  				"baz": aws.String("bazValue"),
   110  				"bar": aws.String("barValue"),
   111  			},
   112  			normalize: true,
   113  		},
   114  	}
   115  
   116  	for name, tt := range cases {
   117  		t.Run(name, func(t *testing.T) {
   118  			cfg := &aws.Config{Region: aws.String("us-west-2"), LowerCaseHeaderMaps: &tt.normalize}
   119  			c := unit.Session.ClientConfig("testService", cfg)
   120  			svc := client.New(
   121  				*cfg,
   122  				metadata.ClientInfo{
   123  					ServiceName:   "testService",
   124  					SigningName:   c.SigningName,
   125  					SigningRegion: c.SigningRegion,
   126  					Endpoint:      c.Endpoint,
   127  					APIVersion:    "",
   128  				},
   129  				c.Handlers,
   130  			)
   131  
   132  			// Handlers
   133  			op := &request.Operation{
   134  				Name:     "test-operation",
   135  				HTTPPath: "/",
   136  			}
   137  
   138  			input := &struct {
   139  				Foo map[string]*string `location:"headers" locationName:"x-amz-meta-" type:"map"`
   140  			}{
   141  				Foo: tt.inputValues,
   142  			}
   143  
   144  			output := &struct {
   145  				Foo map[string]*string `location:"headers" locationName:"x-amz-meta-" type:"map"`
   146  			}{}
   147  
   148  			req := svc.NewRequest(op, input, output)
   149  			req.HTTPResponse = &http.Response{
   150  				StatusCode: 200,
   151  				Body:       ioutil.NopCloser(bytes.NewBuffer(nil)),
   152  				Header:     tt.outputValues,
   153  			}
   154  
   155  			// Build Request
   156  			rest.Build(req)
   157  			if req.Error != nil {
   158  				t.Fatal(req.Error)
   159  			}
   160  
   161  			if e, a := tt.expectedInputHeaders, req.HTTPRequest.Header; !reflect.DeepEqual(e, a) {
   162  				t.Errorf("expected %v, but got %v", e, a)
   163  			}
   164  
   165  			// unmarshal response
   166  			rest.UnmarshalMeta(req)
   167  			rest.Unmarshal(req)
   168  			if req.Error != nil {
   169  				t.Fatal(req.Error)
   170  			}
   171  
   172  			if e, a := tt.expectedOutput, output.Foo; !reflect.DeepEqual(e, a) {
   173  				t.Errorf("expected %v, but got %v", e, a)
   174  			}
   175  		})
   176  	}
   177  }