Make http tests line ending agnostic

This commit is contained in:
albertony 2022-01-24 00:17:59 +01:00
parent 9c451d9ac6
commit d8a874c32b

View File

@ -28,6 +28,7 @@ var (
testPath = "test" testPath = "test"
filesPath = filepath.Join(testPath, "files") filesPath = filepath.Join(testPath, "files")
headers = []string{"X-Potato", "sausage", "X-Rhubarb", "cucumber"} headers = []string{"X-Potato", "sausage", "X-Rhubarb", "cucumber"}
lineEndSize = 1
) )
// prepareServer the test server and return a function to tidy it up afterwards // prepareServer the test server and return a function to tidy it up afterwards
@ -35,6 +36,22 @@ func prepareServer(t *testing.T) (configmap.Simple, func()) {
// file server for test/files // file server for test/files
fileServer := http.FileServer(http.Dir(filesPath)) fileServer := http.FileServer(http.Dir(filesPath))
// verify the file path is correct, and also check which line endings
// are used to get sizes right ("\n" except on Windows, but even there
// we may have "\n" or "\r\n" depending on git crlf setting)
fileList, err := ioutil.ReadDir(filesPath)
require.NoError(t, err)
require.Greater(t, len(fileList), 0)
for _, file := range fileList {
if !file.IsDir() {
data, _ := ioutil.ReadFile(filepath.Join(filesPath, file.Name()))
if strings.HasSuffix(string(data), "\r\n") {
lineEndSize = 2
}
break
}
}
// test the headers are there then pass on to fileServer // test the headers are there then pass on to fileServer
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
what := fmt.Sprintf("%s %s: Header ", r.Method, r.URL.Path) what := fmt.Sprintf("%s %s: Header ", r.Method, r.URL.Path)
@ -91,7 +108,7 @@ func testListRoot(t *testing.T, f fs.Fs, noSlash bool) {
e = entries[1] e = entries[1]
assert.Equal(t, "one%.txt", e.Remote()) assert.Equal(t, "one%.txt", e.Remote())
assert.Equal(t, int64(6), e.Size()) assert.Equal(t, int64(5+lineEndSize), e.Size())
_, ok = e.(*Object) _, ok = e.(*Object)
assert.True(t, ok) assert.True(t, ok)
@ -108,7 +125,7 @@ func testListRoot(t *testing.T, f fs.Fs, noSlash bool) {
_, ok = e.(fs.Directory) _, ok = e.(fs.Directory)
assert.True(t, ok) assert.True(t, ok)
} else { } else {
assert.Equal(t, int64(41), e.Size()) assert.Equal(t, int64(40+lineEndSize), e.Size())
_, ok = e.(*Object) _, ok = e.(*Object)
assert.True(t, ok) assert.True(t, ok)
} }
@ -141,7 +158,7 @@ func TestListSubDir(t *testing.T) {
e := entries[0] e := entries[0]
assert.Equal(t, "three/underthree.txt", e.Remote()) assert.Equal(t, "three/underthree.txt", e.Remote())
assert.Equal(t, int64(9), e.Size()) assert.Equal(t, int64(8+lineEndSize), e.Size())
_, ok := e.(*Object) _, ok := e.(*Object)
assert.True(t, ok) assert.True(t, ok)
} }
@ -154,7 +171,7 @@ func TestNewObject(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, "four/under four.txt", o.Remote()) assert.Equal(t, "four/under four.txt", o.Remote())
assert.Equal(t, int64(9), o.Size()) assert.Equal(t, int64(8+lineEndSize), o.Size())
_, ok := o.(*Object) _, ok := o.(*Object)
assert.True(t, ok) assert.True(t, ok)
@ -187,7 +204,11 @@ func TestOpen(t *testing.T) {
data, err := ioutil.ReadAll(fd) data, err := ioutil.ReadAll(fd)
require.NoError(t, err) require.NoError(t, err)
require.NoError(t, fd.Close()) require.NoError(t, fd.Close())
if lineEndSize == 2 {
assert.Equal(t, "beetroot\r\n", string(data))
} else {
assert.Equal(t, "beetroot\n", string(data)) assert.Equal(t, "beetroot\n", string(data))
}
// Test with range request // Test with range request
fd, err = o.Open(context.Background(), &fs.RangeOption{Start: 1, End: 5}) fd, err = o.Open(context.Background(), &fs.RangeOption{Start: 1, End: 5})
@ -236,7 +257,7 @@ func TestIsAFileSubDir(t *testing.T) {
e := entries[0] e := entries[0]
assert.Equal(t, "underthree.txt", e.Remote()) assert.Equal(t, "underthree.txt", e.Remote())
assert.Equal(t, int64(9), e.Size()) assert.Equal(t, int64(8+lineEndSize), e.Size())
_, ok := e.(*Object) _, ok := e.(*Object)
assert.True(t, ok) assert.True(t, ok)
} }