Add the ability to mock the http client and a test.

This commit is contained in:
Justin Hawkins 2021-10-11 20:25:53 +10:30
parent 244cd7b9da
commit 2a3f4ea21a
2 changed files with 68 additions and 6 deletions

View File

@ -15,6 +15,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
"errors"
"github.com/fogleman/gg" "github.com/fogleman/gg"
"github.com/tardisx/discord-auto-upload/config" "github.com/tardisx/discord-auto-upload/config"
@ -22,6 +23,10 @@ import (
"golang.org/x/image/font/inconsolata" "golang.org/x/image/font/inconsolata"
) )
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
type Uploader struct { type Uploader struct {
Uploads []*Upload `json:"uploads"` Uploads []*Upload `json:"uploads"`
} }
@ -43,6 +48,8 @@ type Upload struct {
Width int `json:"width"` Width int `json:"width"`
Height int `json:"height"` Height int `json:"height"`
Client HTTPClient
} }
func NewUploader() *Uploader { func NewUploader() *Uploader {
@ -73,10 +80,10 @@ func (u *Uploader) Upload() {
} }
} }
func (u *Upload) processUpload() { func (u *Upload) processUpload() error {
if u.webhookURL == "" { if u.webhookURL == "" {
daulog.SendLog("WebHookURL is not configured - cannot upload!", daulog.LogTypeError) daulog.SendLog("WebHookURL is not configured - cannot upload!", daulog.LogTypeError)
return return errors.New("webhook url not configured")
} }
if u.watermark { if u.watermark {
@ -112,11 +119,18 @@ func (u *Upload) processUpload() {
request, err := newfileUploadRequest(u.webhookURL, extraParams, "file", u.filenameToUpload) request, err := newfileUploadRequest(u.webhookURL, extraParams, "file", u.filenameToUpload)
if err != nil { if err != nil {
log.Fatal(err) log.Printf("error creating upload request: %s", err)
return fmt.Errorf("could not create upload request: %s", err)
} }
start := time.Now() start := time.Now()
client := &http.Client{Timeout: time.Second * 30}
resp, err := client.Do(request) if u.Client == nil {
// if no client was specified (a unit test) then create
// a default one
u.Client = &http.Client{Timeout: time.Second * 30}
}
resp, err := u.Client.Do(request)
if err != nil { if err != nil {
log.Print("Error performing request:", err) log.Print("Error performing request:", err)
retriesRemaining-- retriesRemaining--
@ -190,12 +204,13 @@ func (u *Upload) processUpload() {
if retriesRemaining == 0 { if retriesRemaining == 0 {
daulog.SendLog("Failed to upload, even after all retries", daulog.LogTypeError) daulog.SendLog("Failed to upload, even after all retries", daulog.LogTypeError)
} }
return nil
} }
func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) { func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("could not open file '%s': %s", path, err)
} }
defer file.Close() defer file.Close()

47
upload/upload_test.go Normal file
View File

@ -0,0 +1,47 @@
package upload
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
"os"
// "github.com/tardisx/discord-auto-upload/config"
)
// https://www.thegreatcodeadventure.com/mocking-http-requests-in-golang/
type MockClient struct {
DoFunc func(req *http.Request) (*http.Response, error)
}
func (m *MockClient) Do(req *http.Request) (*http.Response, error) {
return m.DoFunc(req)
}
func DoGoodUpload(req *http.Request) (*http.Response, error) {
r := ioutil.NopCloser(bytes.NewReader([]byte(`{"id": "123456789012345678", "type": 0, "content": "", "channel_id": "849615269706203171", "author": {"bot": true, "id": "849615314274484224", "username": "abcdedf", "avatar": null, "discriminator": "0000"}, "attachments": [{"id": "851092588332449812", "filename": "dau480457962.png", "size": 859505, "url": "https://cdn.discordapp.com/attachments/849615269706203171/851092588332449812/dau480457962.png", "proxy_url": "https://media.discordapp.net/attachments/849615269706203171/851092588332449812/dau480457962.png", "width": 640, "height": 640, "content_type": "image/png"}], "embeds": [], "mentions": [], "mention_roles": [], "pinned": false, "mention_everyone": false, "tts": false, "timestamp": "2021-06-06T13:38:05.660000+00:00", "edited_timestamp": null, "flags": 0, "components": [], "webhook_id": "123456789012345678"}`)))
return &http.Response{
StatusCode: 200,
Body: r,
}, nil
}
func TestSuccessfulUpload(t *testing.T) {
// create temporary file, processUpload requires that it exists, even though
// we will not really be uploading it here
f, _ := os.CreateTemp("", "dautest-upload-*")
defer os.Remove(f.Name())
u := Upload{webhookURL: "https://127.0.0.1/", originalFilename: f.Name()}
u.Client = &MockClient{DoFunc: DoGoodUpload}
err := u.processUpload()
if err != nil {
t.Errorf("error occured: %s", err)
}
if u.Width != 640 || u.Height != 640 {
t.Error("dimensions wrong")
}
if u.Url != "https://cdn.discordapp.com/attachments/849615269706203171/851092588332449812/dau480457962.png" {
t.Error("URL wrong")
}
}