From dd0c8337180be3531e533fc13c624b9c8bfa7b83 Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Fri, 23 Jan 2015 19:33:56 -0800 Subject: [PATCH] Adding tests for GET object in minioapi. --- pkg/webapi/minioapi/minioapi_test.go | 151 ++++++++++++++++++++------- 1 file changed, 113 insertions(+), 38 deletions(-) diff --git a/pkg/webapi/minioapi/minioapi_test.go b/pkg/webapi/minioapi/minioapi_test.go index 96cdb512b..50e2c545e 100644 --- a/pkg/webapi/minioapi/minioapi_test.go +++ b/pkg/webapi/minioapi/minioapi_test.go @@ -17,45 +17,120 @@ package minioapi import ( - "encoding/xml" - "fmt" - "log" - "os" + "bytes" + "io/ioutil" + "net/http" + "net/http/httptest" "testing" + + "github.com/minio-io/minio/pkg/storage/inmemory" + . "gopkg.in/check.v1" ) -func TestMinioApi(t *testing.T) { - owner := Owner{ - ID: "MyID", - DisplayName: "MyDisplayName", - } - contents := []Content{ - Content{ - Key: "one", - LastModified: "two", - ETag: "\"ETag\"", - Size: 1, - StorageClass: "three", - Owner: owner, - }, - Content{ - Key: "four", - LastModified: "five", - ETag: "\"ETag\"", - Size: 1, - StorageClass: "six", - Owner: owner, - }, - } - data := &ObjectListResponse{ - Name: "name", - Contents: contents, - } - - xmlEncoder := xml.NewEncoder(os.Stdout) - if err := xmlEncoder.Encode(data); err != nil { - log.Println(err) - } else { - fmt.Println("") - } +func Test(t *testing.T) { TestingT(t) } + +type MySuite struct{} + +var _ = Suite(&MySuite{}) + +func (s *MySuite) TestNonExistantObject(c *C) { + _, _, storage := inmemory.Start() + httpHandler := HttpHandler(storage) + testServer := httptest.NewServer(httpHandler) + defer testServer.Close() + + response, err := http.Get(testServer.URL + "/bucket/object") + c.Assert(err, IsNil) + c.Assert(response.StatusCode, Equals, http.StatusNotFound) +} + +func (s *MySuite) TestEmptyObject(c *C) { + _, _, storage := inmemory.Start() + httpHandler := HttpHandler(storage) + testServer := httptest.NewServer(httpHandler) + defer testServer.Close() + + buffer := bytes.NewBufferString("") + storage.StoreBucket("bucket") + storage.StoreObject("bucket", "object", buffer) + + response, err := http.Get(testServer.URL + "/bucket/object") + c.Assert(err, IsNil) + c.Assert(response.StatusCode, Equals, http.StatusOK) + + responseBody, err := ioutil.ReadAll(response.Body) + c.Assert(err, IsNil) + c.Assert(true, Equals, bytes.Equal(responseBody, buffer.Bytes())) + + // TODO Test Headers +} + +func (s *MySuite) TestObject(c *C) { + _, _, storage := inmemory.Start() + httpHandler := HttpHandler(storage) + testServer := httptest.NewServer(httpHandler) + defer testServer.Close() + + buffer := bytes.NewBufferString("hello world") + storage.StoreBucket("bucket") + storage.StoreObject("bucket", "object", buffer) + + response, err := http.Get(testServer.URL + "/bucket/object") + c.Assert(err, IsNil) + c.Assert(response.StatusCode, Equals, http.StatusOK) + + responseBody, err := ioutil.ReadAll(response.Body) + c.Assert(err, IsNil) + c.Assert(true, Equals, bytes.Equal(responseBody, []byte("hello world"))) + + // TODO Test Headers +} + +func (s *MySuite) TestMultipleObjects(c *C) { + _, _, storage := inmemory.Start() + httpHandler := HttpHandler(storage) + testServer := httptest.NewServer(httpHandler) + defer testServer.Close() + + buffer1 := bytes.NewBufferString("hello one") + buffer2 := bytes.NewBufferString("hello two") + buffer3 := bytes.NewBufferString("hello three") + + storage.StoreBucket("bucket") + storage.StoreObject("bucket", "object1", buffer1) + storage.StoreObject("bucket", "object2", buffer2) + storage.StoreObject("bucket", "object3", buffer3) + + // test non-existant object + response, err := http.Get(testServer.URL + "/bucket/object") + c.Assert(err, IsNil) + c.Assert(response.StatusCode, Equals, http.StatusNotFound) + // TODO Test Headers + + // test object 1 + response, err = http.Get(testServer.URL + "/bucket/object1") + c.Assert(err, IsNil) + c.Assert(response.StatusCode, Equals, http.StatusOK) + responseBody, err := ioutil.ReadAll(response.Body) + c.Assert(err, IsNil) + c.Assert(true, Equals, bytes.Equal(responseBody, []byte("hello one"))) + // TODO Test Headers + + // test object 2 + response, err = http.Get(testServer.URL + "/bucket/object2") + c.Assert(err, IsNil) + c.Assert(response.StatusCode, Equals, http.StatusOK) + responseBody, err = ioutil.ReadAll(response.Body) + c.Assert(err, IsNil) + c.Assert(true, Equals, bytes.Equal(responseBody, []byte("hello two"))) + // TODO Test Headers + + // test object 3 + response, err = http.Get(testServer.URL + "/bucket/object3") + c.Assert(err, IsNil) + c.Assert(response.StatusCode, Equals, http.StatusOK) + responseBody, err = ioutil.ReadAll(response.Body) + c.Assert(err, IsNil) + c.Assert(true, Equals, bytes.Equal(responseBody, []byte("hello three"))) + // TODO Test Headers }