github.com/coreos/mantle@v0.13.0/storage/bucket_test.go (about)

     1  // Copyright 2016 CoreOS, Inc.
     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  	"fmt"
    19  	"net/http"
    20  	"testing"
    21  
    22  	"google.golang.org/api/storage/v1"
    23  )
    24  
    25  type fakeTransport struct{}
    26  
    27  func (f fakeTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    28  	return nil, fmt.Errorf("FAKE! %s %s", req.Method, req.URL)
    29  }
    30  
    31  func FakeBucket(bucketURL string) (*Bucket, error) {
    32  	return NewBucket(&http.Client{Transport: fakeTransport{}}, bucketURL)
    33  }
    34  
    35  func (b *Bucket) AddObject(obj *storage.Object) {
    36  	b.addObject(obj)
    37  }
    38  
    39  func TestBucketURL(t *testing.T) {
    40  	if _, err := FakeBucket("http://bucket/"); err != UnknownScheme {
    41  		t.Errorf("Unexpected error: %v", err)
    42  	}
    43  
    44  	if _, err := FakeBucket("gs:///"); err != UnknownBucket {
    45  		t.Errorf("Unexpected error: %v", err)
    46  	}
    47  
    48  	for _, test := range []struct {
    49  		url    string
    50  		name   string
    51  		prefix string
    52  	}{
    53  		{"gs://bucket", "bucket", ""},
    54  		{"gs://bucket/", "bucket", ""},
    55  		{"gs://bucket/prefix", "bucket", "prefix/"},
    56  		{"gs://bucket/prefix/", "bucket", "prefix/"},
    57  		{"gs://bucket/prefix/foo", "bucket", "prefix/foo/"},
    58  		{"gs://bucket/prefix/foo/", "bucket", "prefix/foo/"},
    59  	} {
    60  
    61  		bkt, err := FakeBucket(test.url)
    62  		if err != nil {
    63  			t.Errorf("Unexpected error for url %q: %v", test.url, err)
    64  			continue
    65  		}
    66  
    67  		if bkt.Name() != test.name {
    68  			t.Errorf("Unexpected name for url %q: %q", test.url, bkt.Name())
    69  		}
    70  		if bkt.Prefix() != test.prefix {
    71  			t.Errorf("Unexpected name for url %q: %q", test.url, bkt.Prefix())
    72  		}
    73  	}
    74  
    75  }
    76  
    77  func ExampleNextPrefix() {
    78  	fmt.Println(NextPrefix("foo/bar/baz"))
    79  	fmt.Println(NextPrefix("foo/bar/"))
    80  	fmt.Println(NextPrefix("foo/bar"))
    81  	fmt.Println(NextPrefix("foo/"))
    82  	fmt.Println(NextPrefix("foo"))
    83  	fmt.Println(NextPrefix(""))
    84  	// Output:
    85  	// foo/bar/
    86  	// foo/
    87  	// foo/
    88  	//
    89  	//
    90  }