github.com/karrick/gorill@v1.10.3/escrowReader_test.go (about)

     1  package gorill
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"io"
     7  	"io/ioutil"
     8  	"testing"
     9  )
    10  
    11  func TestEscrowReader(t *testing.T) {
    12  	const payload = "flubber"
    13  
    14  	original := ioutil.NopCloser(bytes.NewReader([]byte(payload)))
    15  
    16  	wrapped := NewEscrowReader(original, nil)
    17  
    18  	// Ensure Read and Close returns original errors.
    19  	buf, err := ioutil.ReadAll(wrapped)
    20  	if got, want := string(buf), payload; got != want {
    21  		t.Errorf("GOT: %v; WANT: %v", got, want)
    22  	}
    23  	if got, want := err, error(nil); got != want {
    24  		t.Errorf("GOT: %v; WANT: %v", got, want)
    25  	}
    26  	if got, want := wrapped.Close(), error(nil); got != want {
    27  		t.Errorf("GOT: %v; WANT: %v", got, want)
    28  	}
    29  
    30  	// Ensure can reset and do it again
    31  	wrapped.Reset()
    32  	buf, err = ioutil.ReadAll(wrapped)
    33  	if got, want := string(buf), payload; got != want {
    34  		t.Errorf("GOT: %v; WANT: %v", got, want)
    35  	}
    36  	if got, want := err, error(nil); got != want {
    37  		t.Errorf("GOT: %v; WANT: %v", got, want)
    38  	}
    39  
    40  	// Ensure wrapper has access to data
    41  	if got, want := string(wrapped.Bytes()), payload; got != want {
    42  		t.Errorf("GOT: %v; WANT: %v", got, want)
    43  	}
    44  }
    45  
    46  func TestEscrowReaderEOF(t *testing.T) {
    47  	const payload = "flubber"
    48  
    49  	original := ioutil.NopCloser(bytes.NewReader([]byte(payload)))
    50  
    51  	wrapped := NewEscrowReader(original, nil)
    52  
    53  	// Ensure Read and Close returns original errors.
    54  	buf := make([]byte, len(payload))
    55  	n, err := wrapped.Read(buf)
    56  	if got, want := n, len(payload); got != want {
    57  		t.Errorf("GOT: %v; WANT: %v", got, want)
    58  	}
    59  	if got, want := err, error(nil); got != want {
    60  		t.Errorf("GOT: %v; WANT: %v", got, want)
    61  	}
    62  	if got, want := string(buf), payload; got != want {
    63  		t.Errorf("GOT: %v; WANT: %v", got, want)
    64  	}
    65  
    66  	// Following Read will return 0, io.EOF
    67  	n, err = wrapped.Read(buf)
    68  	if got, want := n, 0; got != want {
    69  		t.Errorf("GOT: %v; WANT: %v", got, want)
    70  	}
    71  	if got, want := err, io.EOF; got != want {
    72  		t.Errorf("GOT: %v; WANT: %v", got, want)
    73  	}
    74  }
    75  
    76  func TestEscrowReaderClosesSource(t *testing.T) {
    77  	src := NewNopCloseBuffer()
    78  	_ = NewEscrowReader(src, nil)
    79  
    80  	if got, want := src.IsClosed(), true; got != want {
    81  		t.Errorf("GOT: %v; WANT: %v", got, want)
    82  	}
    83  }
    84  
    85  func TestEscrowReaderHandlesReadAndCloseErrors(t *testing.T) {
    86  	const payload = "flubber"
    87  	const limit = 4
    88  	closeError := errors.New("close-error")
    89  
    90  	original := errorReadCloser{
    91  		Reader: ShortReadWriteCloser{
    92  			Reader:  bytes.NewReader([]byte(payload)),
    93  			MaxRead: limit,
    94  		},
    95  		err: closeError,
    96  	}
    97  
    98  	wrapped := NewEscrowReader(original, nil)
    99  
   100  	// Ensure Read and Close returns original errors.
   101  	buf, err := ioutil.ReadAll(wrapped)
   102  	if got, want := string(buf), payload[:limit]; got != want {
   103  		t.Errorf("GOT: %v; WANT: %v", got, want)
   104  	}
   105  	if got, want := err, io.ErrUnexpectedEOF; got != want {
   106  		t.Errorf("GOT: %v; WANT: %v", got, want)
   107  	}
   108  	if got, want := wrapped.Close(), closeError; got != want {
   109  		t.Errorf("GOT: %v; WANT: %v", got, want)
   110  	}
   111  
   112  	// Ensure can reset and do it again
   113  	wrapped.Reset()
   114  	buf, err = ioutil.ReadAll(wrapped)
   115  	if got, want := string(buf), payload[:limit]; got != want {
   116  		t.Errorf("GOT: %v; WANT: %v", got, want)
   117  	}
   118  	if got, want := err, io.ErrUnexpectedEOF; got != want {
   119  		t.Errorf("GOT: %v; WANT: %v", got, want)
   120  	}
   121  	if got, want := wrapped.Close(), closeError; got != want {
   122  		t.Errorf("GOT: %v; WANT: %v", got, want)
   123  	}
   124  
   125  	// Ensure wrapper has access to data
   126  	if got, want := string(wrapped.Bytes()), payload[:limit]; got != want {
   127  		t.Errorf("GOT: %v; WANT: %v", got, want)
   128  	}
   129  }
   130  
   131  type errorReadCloser struct {
   132  	io.Reader
   133  	err error
   134  }
   135  
   136  func (erc errorReadCloser) Close() error {
   137  	return erc.err
   138  }
   139  
   140  //
   141  // WriteTo
   142  //
   143  
   144  func TestEscrowReaderWriteToReturnsWriterError(t *testing.T) {
   145  	const payload = "flubber"
   146  
   147  	src := bytes.NewBuffer([]byte(payload))
   148  	er := NewEscrowReader(ioutil.NopCloser(src), nil)
   149  	pr, pw := io.Pipe()
   150  	_ = pr.Close()
   151  
   152  	n, err := er.WriteTo(pw)
   153  
   154  	if got, want := n, int64(0); got != want {
   155  		t.Errorf("GOT: %v; WANT: %v", got, want)
   156  	}
   157  	if got, want := err, io.ErrClosedPipe; got != want {
   158  		t.Errorf("GOT: %v; WANT: %v", got, want)
   159  	}
   160  }
   161  
   162  // badWriter claims no errors, but writes fewer than requested bytes.
   163  type badWriter struct{ io.Writer }
   164  
   165  func (w *badWriter) Write(p []byte) (int, error) {
   166  	return w.Writer.Write(p[:len(p)-1])
   167  }
   168  
   169  func TestEscrowReaderWriteToErrShortWrite(t *testing.T) {
   170  	const payload = "flubber"
   171  
   172  	src := bytes.NewBuffer([]byte(payload))
   173  	er := NewEscrowReader(ioutil.NopCloser(src), nil)
   174  	dst := new(bytes.Buffer)
   175  
   176  	n, err := er.WriteTo(&badWriter{dst})
   177  
   178  	if got, want := n, int64(len(payload)-1); got != want {
   179  		t.Errorf("GOT: %v; WANT: %v", got, want)
   180  	}
   181  	if got, want := err, io.ErrShortWrite; got != want {
   182  		t.Errorf("GOT: %v; WANT: %v", got, want)
   183  	}
   184  	if got, want := string(dst.Bytes()), payload[:len(payload)-1]; got != want {
   185  		t.Errorf("GOT: %v; WANT: %v", got, want)
   186  	}
   187  }
   188  
   189  func TestEscrowReaderWriteToEmpty(t *testing.T) {
   190  	src := NewNopCloseBuffer()
   191  	er := NewEscrowReader(src, nil)
   192  	dst := new(bytes.Buffer)
   193  
   194  	n, err := er.WriteTo(dst)
   195  
   196  	if got, want := n, int64(0); got != want {
   197  		t.Errorf("GOT: %v; WANT: %v", got, want)
   198  	}
   199  	if got, want := err, error(nil); got != want {
   200  		t.Errorf("GOT: %v; WANT: %v", got, want)
   201  	}
   202  	if got, want := dst.Len(), 0; got != want {
   203  		t.Errorf("GOT: %v; WANT: %v", got, want)
   204  	}
   205  }
   206  
   207  func TestEscrowReaderWriteTo(t *testing.T) {
   208  	const payload = "flubber"
   209  
   210  	src := bytes.NewBuffer([]byte(payload))
   211  	er := NewEscrowReader(ioutil.NopCloser(src), nil)
   212  	dst := new(bytes.Buffer)
   213  
   214  	n, err := er.WriteTo(dst)
   215  
   216  	if got, want := n, int64(len(payload)); got != want {
   217  		t.Errorf("GOT: %v; WANT: %v", got, want)
   218  	}
   219  	if got, want := err, error(nil); got != want {
   220  		t.Errorf("GOT: %v; WANT: %v", got, want)
   221  	}
   222  	if got, want := string(dst.Bytes()), payload; got != want {
   223  		t.Errorf("GOT: %v; WANT: %v", got, want)
   224  	}
   225  }