func TestUploadDirExecute(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() _, filename, _, _ := runtime.Caller(0) rootDirFix := path.Dir(filename) var count int64 count = 0 filepath.Walk(rootDirFix, func(path string, info os.FileInfo, err error) error { if info.IsDir() { return nil } urlPath := "/foo" + strings.TrimPrefix(path, rootDirFix) th.Mux.HandleFunc(urlPath, func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "PUT") str, err := ioutil.ReadFile(path) if err == nil { th.TestBody(t, r, string(str)) count++ } w.WriteHeader(201) }) return nil }) fs := flag.NewFlagSet("flags", 1) fs.String("container", "", "") fs.String("dir", "", "") fs.String("quiet", "", "") fs.Set("container", "foo") fs.Set("dir", rootDirFix) fs.Set("quiet", "true") cmd := newUpDirCmd(fs) cmd.Ctx.ServiceClient = client.ServiceClient() res := &handler.Resource{} cmd.HandleFlags(res) cmd.Execute(res) th.AssertNoErr(t, res.Err) if !strings.Contains(res.Result.(string), fmt.Sprintf("Uploaded %d %s ", count, util.Pluralize("object", count))) { t.Fatalf("Unexpected result message: %s", res.Result) } }
func TestUploadHandleSingle(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() th.Mux.HandleFunc("/foo/bar", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "text/plain") th.TestBody(t, r, "body") fmt.Fprintf(w, `hodor`) }) fs := flag.NewFlagSet("flags", 1) fs.String("container", "", "") fs.String("name", "", "") fs.String("content", "", "") fs.Set("container", "foo") fs.Set("name", "bar") fs.Set("content", "baz") cmd := newUpCmd(fs) cmd.Ctx.ServiceClient = client.ServiceClient() expected := &handler.Resource{ Params: ¶msUpload{ stream: strings.NewReader("baz"), }, } actual := &handler.Resource{ Params: ¶msUpload{}, } err := cmd.HandleSingle(actual) th.AssertNoErr(t, err) expectedBytes, _ := ioutil.ReadAll(expected.Params.(*paramsUpload).stream) th.AssertNoErr(t, err) actualBytes, _ := ioutil.ReadAll(actual.Params.(*paramsUpload).stream) th.AssertNoErr(t, err) th.AssertDeepEquals(t, expectedBytes, actualBytes) }
func TestRecursionIsDisabledByDefault(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() _, filename, _, _ := runtime.Caller(0) rootDirFix := path.Dir(path.Dir(filename)) th.Mux.HandleFunc("/foo/commands.go", func(w http.ResponseWriter, r *http.Request) { th.TestMethod(t, r, "PUT") str, err := ioutil.ReadFile(rootDirFix + "/commands.go") if err == nil { th.TestBody(t, r, string(str)) } w.WriteHeader(201) }) fs := flag.NewFlagSet("flags", 1) fs.String("container", "", "") fs.String("dir", "", "") fs.String("quiet", "", "") fs.Set("container", "foo") fs.Set("dir", rootDirFix) fs.Set("quiet", "true") cmd := newUpDirCmd(fs) cmd.Ctx.ServiceClient = client.ServiceClient() res := &handler.Resource{} cmd.HandleFlags(res) cmd.Execute(res) th.AssertNoErr(t, res.Err) if !strings.Contains(res.Result.(string), "Uploaded 1 object ") { t.Fatalf("Unexpected result message: %s", res.Result) } }