From 2a3f4ea21af08f759eb5089122e2b9f70ab6b1e2 Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Mon, 11 Oct 2021 20:25:53 +1030 Subject: [PATCH] Add the ability to mock the http client and a test. --- upload/upload.go | 27 +++++++++++++++++++------ upload/upload_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 upload/upload_test.go diff --git a/upload/upload.go b/upload/upload.go index 1a34b25..bf7d924 100644 --- a/upload/upload.go +++ b/upload/upload.go @@ -15,6 +15,7 @@ import ( "os" "path/filepath" "time" + "errors" "github.com/fogleman/gg" "github.com/tardisx/discord-auto-upload/config" @@ -22,6 +23,10 @@ import ( "golang.org/x/image/font/inconsolata" ) +type HTTPClient interface { + Do(req *http.Request) (*http.Response, error) +} + type Uploader struct { Uploads []*Upload `json:"uploads"` } @@ -43,6 +48,8 @@ type Upload struct { Width int `json:"width"` Height int `json:"height"` + + Client HTTPClient } func NewUploader() *Uploader { @@ -73,10 +80,10 @@ func (u *Uploader) Upload() { } } -func (u *Upload) processUpload() { +func (u *Upload) processUpload() error { if u.webhookURL == "" { daulog.SendLog("WebHookURL is not configured - cannot upload!", daulog.LogTypeError) - return + return errors.New("webhook url not configured") } if u.watermark { @@ -112,11 +119,18 @@ func (u *Upload) processUpload() { request, err := newfileUploadRequest(u.webhookURL, extraParams, "file", u.filenameToUpload) 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() - 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 { log.Print("Error performing request:", err) retriesRemaining-- @@ -190,12 +204,13 @@ func (u *Upload) processUpload() { if retriesRemaining == 0 { 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) { file, err := os.Open(path) if err != nil { - return nil, err + return nil, fmt.Errorf("could not open file '%s': %s", path, err) } defer file.Close() diff --git a/upload/upload_test.go b/upload/upload_test.go new file mode 100644 index 0000000..055744e --- /dev/null +++ b/upload/upload_test.go @@ -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") + } + +}