func TestGetRunWithValidArgs(t *testing.T) { requestedBlob := "0ca907f2-dde8-4413-a304-9076c9d0978b" targetFilePath := filepath.Join(os.TempDir(), "testRunGetCommand.txt") defer os.RemoveAll(targetFilePath) handler := func(w http.ResponseWriter, r *http.Request) { req := testcmd.NewHttpRequest(r) username, password, err := req.ExtractBasicAuth() assert.NoError(t, err) assert.Equal(t, req.URL.Path, "/0d/"+requestedBlob) assert.Equal(t, req.Method, "GET") assert.Equal(t, username, "some user") assert.Equal(t, password, "some pwd") w.Write([]byte("this is your blob")) } ts := httptest.NewServer(http.HandlerFunc(handler)) defer ts.Close() config := davconf.Config{ User: "******", Password: "******", Endpoint: ts.URL, } err := runGet(config, []string{requestedBlob, targetFilePath}) assert.NoError(t, err) assert.Equal(t, getFileContent(targetFilePath), "this is your blob") }
func init() { Describe("Testing with Ginkgo", func() { It("put run with valid args", func() { pwd, _ := os.Getwd() sourceFilePath := filepath.Join(pwd, "../../../../fixtures/cat.jpg") targetBlob := "some-other-awesome-guid" serverWasHit := false handler := func(w http.ResponseWriter, r *http.Request) { serverWasHit = true req := testcmd.NewHttpRequest(r) username, password, err := req.ExtractBasicAuth() assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), req.URL.Path, "/d1/"+targetBlob) assert.Equal(GinkgoT(), req.Method, "PUT") assert.Equal(GinkgoT(), username, "some user") assert.Equal(GinkgoT(), password, "some pwd") expectedBytes := fileBytes(sourceFilePath) actualBytes, _ := ioutil.ReadAll(r.Body) assert.Equal(GinkgoT(), expectedBytes, actualBytes) w.WriteHeader(200) } ts := httptest.NewServer(http.HandlerFunc(handler)) defer ts.Close() config := davconf.Config{ User: "******", Password: "******", Endpoint: ts.URL, } err := runPut(config, []string{sourceFilePath, targetBlob}) assert.NoError(GinkgoT(), err) assert.True(GinkgoT(), serverWasHit) }) It("put run with incorrect arg count", func() { config := davconf.Config{} err := runPut(config, []string{}) assert.Error(GinkgoT(), err) assert.Contains(GinkgoT(), err.Error(), "Incorrect usage") }) }) }
func init() { Describe("Testing with Ginkgo", func() { It("get run with valid args", func() { requestedBlob := "0ca907f2-dde8-4413-a304-9076c9d0978b" targetFilePath := filepath.Join(os.TempDir(), "testRunGetCommand.txt") defer os.RemoveAll(targetFilePath) handler := func(w http.ResponseWriter, r *http.Request) { req := testcmd.NewHttpRequest(r) username, password, err := req.ExtractBasicAuth() assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), req.URL.Path, "/0d/"+requestedBlob) assert.Equal(GinkgoT(), req.Method, "GET") assert.Equal(GinkgoT(), username, "some user") assert.Equal(GinkgoT(), password, "some pwd") w.Write([]byte("this is your blob")) } ts := httptest.NewServer(http.HandlerFunc(handler)) defer ts.Close() config := davconf.Config{ User: "******", Password: "******", Endpoint: ts.URL, } err := runGet(config, []string{requestedBlob, targetFilePath}) assert.NoError(GinkgoT(), err) assert.Equal(GinkgoT(), getFileContent(targetFilePath), "this is your blob") }) It("get run with incorrect arg count", func() { config := davconf.Config{} err := runGet(config, []string{}) assert.Error(GinkgoT(), err) assert.Contains(GinkgoT(), err.Error(), "Incorrect usage") }) }) }
func TestPutRunWithValidArgs(t *testing.T) { pwd, _ := os.Getwd() sourceFilePath := filepath.Join(pwd, "../../../../fixtures/cat.jpg") targetBlob := "some-other-awesome-guid" serverWasHit := false handler := func(w http.ResponseWriter, r *http.Request) { serverWasHit = true req := testcmd.NewHttpRequest(r) username, password, err := req.ExtractBasicAuth() assert.NoError(t, err) assert.Equal(t, req.URL.Path, "/d1/"+targetBlob) assert.Equal(t, req.Method, "PUT") assert.Equal(t, username, "some user") assert.Equal(t, password, "some pwd") expectedBytes := fileBytes(sourceFilePath) actualBytes, _ := ioutil.ReadAll(r.Body) assert.Equal(t, expectedBytes, actualBytes) w.WriteHeader(200) } ts := httptest.NewServer(http.HandlerFunc(handler)) defer ts.Close() config := davconf.Config{ User: "******", Password: "******", Endpoint: ts.URL, } err := runPut(config, []string{sourceFilePath, targetBlob}) assert.NoError(t, err) assert.True(t, serverWasHit) }