OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package cipd |
| 6 |
| 7 import ( |
| 8 "bytes" |
| 9 "io/ioutil" |
| 10 "net/http" |
| 11 "os" |
| 12 "path/filepath" |
| 13 "testing" |
| 14 |
| 15 . "github.com/smartystreets/goconvey/convey" |
| 16 ) |
| 17 |
| 18 func TestUpload(t *testing.T) { |
| 19 Convey("Upload full flow", t, func(c C) { |
| 20 storage := mockStorageImpl(c, []expectedHTTPCall{ |
| 21 { |
| 22 Method: "PUT", |
| 23 Path: "/upl", |
| 24 Body: "01234", |
| 25 Headers: http.Header{"Content-Range": []string{"
bytes 0-4/13"}}, |
| 26 }, |
| 27 { |
| 28 Method: "PUT", |
| 29 Path: "/upl", |
| 30 Body: "56789", |
| 31 Headers: http.Header{"Content-Range": []string{"
bytes 5-9/13"}}, |
| 32 }, |
| 33 // Insert a error. |
| 34 { |
| 35 Method: "PUT", |
| 36 Path: "/upl", |
| 37 Body: "abc", |
| 38 Headers: http.Header{"Content-Range": []string{"
bytes 10-12/13"}}, |
| 39 Status: 500, |
| 40 }, |
| 41 // Request for uploaded offset #1: failed itself. |
| 42 { |
| 43 Method: "PUT", |
| 44 Path: "/upl", |
| 45 Body: "", |
| 46 Headers: http.Header{"Content-Range": []string{"
bytes */13"}}, |
| 47 Status: 500, |
| 48 }, |
| 49 // Request for uploaded offset #2: indicates part of dat
a uploaded. |
| 50 { |
| 51 Method: "PUT", |
| 52 Path: "/upl", |
| 53 Body: "", |
| 54 Headers: http.Header{"Content-Range": []
string{"bytes */13"}}, |
| 55 Status: 308, |
| 56 ResponseHeaders: http.Header{"Range": []string{"
bytes=0-7"}}, |
| 57 }, |
| 58 // Resume of the upload from returned offset. |
| 59 { |
| 60 Method: "PUT", |
| 61 Path: "/upl", |
| 62 Body: "89abc", |
| 63 Headers: http.Header{"Content-Range": []string{"
bytes 8-12/13"}}, |
| 64 }, |
| 65 }) |
| 66 err := storage.upload("http://localhost/upl", bytes.NewReader([]
byte("0123456789abc"))) |
| 67 So(err, ShouldBeNil) |
| 68 }) |
| 69 } |
| 70 |
| 71 func TestDownload(t *testing.T) { |
| 72 Convey("With temp directory", t, func() { |
| 73 tempDir, err := ioutil.TempDir("", "cipd_test") |
| 74 So(err, ShouldBeNil) |
| 75 Reset(func() { os.RemoveAll(tempDir) }) |
| 76 tempFile := filepath.Join(tempDir, "pkg") |
| 77 |
| 78 Convey("Download full flow", func(c C) { |
| 79 out, err := os.OpenFile(tempFile, os.O_RDWR|os.O_CREATE,
0666) |
| 80 So(err, ShouldBeNil) |
| 81 defer out.Close() |
| 82 |
| 83 storage := mockStorageImpl(c, []expectedHTTPCall{ |
| 84 // Simulate a transient error. |
| 85 { |
| 86 Method: "GET", |
| 87 Path: "/dwn", |
| 88 Status: 500, |
| 89 Reply: "error", |
| 90 }, |
| 91 { |
| 92 Method: "GET", |
| 93 Path: "/dwn", |
| 94 Status: 200, |
| 95 Reply: "file data", |
| 96 }, |
| 97 }) |
| 98 err = storage.download("http://localhost/dwn", out) |
| 99 So(err, ShouldBeNil) |
| 100 |
| 101 out.Seek(0, os.SEEK_SET) |
| 102 fetched, err := ioutil.ReadAll(out) |
| 103 So(err, ShouldBeNil) |
| 104 So(string(fetched), ShouldEqual, "file data") |
| 105 }) |
| 106 }) |
| 107 } |
| 108 |
| 109 //////////////////////////////////////////////////////////////////////////////// |
| 110 |
| 111 func mockStorageImpl(c C, expectations []expectedHTTPCall) *storageImpl { |
| 112 return &storageImpl{client: mockClient(c, expectations), chunkSize: 5} |
| 113 } |
OLD | NEW |