Skip to content

Commit f9dd363

Browse files
committed
Add TestClient_DownloadAttachment
1 parent b91714a commit f9dd363

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

download_attachment_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package tempmail
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/mock"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestClient_DownloadAttachment(t *testing.T) {
14+
t.Run("success", func(t *testing.T) {
15+
expectedContent := []byte("test attachment content")
16+
17+
mDoer := newMockDoer(t)
18+
mDoer.EXPECT().Do(mock.Anything).Return(newTestResponse(http.StatusOK, expectedContent), nil)
19+
20+
c := newClient()
21+
c.doer = mDoer
22+
content, resp, err := c.DownloadAttachment(context.Background(), "01JE97K1PBYVGKY0PVE3KXSBF9")
23+
require.NoError(t, err)
24+
assert.Equal(t, expectedContent, content)
25+
assert.Equal(t, 200, resp.StatusCode)
26+
})
27+
28+
t.Run("error from newRequest", func(t *testing.T) {
29+
c := newClient()
30+
_, _, err := c.DownloadAttachment(nil, "01JE97K1PBYVGKY0PVE3KXSBF9")
31+
assert.EqualError(t, err, "net/http: nil Context")
32+
})
33+
34+
t.Run("error from rawDo", func(t *testing.T) {
35+
mDoer := newMockDoer(t)
36+
mDoer.EXPECT().Do(mock.Anything).Return(nil, assert.AnError)
37+
38+
c := newClient()
39+
c.doer = mDoer
40+
_, _, err := c.DownloadAttachment(context.Background(), "01JE97K1PBYVGKY0PVE3KXSBF9")
41+
assert.EqualError(t, err, assert.AnError.Error())
42+
})
43+
44+
t.Run("error response", func(t *testing.T) {
45+
mDoer := newMockDoer(t)
46+
mDoer.EXPECT().Do(mock.Anything).Return(newTestResponse(http.StatusBadRequest, readFile(t, "testdata/error_response.json")), nil)
47+
48+
c := newClient()
49+
c.doer = mDoer
50+
_, _, err := c.DownloadAttachment(context.Background(), "nonexistent")
51+
require.Error(t, err)
52+
var httpErr *HTTPError
53+
require.ErrorAs(t, err, &httpErr)
54+
assert.Equal(t, "request_error", httpErr.ErrorDetails.Type)
55+
assert.Equal(t, "not_found", httpErr.ErrorDetails.Code)
56+
})
57+
58+
t.Run("io.ReadAll error", func(t *testing.T) {
59+
mReadCloser := newMockReadCloser(t)
60+
mReadCloser.EXPECT().Read(mock.Anything).Return(0, assert.AnError)
61+
mReadCloser.EXPECT().Close().Return(nil)
62+
63+
mDoer := newMockDoer(t)
64+
mDoer.EXPECT().Do(mock.Anything).Return(&http.Response{
65+
StatusCode: http.StatusOK,
66+
Body: mReadCloser,
67+
}, nil)
68+
69+
c := newClient()
70+
c.doer = mDoer
71+
_, _, err := c.DownloadAttachment(context.Background(), "01JE97K1PBYVGKY0PVE3KXSBF9")
72+
assert.EqualError(t, err, assert.AnError.Error())
73+
})
74+
}

0 commit comments

Comments
 (0)