mirror of
https://github.com/esimov/caire.git
synced 2025-10-13 20:33:52 +08:00
test: extended test cases with the utility methods
This commit is contained in:
@@ -52,3 +52,27 @@ func IsValidUrl(uri string) bool {
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DetectFileContentType detects the file type by reading MIME type information of the file content.
|
||||||
|
func DetectFileContentType(fname string) (interface{}, error) {
|
||||||
|
file, err := os.Open(fname)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
// Only the first 512 bytes are used to sniff the content type.
|
||||||
|
buffer := make([]byte, 512)
|
||||||
|
_, err = file.Read(buffer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset the read pointer if necessary.
|
||||||
|
file.Seek(0, 0)
|
||||||
|
|
||||||
|
// Always returns a valid content-type and "application/octet-stream" if no others seemed to match.
|
||||||
|
contentType := http.DetectContentType(buffer)
|
||||||
|
|
||||||
|
return string(contentType), nil
|
||||||
|
}
|
||||||
|
38
utils/download_test.go
Normal file
38
utils/download_test.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUtils_ShouldDownloadImage(t *testing.T) {
|
||||||
|
f, err := DownloadImage("https://raw.githubusercontent.com/esimov/caire/master/testdata/sample.jpg")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("could't download test file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(f.Name(), "tmp") {
|
||||||
|
t.Errorf("The downloaded image should have been saved in a temporary folder")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUtils_ShouldBeValidUrl(t *testing.T) {
|
||||||
|
ok := IsValidUrl("https://github.com/esimov/caire/")
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("A valid URL should have been provided")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUtils_ShouldDetectValidFileType(t *testing.T) {
|
||||||
|
sampleImg := filepath.Join("../testdata", "sample.jpg")
|
||||||
|
|
||||||
|
ftype, err := DetectFileContentType(sampleImg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("could not detect content type: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(ftype.(string), "image") {
|
||||||
|
t.Errorf("Content type expected to be of type image, got: %v", ftype)
|
||||||
|
}
|
||||||
|
}
|
@@ -3,8 +3,6 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -66,27 +64,3 @@ func FormatTime(d time.Duration) string {
|
|||||||
int64(d.Hours()/24), int64(remainingHours),
|
int64(d.Hours()/24), int64(remainingHours),
|
||||||
int64(remainingMinutes), remainingSeconds)
|
int64(remainingMinutes), remainingSeconds)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DetectFileContentType detects the file type by reading MIME type information of the file content.
|
|
||||||
func DetectFileContentType(fname string) (interface{}, error) {
|
|
||||||
file, err := os.Open(fname)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
// Only the first 512 bytes are used to sniff the content type.
|
|
||||||
buffer := make([]byte, 512)
|
|
||||||
_, err = file.Read(buffer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset the read pointer if necessary.
|
|
||||||
file.Seek(0, 0)
|
|
||||||
|
|
||||||
// Always returns a valid content-type and "application/octet-stream" if no others seemed to match.
|
|
||||||
contentType := http.DetectContentType(buffer)
|
|
||||||
|
|
||||||
return string(contentType), nil
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user