...

Source file src/cloud.google.com/go/storage/headers_test.go

Documentation: cloud.google.com/go/storage

     1  // Copyright 2023 Google LLC
     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  //      http://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  package storage
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"io"
    21  	"net/http"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/googleapis/gax-go/v2/callctx"
    26  	"google.golang.org/api/option"
    27  )
    28  
    29  // Tests that sending a custom header via the context works as expected
    30  // for a few different ops.
    31  func TestCustomHeaders(t *testing.T) {
    32  	var cases = []struct {
    33  		name    string
    34  		call    func(ctx context.Context, client *Client) error
    35  		resp    *http.Response
    36  		opts    []option.ClientOption
    37  		wantURL string
    38  	}{
    39  		{
    40  			name: "Metadata op",
    41  			call: func(ctx context.Context, client *Client) error {
    42  				_, err := client.Bucket("my-bucket").Attrs(ctx)
    43  				return err
    44  			},
    45  			resp: &http.Response{
    46  				StatusCode: 200,
    47  				Body:       bodyReader(`{"name":"my-bucket"}`),
    48  			},
    49  			wantURL: "https://storage.googleapis.com/storage/v1/b/my-bucket?alt=json&prettyPrint=false&projection=full",
    50  		},
    51  		{
    52  			name: "Writer",
    53  			call: func(ctx context.Context, client *Client) error {
    54  				w := client.Bucket("my-bucket").Object("my-object").NewWriter(ctx)
    55  				if _, err := io.Copy(w, strings.NewReader("object data")); err != nil {
    56  					return fmt.Errorf("io.Copy: %w", err)
    57  				}
    58  				return w.Close()
    59  			},
    60  			resp: &http.Response{
    61  				StatusCode: 200,
    62  				Body:       bodyReader(`{"name": "my-object"}`),
    63  			},
    64  			wantURL: "https://storage.googleapis.com/upload/storage/v1/b/my-bucket/o?alt=json&name=my-object&prettyPrint=false&projection=full&uploadType=multipart",
    65  		},
    66  		{
    67  			name: "Reader XML",
    68  			call: func(ctx context.Context, client *Client) error {
    69  				r, err := client.Bucket("my-bucket").Object("my-object").NewReader(ctx)
    70  				if err != nil {
    71  					return fmt.Errorf("NewReader: %w", err)
    72  				}
    73  				if _, err := io.Copy(io.Discard, r); err != nil {
    74  					return fmt.Errorf("io.Copy: %w", err)
    75  				}
    76  				return r.Close()
    77  			},
    78  			resp: &http.Response{
    79  				StatusCode: 200,
    80  				Body:       bodyReader("object data"),
    81  			},
    82  			opts:    []option.ClientOption{WithXMLReads()},
    83  			wantURL: "https://storage.googleapis.com/my-bucket/my-object",
    84  		},
    85  		{
    86  			name: "Reader JSON",
    87  			call: func(ctx context.Context, client *Client) error {
    88  				r, err := client.Bucket("my-bucket").Object("my-object").NewReader(ctx)
    89  				if err != nil {
    90  					return fmt.Errorf("NewReader: %w", err)
    91  				}
    92  				if _, err := io.Copy(io.Discard, r); err != nil {
    93  					return fmt.Errorf("io.Copy: %w", err)
    94  				}
    95  				return r.Close()
    96  			},
    97  			resp: &http.Response{
    98  				StatusCode: 200,
    99  				Body:       bodyReader("object data"),
   100  			},
   101  			opts:    []option.ClientOption{WithJSONReads()},
   102  			wantURL: "https://storage.googleapis.com/storage/v1/b/my-bucket/o/my-object?alt=media&prettyPrint=false&projection=full",
   103  		},
   104  	}
   105  	for _, c := range cases {
   106  		t.Run(c.name, func(r *testing.T) {
   107  			mt := &mockTransport{}
   108  			client := mockClient(t, mt, c.opts...)
   109  
   110  			key := "x-goog-custom-audit-foo"
   111  			value := "bar"
   112  			ctx := callctx.SetHeaders(context.Background(), key, value)
   113  
   114  			mt.addResult(c.resp, nil)
   115  			if err := c.call(ctx, client); err != nil {
   116  				r.Fatal(err)
   117  			}
   118  			if gotURL := mt.gotReq.URL.String(); gotURL != c.wantURL {
   119  				r.Errorf("Got URL %v, want %v", gotURL, c.wantURL)
   120  			}
   121  			if gotValue := mt.gotReq.Header.Get(key); gotValue != value {
   122  				r.Errorf("Got headers %v, want to contain %q: %q", mt.gotReq.Header, key, value)
   123  			}
   124  		})
   125  	}
   126  
   127  }
   128  

View as plain text