github.com/cornelk/go-cloud@v0.17.1/blob/blob_writer_test.go (about)

     1  // Copyright 2019 The Go Cloud Development Kit Authors
     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  //     https://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 blob_test
    16  
    17  import (
    18  	"context"
    19  	"crypto/rand"
    20  	"fmt"
    21  	"io"
    22  	"testing"
    23  
    24  	"github.com/cornelk/go-cloud/blob"
    25  	"github.com/cornelk/go-cloud/blob/memblob"
    26  )
    27  
    28  // TestWriteReturnValues verifies that blob.Writer returns the correct n
    29  // even when it is doing content sniffing.
    30  func TestWriteReturnValues(t *testing.T) {
    31  	ctx := context.Background()
    32  
    33  	for _, withContentType := range []bool{true, false} {
    34  		t.Run(fmt.Sprintf("withContentType %v", withContentType), func(t *testing.T) {
    35  			bucket := memblob.OpenBucket(nil)
    36  			defer bucket.Close()
    37  
    38  			var opts *blob.WriterOptions
    39  			if withContentType {
    40  				opts = &blob.WriterOptions{ContentType: "application/octet-stream"}
    41  			}
    42  			w, err := bucket.NewWriter(ctx, "testkey", opts)
    43  			if err != nil {
    44  				t.Fatalf("couldn't create writer with options: %v", err)
    45  			}
    46  			defer func() {
    47  				if err := w.Close(); err != nil {
    48  					t.Errorf("failed to close writer: %v", err)
    49  				}
    50  			}()
    51  			n, err := io.CopyN(w, rand.Reader, 182)
    52  			if err != nil || n != 182 {
    53  				t.Fatalf("CopyN(182) got %d, want 182: %v", n, err)
    54  			}
    55  			n, err = io.CopyN(w, rand.Reader, 1812)
    56  			if err != nil || n != 1812 {
    57  				t.Fatalf("CopyN(1812) got %d, want 1812: %v", n, err)
    58  			}
    59  		})
    60  	}
    61  }