From c41bf2671216e2b2d06c4ad86b2cd0ae2ee17b1d Mon Sep 17 00:00:00 2001 From: Krishna Srinivas Date: Mon, 20 Jun 2016 02:03:00 +0530 Subject: [PATCH] Unit tests: add unit tests for listv1/v2 for list bucket handler. (#1933) fixes #1818 --- server_test.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++ server_xl_test.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) diff --git a/server_test.go b/server_test.go index 1d701f90a..dc5c3c87e 100644 --- a/server_test.go +++ b/server_test.go @@ -1387,3 +1387,77 @@ func (s *MyAPISuite) TestObjectMultipart(c *C) { c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusOK) } + +func (s *MyAPISuite) TestListObjects(c *C) { + request, err := s.newRequest("PUT", testAPIFSCacheServer.URL+"/list-objects-bucket", 0, nil) + c.Assert(err, IsNil) + + client := http.Client{} + response, err := client.Do(request) + c.Assert(err, IsNil) + c.Assert(response.StatusCode, Equals, http.StatusOK) + + buffer1 := []byte("hello world") + objects := []string{"object1", "object2", "object3"} + + for _, object := range objects { + request, err = s.newRequest("PUT", testAPIFSCacheServer.URL+"/list-objects-bucket/"+object, int64(len(buffer1)), bytes.NewReader(buffer1)) + c.Assert(err, IsNil) + + response, err = client.Do(request) + c.Assert(err, IsNil) + c.Assert(response.StatusCode, Equals, http.StatusOK) + } + + // ListV1 - Should list all 3 entries. + request, err = s.newRequest("GET", testAPIFSCacheServer.URL+"/list-objects-bucket", 0, nil) + c.Assert(err, IsNil) + + response, err = client.Do(request) + c.Assert(err, IsNil) + result := ListObjectsResponse{} + err = xmlDecoder(response.Body, &result) + c.Assert(err, IsNil) + for i, object := range objects { + c.Assert(object, Equals, result.Contents[i].Key) + } + c.Assert(response.StatusCode, Equals, http.StatusOK) + + // ListV1 - should set NextMarker in the result. + request, err = s.newRequest("GET", testAPIFSCacheServer.URL+"/list-objects-bucket?max-keys=1&delimiter=/", 0, nil) + c.Assert(err, IsNil) + + response, err = client.Do(request) + c.Assert(err, IsNil) + resultPartial := ListObjectsResponse{} + err = xmlDecoder(response.Body, &resultPartial) + c.Assert(err, IsNil) + c.Assert(resultPartial.NextMarker, Equals, "object1") + c.Assert(response.StatusCode, Equals, http.StatusOK) + + // ListV2 - Should list all 3 entries. + request, err = s.newRequest("GET", testAPIFSCacheServer.URL+"/list-objects-bucket?list-type=2", 0, nil) + c.Assert(err, IsNil) + + response, err = client.Do(request) + c.Assert(err, IsNil) + resultV2 := ListObjectsV2Response{} + err = xmlDecoder(response.Body, &resultV2) + c.Assert(err, IsNil) + for i, object := range objects { + c.Assert(object, Equals, resultV2.Contents[i].Key) + } + c.Assert(response.StatusCode, Equals, http.StatusOK) + + // ListV2 - Should set NextContinuationToken in the result. + request, err = s.newRequest("GET", testAPIFSCacheServer.URL+"/list-objects-bucket?list-type=2&max-keys=1&delimiter=/", 0, nil) + c.Assert(err, IsNil) + + response, err = client.Do(request) + c.Assert(err, IsNil) + resultPartial2 := ListObjectsV2Response{} + err = xmlDecoder(response.Body, &resultPartial2) + c.Assert(err, IsNil) + c.Assert(resultPartial2.NextContinuationToken, Equals, "object1") + c.Assert(response.StatusCode, Equals, http.StatusOK) +} diff --git a/server_xl_test.go b/server_xl_test.go index 34e238fe4..abd61eb1f 100644 --- a/server_xl_test.go +++ b/server_xl_test.go @@ -1498,3 +1498,77 @@ func (s *MyAPIXLSuite) TestObjectMultipartOverwriteSinglePut(c *C) { c.Assert(n, Equals, int64(len([]byte("hello world")))) c.Assert(true, Equals, bytes.Equal(buffer3.Bytes(), []byte("hello world"))) } + +func (s *MyAPIXLSuite) TestListObjects(c *C) { + request, err := s.newRequest("PUT", testAPIXLServer.URL+"/list-objects-bucket", 0, nil) + c.Assert(err, IsNil) + + client := http.Client{} + response, err := client.Do(request) + c.Assert(err, IsNil) + c.Assert(response.StatusCode, Equals, http.StatusOK) + + buffer1 := []byte("hello world") + objects := []string{"object1", "object2", "object3"} + + for _, object := range objects { + request, err = s.newRequest("PUT", testAPIXLServer.URL+"/list-objects-bucket/"+object, int64(len(buffer1)), bytes.NewReader(buffer1)) + c.Assert(err, IsNil) + + response, err = client.Do(request) + c.Assert(err, IsNil) + c.Assert(response.StatusCode, Equals, http.StatusOK) + } + + // ListV1 - Should list all 3 entries. + request, err = s.newRequest("GET", testAPIXLServer.URL+"/list-objects-bucket", 0, nil) + c.Assert(err, IsNil) + + response, err = client.Do(request) + c.Assert(err, IsNil) + result := ListObjectsResponse{} + err = xmlDecoder(response.Body, &result) + c.Assert(err, IsNil) + for i, object := range objects { + c.Assert(object, Equals, result.Contents[i].Key) + } + c.Assert(response.StatusCode, Equals, http.StatusOK) + + // ListV1 - should set NextMarker in the result. + request, err = s.newRequest("GET", testAPIXLServer.URL+"/list-objects-bucket?max-keys=1&delimiter=/", 0, nil) + c.Assert(err, IsNil) + + response, err = client.Do(request) + c.Assert(err, IsNil) + resultPartial := ListObjectsResponse{} + err = xmlDecoder(response.Body, &resultPartial) + c.Assert(err, IsNil) + c.Assert(resultPartial.NextMarker, Equals, "object1") + c.Assert(response.StatusCode, Equals, http.StatusOK) + + // ListV2 - Should list all 3 entries. + request, err = s.newRequest("GET", testAPIXLServer.URL+"/list-objects-bucket?list-type=2", 0, nil) + c.Assert(err, IsNil) + + response, err = client.Do(request) + c.Assert(err, IsNil) + resultV2 := ListObjectsV2Response{} + err = xmlDecoder(response.Body, &resultV2) + c.Assert(err, IsNil) + for i, object := range objects { + c.Assert(object, Equals, resultV2.Contents[i].Key) + } + c.Assert(response.StatusCode, Equals, http.StatusOK) + + // ListV2 - Should set NextContinuationToken in the result. + request, err = s.newRequest("GET", testAPIXLServer.URL+"/list-objects-bucket?list-type=2&max-keys=1&delimiter=/", 0, nil) + c.Assert(err, IsNil) + + response, err = client.Do(request) + c.Assert(err, IsNil) + resultPartial2 := ListObjectsV2Response{} + err = xmlDecoder(response.Body, &resultPartial2) + c.Assert(err, IsNil) + c.Assert(resultPartial2.NextContinuationToken, Equals, "object1") + c.Assert(response.StatusCode, Equals, http.StatusOK) +}