Ejemplo n.º 1
0
func TestWriteErrorNoFile(t *testing.T) {
	blob := filepicker.NewBlob("XYZ")
	client := filepicker.NewClient(FakeApiKey)
	switch blob, err := client.Write(blob, "unknown.unknown.file", nil); {
	case blob != nil:
		t.Errorf("want blob == nil; got %v", blob)
	case err == nil:
		t.Error("want err != nil; got nil")
	}
}
Ejemplo n.º 2
0
func TestDownloadToFileError(t *testing.T) {
	fperr, handler := ErrorHandler(dummyErrStr)

	blob := filepicker.NewBlob(FakeHandle)
	client := filepicker.NewClientStorage(FakeApiKey, filepicker.Azure)
	mock := MockServer(t, client, handler)
	defer mock.Close()

	if err := client.DownloadToFile(blob, nil, "."); err.Error() != fperr.Error() {
		t.Errorf("want error message == %q; got %q", fperr, err)
	}
}
Ejemplo n.º 3
0
func TestWriteURL(t *testing.T) {
	const TestURL = "https://www.filepicker.com/image.png"
	tests := []struct {
		Opt *filepicker.WriteOpts
		URL string
	}{
		{
			Opt: nil,
			URL: "http://www.filepicker.io/api/file/2HHH3",
		},
		{
			Opt: &filepicker.WriteOpts{
				Base64Decode: true,
			},
			URL: "http://www.filepicker.io/api/file/2HHH3?base64decode=true",
		},
		{
			Opt: &filepicker.WriteOpts{
				Base64Decode: true,
				Security:     dummySecurity,
			},
			URL: "http://www.filepicker.io/api/file/2HHH3?base64decode=true&policy=P&signature=S",
		},
	}

	var reqURL, reqMethod, reqBody string
	handler := testHandle(&reqURL, &reqMethod, &reqBody)

	blob := filepicker.NewBlob(FakeHandle)
	client := filepicker.NewClient(FakeApiKey)
	mock := MockServer(t, client, handler)
	defer mock.Close()

	for i, test := range tests {
		blob, err := client.WriteURL(blob, TestURL, test.Opt)
		if err != nil {
			t.Errorf("want err == nil; got %v (i:%d)", err, i)
		}
		if blob == nil {
			t.Errorf("want blob != nil; got nil (i:%d)", i)
		}
		if test.URL != reqURL {
			t.Errorf("want reqURL == %q; got %q (i:%d)", test.URL, reqURL, i)
		}
		if reqMethod != "POST" {
			t.Errorf("want reqMethod == POST; got %s (i:%d)", reqMethod, i)
		}
		if TestUrlEsc := "url=" + url.QueryEscape(TestURL); reqBody != TestUrlEsc {
			t.Errorf("want reqBody == %q; got %q (i:%d)", TestUrlEsc, reqBody, i)
		}
	}
}
Ejemplo n.º 4
0
func TestRemoveError(t *testing.T) {
	fperr, handler := ErrorHandler(dummyErrStr)

	blob := filepicker.NewBlob(FakeHandle)
	client := filepicker.NewClient(FakeApiKey)

	mock := MockServer(t, client, handler)
	defer mock.Close()

	if err := client.Remove(blob, nil); err.Error() != fperr.Error() {
		t.Errorf("want error message == %q; got %q", fperr, err)
	}
}
Ejemplo n.º 5
0
func TestWrite(t *testing.T) {
	tests := []struct {
		Opt *filepicker.WriteOpts
		URL string
	}{
		{
			Opt: nil,
			URL: "http://www.filepicker.io/api/file/2HHH3",
		},
		{
			Opt: &filepicker.WriteOpts{
				Base64Decode: true,
			},
			URL: "http://www.filepicker.io/api/file/2HHH3?base64decode=true",
		},
		{
			Opt: &filepicker.WriteOpts{
				Base64Decode: true,
				Security:     dummySecurity,
			},
			URL: "http://www.filepicker.io/api/file/2HHH3?base64decode=true&policy=P&signature=S",
		},
	}

	filename := tempFile(t)
	defer os.Remove(filename)

	var reqURL, reqMethod, reqBody string
	handler := testHandle(&reqURL, &reqMethod, &reqBody)

	blob := filepicker.NewBlob(FakeHandle)
	client := filepicker.NewClient(FakeApiKey)
	mock := MockServer(t, client, handler)
	defer mock.Close()

	for i, test := range tests {
		blob, err := client.Write(blob, filename, test.Opt)
		if err != nil {
			t.Errorf("want err == nil; got %v (i:%d)", err, i)
		}
		if blob == nil {
			t.Errorf("want blob != nil; got nil (i:%d)", i)
		}
		if reqMethod != "POST" {
			t.Errorf("want reqMethod == POST; got %s (i:%d)", reqMethod, i)
		}
		if test.URL != reqURL {
			t.Errorf("want reqURL == %q; got %q (i:%d)", test.URL, reqURL, i)
		}
	}
}
Ejemplo n.º 6
0
func TestRemove(t *testing.T) {
	tests := []struct {
		Opt *filepicker.RemoveOpts
		URL string
	}{
		{
			Opt: nil,
			URL: "http://www.filepicker.io/api/file/2HHH3?key=0KKK1",
		},
		{
			Opt: &filepicker.RemoveOpts{},
			URL: "http://www.filepicker.io/api/file/2HHH3?key=0KKK1",
		},
		{
			Opt: &filepicker.RemoveOpts{
				Security: dummySecurity,
			},
			URL: "http://www.filepicker.io/api/file/2HHH3?key=0KKK1&policy=P&signature=S",
		},
	}

	var reqURL, reqMethod, reqBody string
	handler := func(w http.ResponseWriter, req *http.Request) {
		body, _ := ioutil.ReadAll(req.Body)
		reqBody = string(body)
		reqURL = req.URL.String()
		reqMethod = req.Method
	}

	blob := filepicker.NewBlob(FakeHandle)
	client := filepicker.NewClient(FakeApiKey)
	mock := MockServer(t, client, handler)
	defer mock.Close()

	for i, test := range tests {
		if err := client.Remove(blob, test.Opt); err != nil {
			t.Errorf("want err == nil; got %v (i:%d)", err, i)
		}
		if test.URL != reqURL {
			t.Errorf("want reqURL == %q; got %q (i:%d)", test.URL, reqURL, i)
		}
		if reqMethod != "DELETE" {
			t.Errorf("want reqMethod == DELETE; got %s (i:%d)", reqMethod, i)
		}
		if reqBody != "" {
			t.Errorf("want reqBody == ``; got %q (i:%d)", reqBody, i)
		}
	}
}
Ejemplo n.º 7
0
func TestStatError(t *testing.T) {
	fperr, handler := ErrorHandler(dummyErrStr)

	blob := filepicker.NewBlob(FakeHandle)
	client := filepicker.NewClient(FakeApiKey)
	mock := MockServer(t, client, handler)
	defer mock.Close()

	switch meta, err := client.Stat(blob, nil); {
	case meta != nil:
		t.Errorf("want meta == nil; got %v", meta)
	case err.Error() != fperr.Error():
		t.Errorf("want error message == %q; got %q", fperr, err)
	}
}
Ejemplo n.º 8
0
func TestWriteURLError(t *testing.T) {
	fperr, handler := ErrorHandler(dummyErrStr)

	blob := filepicker.NewBlob(FakeHandle)
	client := filepicker.NewClient(FakeApiKey)
	mock := MockServer(t, client, handler)
	defer mock.Close()

	switch blob, err := client.WriteURL(blob, "http://www.address.fp", nil); {
	case blob != nil:
		t.Errorf("want blob == nil; got %v", blob)
	case err.Error() != fperr.Error():
		t.Errorf("want error message == %q; got %q", fperr, err)
	}
}
Ejemplo n.º 9
0
func TestConvertAndStoreError(t *testing.T) {
	fperr, handler := ErrorHandler(dummyErrStr)

	blob := filepicker.NewBlob(FakeHandle)
	opts := &filepicker.ConvertOpts{Width: 300}
	client := filepicker.NewClient(FakeApiKey)
	mock := MockServer(t, client, handler)
	defer mock.Close()

	switch blob, err := client.ConvertAndStore(blob, opts); {
	case blob != nil:
		t.Errorf("want blob == nil; got %v", blob)
	case err.Error() != fperr.Error():
		t.Errorf("want error message == %q; got %q", fperr, err)
	}
}
Ejemplo n.º 10
0
func TestDownloadToError(t *testing.T) {
	fperr, handler := ErrorHandler(dummyErrStr)

	blob := filepicker.NewBlob(FakeHandle)
	client := filepicker.NewClient(FakeApiKey)
	mock := MockServer(t, client, handler)
	defer mock.Close()

	var buff bytes.Buffer
	switch byteRead, err := client.DownloadTo(blob, nil, &buff); {
	case byteRead != 0:
		t.Errorf("want byteRead == 0; got %d", byteRead)
	case err.Error() != fperr.Error():
		t.Errorf("want error message == %q; got %q", fperr, err)
	}
}
Ejemplo n.º 11
0
func TestWriteError(t *testing.T) {
	fperr, handler := ErrorHandler(dummyErrStr)

	client := filepicker.NewClient(FakeApiKey)
	mock := MockServer(t, client, handler)
	defer mock.Close()

	blob := filepicker.NewBlob("XYZ")
	filename := tempFile(t)
	defer os.Remove(filename)

	switch blob, err := client.Write(blob, filename, nil); {
	case blob != nil:
		t.Errorf("want blob == nil; got %v", blob)
	case err.Error() != fperr.Error():
		t.Errorf("want error message == %q; got %q", fperr, err)
	}
}
Ejemplo n.º 12
0
func TestStat(t *testing.T) {
	tests := []struct {
		Opt *filepicker.StatOpts
		URL string
	}{
		{
			Opt: nil,
			URL: "http://www.filepicker.io/api/file/2HHH3/metadata",
		},
		{
			Opt: &filepicker.StatOpts{
				Tags:     nil,
				Security: dummySecurity,
			},
			URL: "http://www.filepicker.io/api/file/2HHH3/metadata?policy=P&signature=S",
		},
		{
			Opt: &filepicker.StatOpts{
				Tags: []filepicker.MetaTag{
					filepicker.TagSize,
					filepicker.TagWidth,
				},
			},
			URL: "http://www.filepicker.io/api/file/2HHH3/metadata?size=true&width=true",
		},
		{
			Opt: &filepicker.StatOpts{
				Tags: []filepicker.MetaTag{
					filepicker.TagLocation,
				},
				Security: dummySecurity,
			},
			URL: "http://www.filepicker.io/api/file/2HHH3/metadata?location=true&policy=P&signature=S",
		},
	}

	var reqURL, reqMethod, reqBody string
	handler := func(w http.ResponseWriter, req *http.Request) {
		body, _ := ioutil.ReadAll(req.Body)
		reqBody = string(body)
		reqURL = req.URL.String()
		reqMethod = req.Method
		w.Write([]byte("{}"))
	}

	blob := filepicker.NewBlob(FakeHandle)
	client := filepicker.NewClient(FakeApiKey)
	mock := MockServer(t, client, handler)
	defer mock.Close()

	for i, test := range tests {
		meta, err := client.Stat(blob, test.Opt)
		if err != nil {
			t.Errorf("want err == nil; got %v (i:%d)", err, i)
		}
		if meta == nil {
			t.Errorf("want meta != nil; got nil (i:%d)", i)
		}
		if test.URL != reqURL {
			t.Errorf("want reqURL == %q; got %q (i:%d)", test.URL, reqURL, i)
		}
		if reqMethod != "GET" {
			t.Errorf("want reqMethod == GET; got %s (i:%d)", reqMethod, i)
		}
		if reqBody != "" {
			t.Errorf("want reqBody == ``; got %q (i:%d)", reqBody, i)
		}
	}
}
Ejemplo n.º 13
0
func TestConvertAndStore(t *testing.T) {
	tests := []struct {
		Opt  *filepicker.ConvertOpts
		URL  string
		Body string
	}{
		{
			Opt: &filepicker.ConvertOpts{
				Width:    100,
				Location: filepicker.Azure,
			},
			URL:  "http://www.filepicker.io/api/file/2HHH3/convert",
			Body: "key=0KKK1&storeLocation=azure&width=100",
		},
		{
			Opt: &filepicker.ConvertOpts{
				Width:  150,
				Height: 200,
				Fit:    filepicker.FitScale,
			},
			URL:  "http://www.filepicker.io/api/file/2HHH3/convert",
			Body: "fit=scale&height=200&key=0KKK1&width=150",
		},
		{
			Opt: &filepicker.ConvertOpts{
				Align:   filepicker.AlignTop,
				Quality: 34,
			},
			URL:  "http://www.filepicker.io/api/file/2HHH3/convert",
			Body: "align=top&key=0KKK1&quality=34",
		},
		{
			Opt: &filepicker.ConvertOpts{
				Width:    100,
				Security: dummySecurity,
			},
			URL:  "http://www.filepicker.io/api/file/2HHH3/convert",
			Body: "key=0KKK1&policy=P&signature=S&width=100",
		},
	}

	var reqURL, reqMethod, reqBody string
	handler := testHandle(&reqURL, &reqMethod, &reqBody)

	blob := filepicker.NewBlob(FakeHandle)
	client := filepicker.NewClient(FakeApiKey)
	mock := MockServer(t, client, handler)
	defer mock.Close()

	for i, test := range tests {
		blob, err := client.ConvertAndStore(blob, test.Opt)
		if err != nil {
			t.Errorf("want err == nil; got %v (i:%d)", err, i)
		}
		if blob == nil {
			t.Errorf("want blob != nil; got nil (i:%d)", i)
		}
		if test.URL != reqURL {
			t.Errorf("want reqURL == %q; got %q (i:%d)", test.URL, reqURL, i)
		}
		if reqMethod != "POST" {
			t.Errorf("want reqMethod == POST; got %s (i:%d)", reqMethod, i)
		}
		if reqBody != test.Body {
			t.Errorf("want reqBody == %q; got %q (i:%d)", test.Body, reqBody, i)
		}
	}
}
Ejemplo n.º 14
0
func TestDownloadToFile(t *testing.T) {
	var testCounter int
	tests := []struct {
		Opt     *filepicker.DownloadOpts
		URL     string
		XName   string
		FileDir string
		Path    string
	}{
		{
			Opt:     nil,
			URL:     "http://www.filepicker.io/api/file/2HHH3",
			XName:   "document.txt",
			FileDir: ".",
			Path:    "./document.txt",
		},
		{
			Opt:     nil,
			URL:     "http://www.filepicker.io/api/file/2HHH3",
			XName:   "",
			FileDir: "something.txt",
			Path:    "./something.txt",
		},
		{
			Opt:     nil,
			URL:     "http://www.filepicker.io/api/file/2HHH3",
			XName:   "document.txt",
			FileDir: "./doc.txt",
			Path:    "./doc.txt",
		},
		{
			Opt: &filepicker.DownloadOpts{
				Base64Decode: true,
			},
			URL:     "http://www.filepicker.io/api/file/2HHH3?base64decode=true",
			XName:   "dc.txt",
			FileDir: ".",
			Path:    "./dc.txt",
		},
		{
			Opt: &filepicker.DownloadOpts{
				Base64Decode: true,
				Security:     dummySecurity,
			},
			URL:     "http://www.filepicker.io/api/file/2HHH3?base64decode=true&policy=P&signature=S",
			XName:   "dc.txt",
			FileDir: ".",
			Path:    "./dc.txt",
		},
	}

	var reqURL, reqMethod, reqBody string
	handler := func(w http.ResponseWriter, req *http.Request) {
		body, _ := ioutil.ReadAll(req.Body)
		reqBody = string(body)
		reqURL = req.URL.String()
		reqMethod = req.Method
		w.Header().Set("X-File-Name", tests[testCounter].XName)
		testCounter++
		if _, err := w.Write([]byte(downloadFileContent)); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}
	}

	blob := filepicker.NewBlob(FakeHandle)
	client := filepicker.NewClient(FakeApiKey)
	mock := MockServer(t, client, handler)
	defer mock.Close()

	for i, test := range tests {
		if err := client.DownloadToFile(blob, test.Opt, test.FileDir); err != nil {
			t.Errorf("want err == nil; got %v (i:%d)", err, i)
		}
		if test.URL != reqURL {
			t.Errorf("want reqURL == %q; got %q (i:%d)", test.URL, reqURL, i)
		}
		if reqMethod != "GET" {
			t.Errorf("want reqMethod == GET; got %s (i:%d)", reqMethod, i)
		}
		b, err := ioutil.ReadFile(test.Path)
		if err != nil {
			t.Errorf("want err == nil; got %v (i:%d)", err, i)
		}
		if content := string(b); content != downloadFileContent {
			t.Errorf("want content == %q; got %q (i:%d)", downloadFileContent, content, i)
		}
		if err := os.Remove(test.Path); err != nil {
			t.Errorf("want err == nil; got %v (i:%d)", err, i)
		}
	}
}
Ejemplo n.º 15
0
func TestDownloadTo(t *testing.T) {
	tests := []struct {
		Opt *filepicker.DownloadOpts
		URL string
	}{
		{
			Opt: nil,
			URL: "http://www.filepicker.io/api/file/2HHH3",
		},
		{
			Opt: &filepicker.DownloadOpts{
				Base64Decode: true,
			},
			URL: "http://www.filepicker.io/api/file/2HHH3?base64decode=true",
		},
		{
			Opt: &filepicker.DownloadOpts{
				Base64Decode: true,
				Security:     dummySecurity,
			},
			URL: "http://www.filepicker.io/api/file/2HHH3?base64decode=true&policy=P&signature=S",
		},
	}

	var reqURL, reqMethod, reqBody string
	handler := func(w http.ResponseWriter, req *http.Request) {
		body, _ := ioutil.ReadAll(req.Body)
		reqBody = string(body)
		reqURL = req.URL.String()
		reqMethod = req.Method
		if _, err := w.Write([]byte(downloadFileContent)); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}
	}

	blob := filepicker.NewBlob(FakeHandle)
	client := filepicker.NewClient(FakeApiKey)
	mock := MockServer(t, client, handler)
	defer mock.Close()

	for i, test := range tests {
		var buff bytes.Buffer
		byteRead, err := client.DownloadTo(blob, test.Opt, &buff)
		if err != nil {
			t.Errorf("want err == nil; got %v (i:%d)", err, i)
		}
		if l := int64(len(downloadFileContent)); l != byteRead {
			t.Errorf("want byteRead == %d; got %d (i:%d)", l, byteRead, i)
		}
		if test.URL != reqURL {
			t.Errorf("want reqURL == %q; got %q (i:%d)", test.URL, reqURL, i)
		}
		if reqMethod != "GET" {
			t.Errorf("want reqMethod == GET; got %q (i:%d)", reqMethod, i)
		}
		content := string(buff.Bytes())
		if content != downloadFileContent {
			t.Errorf("want content == %q; got %q (i:%d)", downloadFileContent, content, i)
		}
	}
}