@@ -29,6 +29,7 @@ import (
29
29
"strings"
30
30
"testing"
31
31
32
+ "github.com/google/go-cmp/cmp"
32
33
"google.golang.org/api/option"
33
34
)
34
35
@@ -380,6 +381,7 @@ func TestContentEncodingGzipWithReader(t *testing.T) {
380
381
w .Header ().Set ("Etag" , `"c50e3e41c9bc9df34e84c94ce073f928"` )
381
382
w .Header ().Set ("X-Goog-Generation" , "1587012235914578" )
382
383
w .Header ().Set ("X-Goog-MetaGeneration" , "2" )
384
+ w .Header ().Set ("X-Goog-Meta-custom-metadata-key" , "custom-metadata-value" )
383
385
w .Header ().Set ("X-Goog-Stored-Content-Encoding" , "gzip" )
384
386
w .Header ().Set ("vary" , "Accept-Encoding" )
385
387
w .Header ().Set ("x-goog-stored-content-length" , "43" )
@@ -470,6 +472,72 @@ func TestContentEncodingGzipWithReader(t *testing.T) {
470
472
}, option .WithEndpoint (mockGCS .URL ), option .WithoutAuthentication (), option .WithHTTPClient (whc ))
471
473
}
472
474
475
+ func TestMetadataParsingWithReader (t * testing.T ) {
476
+ bucketName := "my-bucket"
477
+ objectName := "test"
478
+ downloadObjectXMLurl := fmt .Sprintf ("/%s/%s" , bucketName , objectName )
479
+ downloadObjectJSONurl := fmt .Sprintf ("/b/%s/o/%s?alt=media&prettyPrint=false&projection=full" , bucketName , objectName )
480
+
481
+ original := bytes .Repeat ([]byte ("a" ), 4 )
482
+ mockGCS := httptest .NewUnstartedServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
483
+ switch r .URL .String () {
484
+ case downloadObjectXMLurl , downloadObjectJSONurl :
485
+ // Serve back the file.
486
+ w .Header ().Set ("Content-Type" , "text/plain" )
487
+ w .Header ().Set ("Etag" , `"c50e3e41c9bc9df34e84c94ce073f928"` )
488
+ w .Header ().Set ("X-Goog-Generation" , "1587012235914578" )
489
+ w .Header ().Set ("X-Goog-MetaGeneration" , "2" )
490
+ w .Header ().Set ("X-Goog-Meta-custom-metadata-key" , "custom-metadata-value" )
491
+ w .Header ().Set ("vary" , "Accept-Encoding" )
492
+ w .Header ().Set ("x-goog-stored-content-length" , "4" )
493
+ w .Header ().Set ("x-goog-hash" , "crc32c=pYIWwQ==" )
494
+ w .Header ().Set ("x-goog-hash" , "md5=xQ4+Qcm8nfNOhMlM4HP5KA==" )
495
+ w .Header ().Set ("x-goog-storage-class" , "STANDARD" )
496
+ w .Write (original )
497
+ default :
498
+ fmt .Fprintf (w , "unrecognized URL %s" , r .URL )
499
+ }
500
+ }))
501
+ mockGCS .EnableHTTP2 = true
502
+ mockGCS .StartTLS ()
503
+ defer mockGCS .Close ()
504
+
505
+ ctx := context .Background ()
506
+ hc := mockGCS .Client ()
507
+ ux , _ := url .Parse (mockGCS .URL )
508
+ hc .Transport .(* http.Transport ).TLSClientConfig .InsecureSkipVerify = true
509
+ wrt := & alwaysToTargetURLRoundTripper {
510
+ destURL : ux ,
511
+ hc : hc ,
512
+ }
513
+
514
+ whc := & http.Client {Transport : wrt }
515
+
516
+ multiReaderTest (ctx , t , func (t * testing.T , c * Client ) {
517
+ obj := c .Bucket (bucketName ).Object (objectName )
518
+ rd , err := obj .NewReader (ctx )
519
+ if err != nil {
520
+ t .Fatal (err )
521
+ }
522
+ defer rd .Close ()
523
+
524
+ expectedMetadata := map [string ]string {
525
+ "Custom-Metadata-Key" : "custom-metadata-value" ,
526
+ }
527
+ if diff := cmp .Diff (rd .Metadata (), expectedMetadata ); diff != "" {
528
+ t .Fatalf ("metadata mismatch diff got vs want: %v" , diff )
529
+ }
530
+
531
+ got , err := ioutil .ReadAll (rd )
532
+ if err != nil {
533
+ t .Fatal (err )
534
+ }
535
+ if g , w := got , original ; ! bytes .Equal (g , w ) {
536
+ t .Fatalf ("Response mismatch\n Got:\n %q\n \n Want:\n %q" , g , w )
537
+ }
538
+ }, option .WithEndpoint (mockGCS .URL ), option .WithoutAuthentication (), option .WithHTTPClient (whc ))
539
+ }
540
+
473
541
// alwaysToTargetURLRoundTripper ensures that every single request
474
542
// is routed to a target destination. Some requests within the storage
475
543
// client by-pass using the provided HTTP client, hence this enforcemenet.
0 commit comments