github.com/auxten/ginkgo@v0.0.0-20220130172820-7d98ad59d232/download/http_test.go (about)

     1  package download
     2  
     3  import (
     4  	"crypto/md5"
     5  	"encoding/hex"
     6  	"io"
     7  	"testing"
     8  
     9  	. "github.com/smartystreets/goconvey/convey"
    10  )
    11  
    12  var (
    13  	uri = "http://nginx.org/download/nginx-1.0.15.tar.gz"
    14  	//uri    = "http://localhost:9099/nginx-1.0.15.tar.gz"
    15  	//uri    = "http://localhost:1323/nginx"
    16  	md5sum = "17da4802209b83d9bebb0f0edd975dfc"
    17  )
    18  
    19  func TestHttpDownloader_DownOffset(t *testing.T) {
    20  	Convey("nginx-1.0.15.tar.gz", t, func() {
    21  		var (
    22  			n int
    23  		)
    24  
    25  		buf := make([]byte, 102400)
    26  		down := HttpDownloader{}
    27  		r, err := down.DownOffset(uri, 0, -1)
    28  		So(err, ShouldBeNil)
    29  		h := md5.New()
    30  		for err != io.EOF {
    31  			n, err = r.Read(buf)
    32  			h.Write(buf[:n])
    33  		}
    34  		_ = r.Close()
    35  		So(hex.EncodeToString(h.Sum(nil)), ShouldResemble, md5sum)
    36  	})
    37  
    38  	Convey("nginx-1.0.15.tar.gz", t, func() {
    39  		var (
    40  			n int
    41  		)
    42  
    43  		buf := make([]byte, 102400)
    44  		down := HttpDownloader{}
    45  		r, err := down.DownOffset(uri, 10, -1)
    46  		So(err, ShouldBeNil)
    47  		h := md5.New()
    48  		for err != io.EOF {
    49  			n, err = r.Read(buf)
    50  			h.Write(buf[:n])
    51  		}
    52  		_ = r.Close()
    53  		So(hex.EncodeToString(h.Sum(nil)), ShouldResemble, "23c12b79ca4dee716f43b9fef10b296d")
    54  	})
    55  
    56  	Convey("nginx-1.0.15.tar.gz", t, func() {
    57  		var (
    58  			n int
    59  		)
    60  
    61  		buf := make([]byte, 102400)
    62  		down := HttpDownloader{}
    63  		r, err := down.DownOffset(uri, 10, 11) //echo "ECBD" | xxd -r -p| md5
    64  		So(err, ShouldBeNil)
    65  		h := md5.New()
    66  		for err != io.EOF {
    67  			n, err = r.Read(buf)
    68  			h.Write(buf[:n])
    69  		}
    70  		_ = r.Close()
    71  		So(hex.EncodeToString(h.Sum(nil)), ShouldResemble, "08581bfce36b88261f1158c2b01efa82")
    72  	})
    73  
    74  }
    75  
    76  func TestHttpDownloader_DownOffset_multi(t *testing.T) {
    77  	Convey("nginx-1.0.15.tar.gz multi ranges", t, func() {
    78  		var (
    79  			n      int
    80  			acc    int
    81  			offset int64
    82  			err    error
    83  			r      io.Reader
    84  		)
    85  
    86  		buf := make([]byte, 102400)
    87  		down := HttpDownloader{}
    88  		h := md5.New()
    89  		for err != io.EOF {
    90  			acc = 0
    91  			r, err = down.DownOffset(uri, offset, offset+int64(len(buf)))
    92  			So(err, ShouldBeNil)
    93  			for err != io.EOF {
    94  				if n, err = r.Read(buf[acc:]); err != nil {
    95  					return
    96  				}
    97  				acc += n
    98  				if acc == len(buf) {
    99  					break
   100  				}
   101  			}
   102  			h.Write(buf[:acc])
   103  			offset += int64(acc)
   104  		}
   105  		So(hex.EncodeToString(h.Sum(nil)), ShouldResemble, md5sum)
   106  	})
   107  }