github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/lib/http/serve/serve_test.go (about) 1 package serve 2 3 import ( 4 "context" 5 "io" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 "time" 10 11 "github.com/rclone/rclone/fstest/mockobject" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestObjectBadMethod(t *testing.T) { 16 w := httptest.NewRecorder() 17 r := httptest.NewRequest("BADMETHOD", "http://example.com/aFile", nil) 18 o := mockobject.New("aFile") 19 Object(w, r, o) 20 resp := w.Result() 21 assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) 22 body, _ := io.ReadAll(resp.Body) 23 assert.Equal(t, "Method Not Allowed\n", string(body)) 24 } 25 26 func TestObjectHEAD(t *testing.T) { 27 w := httptest.NewRecorder() 28 r := httptest.NewRequest("HEAD", "http://example.com/aFile", nil) 29 o := mockobject.New("aFile").WithContent([]byte("hello"), mockobject.SeekModeNone) 30 _ = o.SetModTime(context.Background(), time.Date(2023, 9, 20, 12, 11, 15, 0, time.FixedZone("", 4*60*60))) // UTC+4 31 Object(w, r, o) 32 resp := w.Result() 33 assert.Equal(t, http.StatusOK, resp.StatusCode) 34 assert.Equal(t, "5", resp.Header.Get("Content-Length")) 35 assert.Equal(t, "bytes", resp.Header.Get("Accept-Ranges")) 36 assert.Equal(t, "Wed, 20 Sep 2023 08:11:15 GMT", resp.Header.Get("Last-Modified")) 37 body, _ := io.ReadAll(resp.Body) 38 assert.Equal(t, "", string(body)) 39 } 40 41 func TestObjectGET(t *testing.T) { 42 w := httptest.NewRecorder() 43 r := httptest.NewRequest("GET", "http://example.com/aFile", nil) 44 o := mockobject.New("aFile").WithContent([]byte("hello"), mockobject.SeekModeNone) 45 _ = o.SetModTime(context.Background(), time.Date(2023, 9, 20, 12, 11, 15, 0, time.FixedZone("", 2*60*60))) // UTC+2 46 Object(w, r, o) 47 resp := w.Result() 48 assert.Equal(t, http.StatusOK, resp.StatusCode) 49 assert.Equal(t, "5", resp.Header.Get("Content-Length")) 50 assert.Equal(t, "bytes", resp.Header.Get("Accept-Ranges")) 51 assert.Equal(t, "Wed, 20 Sep 2023 10:11:15 GMT", resp.Header.Get("Last-Modified")) 52 body, _ := io.ReadAll(resp.Body) 53 assert.Equal(t, "hello", string(body)) 54 } 55 56 func TestObjectRange(t *testing.T) { 57 w := httptest.NewRecorder() 58 r := httptest.NewRequest("GET", "http://example.com/aFile", nil) 59 r.Header.Add("Range", "bytes=3-5") 60 o := mockobject.New("aFile").WithContent([]byte("0123456789"), mockobject.SeekModeNone) 61 Object(w, r, o) 62 resp := w.Result() 63 assert.Equal(t, http.StatusPartialContent, resp.StatusCode) 64 assert.Equal(t, "3", resp.Header.Get("Content-Length")) 65 assert.Equal(t, "bytes", resp.Header.Get("Accept-Ranges")) 66 assert.Equal(t, "bytes 3-5/10", resp.Header.Get("Content-Range")) 67 body, _ := io.ReadAll(resp.Body) 68 assert.Equal(t, "345", string(body)) 69 } 70 71 func TestObjectBadRange(t *testing.T) { 72 w := httptest.NewRecorder() 73 r := httptest.NewRequest("GET", "http://example.com/aFile", nil) 74 r.Header.Add("Range", "xxxbytes=3-5") 75 o := mockobject.New("aFile").WithContent([]byte("0123456789"), mockobject.SeekModeNone) 76 Object(w, r, o) 77 resp := w.Result() 78 assert.Equal(t, http.StatusBadRequest, resp.StatusCode) 79 assert.Equal(t, "10", resp.Header.Get("Content-Length")) 80 body, _ := io.ReadAll(resp.Body) 81 assert.Equal(t, "Bad Request\n", string(body)) 82 }