Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Arimeka committed Mar 16, 2020
1 parent 2e949ae commit 9d0458e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
17 changes: 17 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package mediaprobe_test
import (
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"time"
)

Expand Down Expand Up @@ -30,13 +32,28 @@ func ServeHttp(handler *Handler) *Server {
type Handler struct {
Status int
Filename string

FailOnAttempt uint64
Counter uint64
mu sync.RWMutex
}

func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if h.Status != 0 {
rw.WriteHeader(h.Status)
return
}
if h.FailOnAttempt > 0 {
h.mu.RLock()
counter := h.Counter
h.mu.RUnlock()
if counter >= h.FailOnAttempt {
rw.WriteHeader(http.StatusInternalServerError)
return
}

atomic.AddUint64(&h.Counter, 1)
}

http.ServeFile(rw, req, h.Filename)
}
61 changes: 58 additions & 3 deletions image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ func TestInfo_ParseImage(t *testing.T) {
t.Run("not_found", parseImageNotFound)
t.Run("not_found_remote", parseImageNotFoundRemote)
t.Run("conn_error_remote", parseImageConnectionErrorRemote)
t.Run("server_error_remote", parseImageServerErrorsRemote)
t.Run("valid", parseImageValid)
t.Run("valid_remote", parseImageValidRemote)
t.Run("invalid", parseImageInvalid)
t.Run("invalid_remote", parseImageInvalidRemote)
t.Run("with_orientation", parseImageWithOrientation)
t.Run("jpeg_without_exif", parseImageJPEGWithoutExif)
t.Run("png_without_exif", parseImagePNGWithoutExif)
Expand All @@ -37,7 +39,7 @@ func parseImageNotFound(t *testing.T) {

func parseImageNotFoundRemote(t *testing.T) {
handler := &Handler{
Filename: "./fixtures/image.jpeg",
Filename: testImageValidImage,
}
srv := ServeHttp(handler)
defer srv.Stop()
Expand All @@ -56,7 +58,7 @@ func parseImageNotFoundRemote(t *testing.T) {

func parseImageConnectionErrorRemote(t *testing.T) {
handler := &Handler{
Filename: "./fixtures/image.jpeg",
Filename: testImageValidImage,
}
srv := ServeHttp(handler)

Expand All @@ -72,6 +74,42 @@ func parseImageConnectionErrorRemote(t *testing.T) {
}
}

func parseImageServerErrorsRemote(t *testing.T) {
handler := &Handler{
Filename: testImageValidImage,
FailOnAttempt: 2,
}
srv := ServeHttp(handler)
defer srv.Stop()

info, err := mediaprobe.New(srv.Endpoint())
if err != nil {
t.Fatalf("Unexpected error %v", err)
}

err = info.ParseImage()
if err == nil {
t.Errorf("Expected to return error found but return nil")
}

handler = &Handler{
Filename: testImageValidImage,
FailOnAttempt: 3,
}
srv = ServeHttp(handler)
defer srv.Stop()

info, err = mediaprobe.New(srv.Endpoint())
if err != nil {
t.Fatalf("Unexpected error %v", err)
}

err = info.ParseImage()
if err == nil {
t.Errorf("Expected to return error found but return nil")
}
}

func parseImageValid(t *testing.T) {
info, err := mediaprobe.New(testImageValidImage)
if err != nil {
Expand All @@ -90,7 +128,7 @@ func parseImageValid(t *testing.T) {

func parseImageValidRemote(t *testing.T) {
handler := &Handler{
Filename: "./fixtures/image.jpeg",
Filename: testImageValidImage,
}
srv := ServeHttp(handler)
defer srv.Stop()
Expand Down Expand Up @@ -122,6 +160,23 @@ func parseImageInvalid(t *testing.T) {
}
}

func parseImageInvalidRemote(t *testing.T) {
handler := &Handler{
Filename: testImageInvalidImage,
}
srv := ServeHttp(handler)
defer srv.Stop()

info, err := mediaprobe.New(srv.Endpoint())
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
err = info.ParseImage()
if err == nil {
t.Errorf("Expected to return error but return nil")
}
}

func parseImageWithOrientation(t *testing.T) {
info, _ := mediaprobe.New(testImageWithExifOrientation)
err := info.ParseImage()
Expand Down
15 changes: 15 additions & 0 deletions info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func TestNew(t *testing.T) {
t.Run("not_found", newNotFound)
t.Run("not_found_remote", newNotFoundRemote)
t.Run("conn_error_remote", newConnErrorRemote)
t.Run("server_error_remote", newServerErrorRemote)
t.Run("local", newLocalFile)
t.Run("remote", newRemoteFile)
}
Expand Down Expand Up @@ -42,6 +43,20 @@ func newConnErrorRemote(t *testing.T) {
}
}

func newServerErrorRemote(t *testing.T) {
handler := &Handler{
Filename: "./fixtures/video.mp4",
FailOnAttempt: 1,
}
srv := ServeHttp(handler)
defer srv.Stop()

_, err := mediaprobe.New(srv.Endpoint())
if err == nil {
t.Errorf("Expected to return error found but return nil")
}
}

func newLocalFile(t *testing.T) {
info, err := mediaprobe.New("./fixtures/video.mp4")
if err != nil {
Expand Down

0 comments on commit 9d0458e

Please sign in to comment.