writer_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  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. package storage
  15. import (
  16. "fmt"
  17. "net/http"
  18. "testing"
  19. "golang.org/x/net/context"
  20. "google.golang.org/cloud"
  21. )
  22. type fakeTransport struct{}
  23. func (t *fakeTransport) RoundTrip(req *http.Request) (*http.Response, error) {
  24. return nil, fmt.Errorf("error handling request")
  25. }
  26. func TestErrorOnObjectsInsertCall(t *testing.T) {
  27. ctx := context.Background()
  28. hc := &http.Client{Transport: &fakeTransport{}}
  29. client, err := NewClient(ctx, cloud.WithBaseHTTP(hc))
  30. if err != nil {
  31. t.Errorf("error when creating client: %v", err)
  32. }
  33. wc := client.Bucket("bucketname").Object("filename1").NewWriter(ctx)
  34. wc.ContentType = "text/plain"
  35. // We can't check that the Write fails, since it depends on the write to the
  36. // underling fakeTransport failing which is racy.
  37. wc.Write([]byte("hello world"))
  38. // Close must always return an error though since it waits for the transport to
  39. // have closed.
  40. if err := wc.Close(); err == nil {
  41. t.Errorf("expected error on close, got nil")
  42. }
  43. }