-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor code and add missing tests
- Loading branch information
1 parent
c586006
commit 112e517
Showing
11 changed files
with
237 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package foscam | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
) | ||
|
||
// Send a GET request to URL via the provided client. | ||
func getRequest(client HTTPClient, url string) ([]byte, error) { | ||
res, err := client.Get(url) | ||
if err != nil { | ||
return nil, &CameraError{err.Error()} | ||
} | ||
defer res.Body.Close() | ||
|
||
if res.StatusCode != 200 { | ||
return nil, &BadStatusError{URL: url, Status: res.StatusCode, Expected: http.StatusOK} | ||
} | ||
|
||
b, _ := io.ReadAll(res.Body) | ||
|
||
return b, nil | ||
} | ||
|
||
// Get a snapshot from the URL, via the provided client. | ||
func getSnap(client HTTPClient, url string) ([]byte, error) { | ||
b, err := getRequest(client, url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Check that camera returns a JPEG image | ||
if err = isJpeg(b); err != nil { | ||
return nil, err | ||
} | ||
|
||
return b, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package foscam | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/mdmfernandes/go-foscam/mocks" | ||
"github.com/mdmfernandes/go-foscam/testutil" | ||
) | ||
|
||
func Test_getRequest(t *testing.T) { | ||
// request() arguments | ||
type args struct { | ||
client *mocks.MockHTTPClient | ||
url string | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args *args | ||
want []byte | ||
wantErr error | ||
wantGetCalled int | ||
}{ | ||
{ | ||
name: "Camera error", | ||
args: &args{ | ||
client: &mocks.MockHTTPClient{ | ||
GetFunc: func(url string) (*http.Response, error) { | ||
return nil, errors.New("some error") | ||
}, | ||
}, | ||
url: "", | ||
}, | ||
want: nil, | ||
wantErr: &CameraError{"some error"}, | ||
wantGetCalled: 1, | ||
}, | ||
{ | ||
name: "Unexpected status code (404)", | ||
args: &args{ | ||
client: &mocks.MockHTTPClient{ | ||
GetFunc: func(url string) (*http.Response, error) { | ||
return &http.Response{ | ||
StatusCode: http.StatusNotFound, | ||
Body: io.NopCloser(bytes.NewBufferString("don't care")), | ||
}, nil | ||
}, | ||
}, | ||
url: "", | ||
}, | ||
want: nil, | ||
wantErr: &BadStatusError{"", http.StatusNotFound, http.StatusOK}, | ||
wantGetCalled: 1, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
b, err := getRequest(tt.args.client, tt.args.url) | ||
|
||
// Check how many times the client was called | ||
if tt.wantGetCalled != tt.args.client.GetCount { | ||
t.Errorf("Number of requests: got = %d; want = %d", tt.args.client.GetCount, tt.wantGetCalled) | ||
} | ||
|
||
// Check for errors | ||
if !testutil.EqualError(t, err, tt.wantErr) { | ||
t.Errorf("Error: got = %v; want = %v", err, tt.wantErr) | ||
} | ||
|
||
// Check for the response body | ||
if !bytes.Equal(b, tt.want) { | ||
t.Errorf("Response: got = %v; want = %v", b, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.