package s3api import ( "net/http" "net/http/httptest" "net/url" "testing" "github.com/gorilla/mux" "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants" "github.com/stretchr/testify/assert" ) func getPartRequest() *http.Request { req := &http.Request{ Method: http.MethodGet, URL: &url.URL{Path: "/bucket/key", RawQuery: "part&partNumber=1&uploadId=abc"}, } return mux.SetURLVars(req, map[string]string{"bucket": "bucket", "object": "key"}) } func TestGetPartIncompleteUploadActionIgnoresRequestShape(t *testing.T) { assert.Equal(t, s3_constants.S3_ACTION_GET_INCOMPLETE_UPLOAD, ResolveS3Action(getPartRequest(), s3_constants.ACTION_GET_INCOMPLETE_UPLOAD, "bucket", "key")) } func TestGetPartIsNotAPartsListing(t *testing.T) { assert.Equal(t, Action(""), multipartListingAction(getPartRequest(), s3_constants.ACTION_READ)) withUploads := getPartRequest() withUploads.URL.RawQuery = "part&partNumber=1&uploadId=abc&uploads" assert.Equal(t, Action(""), multipartListingAction(withUploads, s3_constants.ACTION_READ)) listing := getPartRequest() listing.URL.RawQuery = "uploadId=abc" assert.Equal(t, Action(s3_constants.ACTION_LIST_PARTS), multipartListingAction(listing, s3_constants.ACTION_READ)) } func TestPartRangeWriter(t *testing.T) { cases := []struct { name string ranged bool wantStatus int wantRangeHdr string }{ {"whole part answers 200", false, http.StatusOK, ""}, {"ranged read counts within the part", true, http.StatusPartialContent, "bytes 2-5/10"}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { rec := httptest.NewRecorder() w := &partRangeWriter{ResponseWriter: rec, ranged: c.ranged, contentRange: "bytes 2-5/10"} w.Header().Set("Content-Range", "bytes 102-105/4096") w.WriteHeader(http.StatusPartialContent) assert.Equal(t, c.wantStatus, rec.Code) assert.Equal(t, c.wantRangeHdr, rec.Header().Get("Content-Range")) }) } rec := httptest.NewRecorder() (&partRangeWriter{ResponseWriter: rec}).WriteHeader(http.StatusInternalServerError) assert.Equal(t, http.StatusInternalServerError, rec.Code) }