File tree Expand file tree Collapse file tree 4 files changed +14
-40
lines changed Expand file tree Collapse file tree 4 files changed +14
-40
lines changed Original file line number Diff line number Diff line change 1
1
package dummy
2
2
3
- import "io"
3
+ import (
4
+ "bytes"
5
+ )
4
6
5
- type ReadCloser struct {
6
- Data []byte
7
- pos int
7
+ type ByteReadCloser struct {
8
+ * bytes.Buffer
8
9
}
9
10
10
- func (rc * ReadCloser ) Read (p []byte ) (n int , err error ) {
11
- if rc .Data == nil {
12
- return 0 , nil
13
- }
14
- var bytesWritten int
15
- if len (p ) < len (rc .Data )- rc .pos {
16
- bytesWritten = rc .write (p , len (p ))
17
- } else {
18
- bytesWritten = rc .write (p , len (rc .Data )- rc .pos )
19
- }
20
- if rc .pos == len (rc .Data ) {
21
- return bytesWritten , io .EOF
22
- }
23
- return bytesWritten , nil
24
- }
25
-
26
- func (rc * ReadCloser ) write (p []byte , count int ) int {
27
- x := 0
28
- for i := rc .pos ; i < rc .pos + count ; i ++ {
29
- p [x ] = rc .Data [i ]
30
- x ++
31
- }
32
- rc .pos += x
33
- return x
34
- }
35
-
36
- func (rc * ReadCloser ) Close () error {
37
- return nil
38
- }
11
+ func (rc * ByteReadCloser ) Close () error { return nil }
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ import (
11
11
12
12
func TestReadCloser (t * testing.T ) {
13
13
data := []byte ("super amazing data" )
14
- rc := & dummy.ReadCloser { Data : data }
14
+ rc := & dummy.ByteReadCloser { Buffer : bytes . NewBuffer ( data ) }
15
15
readData , err := ioutil .ReadAll (rc )
16
16
if err != nil {
17
17
t .Fatalf ("Error: %v" , err )
@@ -23,7 +23,7 @@ func TestReadCloser(t *testing.T) {
23
23
24
24
func TestReadCloserBigFile (t * testing.T ) {
25
25
data := []byte ("hyper amazing data" + misc .MustGetRandomHexString (10000 ))
26
- rc := & dummy.ReadCloser { Data : data }
26
+ rc := & dummy.ByteReadCloser { Buffer : bytes . NewBuffer ( data ) }
27
27
readData , err := ioutil .ReadAll (rc )
28
28
if err != nil {
29
29
t .Fatalf ("Error: %v" , err )
Original file line number Diff line number Diff line change @@ -64,7 +64,7 @@ func TestHttpClientDoFuncFailure(t *testing.T) {
64
64
func TestBadHttpResponse (t * testing.T ) {
65
65
de := fetching .CreateDummyEnvironment (t , 1000 , - 1 )
66
66
fetching .DoForClientFunc = func (client * http.Client , req * http.Request ) (* http.Response , error ) {
67
- return & http.Response {StatusCode : http .StatusInternalServerError , Body : & dummy.ReadCloser {}}, nil
67
+ return & http.Response {StatusCode : http .StatusInternalServerError , Body : & dummy.ByteReadCloser {}}, nil
68
68
}
69
69
de .TestDownloadRetries (t , "http://example.com" )
70
70
de .OmitContentLength = true
Original file line number Diff line number Diff line change 1
1
package hashing
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"fmt"
6
7
"io"
@@ -49,13 +50,13 @@ func dummyListDirectory(dirPath string) ([]os.FileInfo, error) {
49
50
func dummyReadFile (filePath string ) (io.ReadCloser , error ) {
50
51
switch filePath {
51
52
case filepath .FromSlash ("x/foo" ):
52
- return & dummy.ReadCloser { Data : []byte ("abc" )}, nil
53
+ return & dummy.ByteReadCloser { Buffer : bytes . NewBuffer ( []byte ("abc" ) )}, nil
53
54
case filepath .FromSlash ("x/foo/bar" ):
54
- return & dummy.ReadCloser { Data : []byte ("def" )}, nil
55
+ return & dummy.ByteReadCloser { Buffer : bytes . NewBuffer ( []byte ("def" ) )}, nil
55
56
case filepath .FromSlash ("x/fuu/baaar" ):
56
- return & dummy.ReadCloser { Data : []byte ("ghi" )}, nil
57
+ return & dummy.ByteReadCloser { Buffer : bytes . NewBuffer ( []byte ("ghi" ) )}, nil
57
58
case filepath .FromSlash ("x/fuu/moo/meow/bla" ):
58
- return & dummy.ReadCloser { Data : []byte ("jkl" )}, nil
59
+ return & dummy.ByteReadCloser { Buffer : bytes . NewBuffer ( []byte ("jkl" ) )}, nil
59
60
}
60
61
return nil , fmt .Errorf ("File not found" )
61
62
}
You can’t perform that action at this time.
0 commit comments