Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for HttpReaderAt #2

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ module github.com/snabb/httpreaderat

require (
github.com/avvmoto/buf-readerat v0.0.0-20171115124131-a17c8cb89270
github.com/pkg/errors v0.8.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.6.1
)

go 1.13
16 changes: 14 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
github.com/avvmoto/buf-readerat v0.0.0-20171115124131-a17c8cb89270 h1:JIxGEMs4E5Zb6R7z2C5IgecI0mkqS97WAEF31wUbYTM=
github.com/avvmoto/buf-readerat v0.0.0-20171115124131-a17c8cb89270/go.mod h1:2XtVRGCw/HthOLxU0Qw6o6jSJrcEoOb2OCCl8gQYvGw=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
94 changes: 94 additions & 0 deletions httpreaderat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package httpreaderat

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/suite"
)

type readerAtFixture struct {
suite.Suite
server *httptest.Server
}

func (ra *readerAtFixture) AfterTest(suiteName, testName string) {
if ra.server != nil {
ra.server.Close()
}
}

func (ra *readerAtFixture) reader() (*HTTPReaderAt, error) {
req, err := http.NewRequest("GET", ra.server.URL+"/file.zip", nil)
ra.Nil(err)
return New(nil, req, nil)
}

func TestReaderAtFixture(t *testing.T) {
suite.Run(t, new(readerAtFixture))
}

func (ra *readerAtFixture) TestRangeNotSupported() {
ra.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ra.Equal(r.Method, "GET")
ra.Equal(r.URL.String(), "/file.zip")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"content":{"data": [1,2,3]}}`))
}))

reader, err := ra.reader()
ra.EqualError(err, "server does not support range requests")
ra.Nil(reader)
}

func (ra *readerAtFixture) TestNonGetRequestMethod() {
req, err := http.NewRequest("POST", "http://not-valid.url/file.zip", nil)
ra.Nil(err)
reader, err := New(nil, req, nil)
ra.EqualError(err, "invalid HTTP method")
ra.Nil(reader)
}

func (ra *readerAtFixture) TestRangeSupportIntial() {
ra.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rnge := r.Header.Get("Range")
ra.Equal(rnge, "bytes=0-0")
w.Header().Set("Content-Range", fmt.Sprintf(" bytes 0-0/%d", 1))
w.WriteHeader(http.StatusPartialContent)
w.Write([]byte{17})
}))

reader, err := ra.reader()
ra.Nil(err)
ra.NotNil(reader)
}

func (ra *readerAtFixture) TestRangeSupportIntialEmptyResponse() {
ra.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rnge := r.Header.Get("Range")
ra.Equal(rnge, "bytes=0-0")
w.Header().Set("Content-Range", fmt.Sprintf(" bytes 0-0/%d", 1))
w.WriteHeader(http.StatusPartialContent)
w.Write([]byte{})
}))

reader, err := ra.reader()
ra.EqualError(err, "content-length mismatch in http response")
ra.Nil(reader)
}

func (ra *readerAtFixture) TestRangeSupportIntialTooMuchResponse() {
ra.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rnge := r.Header.Get("Range")
ra.Equal(rnge, "bytes=0-0")
w.Header().Set("Content-Range", fmt.Sprintf(" bytes 0-0/%d", 1))
w.WriteHeader(http.StatusPartialContent)
w.Write([]byte{17, 18, 19}) // should be 1
}))

reader, err := ra.reader()
ra.EqualError(err, "content-length mismatch in http response")
ra.Nil(reader)
}