From f14f983a760773ff4c2fd82c909c4cdc6c2e6ceb Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 23 Jul 2015 20:57:15 -0700 Subject: [PATCH] Add common methods test --- api_handlers_test.go | 4 +++ cmd_test.go | 33 ----------------- common_methods_test.go | 81 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 33 deletions(-) create mode 100644 common_methods_test.go diff --git a/api_handlers_test.go b/api_handlers_test.go index e386db3a..45dd800c 100644 --- a/api_handlers_test.go +++ b/api_handlers_test.go @@ -47,6 +47,10 @@ func (h objectAPIHandler) getHandler(w http.ResponseWriter, r *http.Request) { w.Write(response) return case r.URL.Path != "": + if _, ok := h.object[filepath.Base(r.URL.Path)]; !ok { + w.WriteHeader(http.StatusNotFound) + return + } w.Header().Set("Content-Length", strconv.Itoa(len(h.object[filepath.Base(r.URL.Path)]))) w.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat)) w.Header().Set("ETag", "b1946ac92492d2347c6235b4d2611184") diff --git a/cmd_test.go b/cmd_test.go index 3b1bf158..ae3254f3 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -17,8 +17,6 @@ package main import ( - "bytes" - "io" "io/ioutil" "os" "path/filepath" @@ -297,34 +295,3 @@ func (ta *testAddr) Error() string { func (ta *testAddr) String() string { return "testAddr" } - -func (s *CmdTestSuite) TestCommonMethods(c *C) { - /// filesystem - root, err := ioutil.TempDir(os.TempDir(), "cmd-") - c.Assert(err, IsNil) - defer os.RemoveAll(root) - - objectPath := filepath.Join(root, "object1") - data := "hello" - dataLen := len(data) - - err = putTarget(objectPath, int64(dataLen), bytes.NewReader([]byte(data))) - c.Assert(err, IsNil) - - reader, size, err := getSource(objectPath) - c.Assert(err, IsNil) - c.Assert(size, Not(Equals), 0) - var results bytes.Buffer - _, err = io.CopyN(&results, reader, int64(size)) - c.Assert(err, IsNil) - c.Assert([]byte(data), DeepEquals, results.Bytes()) - - _, content, err := url2Stat(objectPath) - c.Assert(err, IsNil) - c.Assert(content.Name, Equals, filepath.Join(root, "object1")) - c.Assert(content.Type.IsRegular(), Equals, true) - - _, _, err = url2Stat(objectPath + "invalid") - c.Assert(err, Not(IsNil)) - -} diff --git a/common_methods_test.go b/common_methods_test.go new file mode 100644 index 00000000..57f0221c --- /dev/null +++ b/common_methods_test.go @@ -0,0 +1,81 @@ +/* + * Minio Client (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "bytes" + "io" + "io/ioutil" + "os" + "path/filepath" + + . "gopkg.in/check.v1" +) + +func (s *CmdTestSuite) TestCommonMethods(c *C) { + /// filesystem + root, err := ioutil.TempDir(os.TempDir(), "cmd-") + c.Assert(err, IsNil) + defer os.RemoveAll(root) + + objectPath := filepath.Join(root, "object1") + + c.Assert(isTargetURLDir(root), Equals, true) + c.Assert(isTargetURLDir(objectPath), Equals, false) + + objectPathServer := server.URL + "/bucket/object1" + data := "hello" + dataLen := len(data) + err = putTarget(objectPath, int64(dataLen), bytes.NewReader([]byte(data))) + c.Assert(err, IsNil) + err = putTarget(objectPathServer, int64(dataLen), bytes.NewReader([]byte(data))) + c.Assert(err, IsNil) + + c.Assert(isTargetURLDir(objectPathServer), Equals, false) + c.Assert(isTargetURLDir(server.URL+"/bucket"), Equals, true) + + reader, size, err := getSource(objectPathServer) + c.Assert(err, IsNil) + c.Assert(size, Not(Equals), 0) + var results bytes.Buffer + _, err = io.CopyN(&results, reader, int64(size)) + c.Assert(err, IsNil) + c.Assert([]byte(data), DeepEquals, results.Bytes()) + + _, content, err := url2Stat(objectPathServer) + c.Assert(err, IsNil) + c.Assert(content.Name, Equals, "object1") + c.Assert(content.Type.IsRegular(), Equals, true) + + _, _, err = getSource(objectPathServer + "invalid") + c.Assert(err, Not(IsNil)) + + _, _, err = url2Stat(objectPath + "invalid") + c.Assert(err, Not(IsNil)) + + _, err = source2Client(objectPathServer) + c.Assert(err, IsNil) + + _, err = target2Client(objectPathServer) + c.Assert(err, IsNil) + + _, err = source2Client("http://test.minio.io" + "/bucket/fail") + c.Assert(err, Not(IsNil)) + + _, err = target2Client("http://test.minio.io" + "/bucket/fail") + c.Assert(err, Not(IsNil)) +}