Skip to content

Commit 5de742b

Browse files
committed
Coverage
1 parent d774bdd commit 5de742b

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

Diff for: attachments.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,6 @@ func FetchAndStoreAttachment(ctx context.Context, b Backend, channel Channel, at
113113
extension = extension[1:]
114114
}
115115

116-
// first try getting our mime type from the first 300 bytes of our body
117-
fileType, _ := filetype.Match(trace.ResponseBody[:300])
118-
if fileType != filetype.Unknown {
119-
mimeType = fileType.MIME.Value
120-
extension = fileType.Extension
121-
} else {
122-
// if that didn't work, try from our extension
123-
fileType = filetype.GetType(extension)
124-
if fileType != filetype.Unknown {
125-
mimeType = fileType.MIME.Value
126-
extension = fileType.Extension
127-
}
128-
}
129-
130116
// prioritize to use the response content type header if provided
131117
contentTypeHeader := trace.Response.Header.Get("Content-Type")
132118
if contentTypeHeader != "" {
@@ -139,6 +125,21 @@ func FetchAndStoreAttachment(ctx context.Context, b Backend, channel Channel, at
139125
extension = extensions[0][1:]
140126
}
141127
}
128+
} else {
129+
130+
// first try getting our mime type from the first 300 bytes of our body
131+
fileType, _ := filetype.Match(trace.ResponseBody[:300])
132+
if fileType != filetype.Unknown {
133+
mimeType = fileType.MIME.Value
134+
extension = fileType.Extension
135+
} else {
136+
// if that didn't work, try from our extension
137+
fileType = filetype.GetType(extension)
138+
if fileType != filetype.Unknown {
139+
mimeType = fileType.MIME.Value
140+
extension = fileType.Extension
141+
}
142+
}
142143
}
143144

144145
storageURL, err := b.SaveAttachment(ctx, channel, mimeType, trace.ResponseBody, extension)

Diff for: attachments_test.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ func TestFetchAndStoreAttachment(t *testing.T) {
2020
"http://mock.com/media/hello.jpg": {
2121
httpx.NewMockResponse(200, nil, testJPG),
2222
},
23+
"http://mock.com/media/hello2": {
24+
httpx.NewMockResponse(200, map[string]string{"Content-Type": "image/jpeg"}, testJPG),
25+
},
2326
"http://mock.com/media/hello.mp3": {
2427
httpx.NewMockResponse(502, nil, []byte(`My gateways!`)),
2528
},
@@ -53,15 +56,26 @@ func TestFetchAndStoreAttachment(t *testing.T) {
5356
assert.Len(t, clog.HTTPLogs(), 1)
5457
assert.Equal(t, "http://mock.com/media/hello.jpg", clog.HTTPLogs()[0].URL)
5558

59+
att, err = courier.FetchAndStoreAttachment(ctx, mb, mockChannel, "http://mock.com/media/hello2", clog)
60+
assert.NoError(t, err)
61+
assert.Equal(t, "image/jpeg", att.ContentType)
62+
assert.Equal(t, "https://backend.com/attachments/547deaf7-7620-4434-95b3-58675999c4b7.jpe", att.URL)
63+
assert.Equal(t, 17301, att.Size)
64+
65+
assert.Len(t, mb.SavedAttachments(), 2)
66+
assert.Equal(t, &test.SavedAttachment{Channel: mockChannel, ContentType: "image/jpeg", Data: testJPG, Extension: "jpg"}, mb.SavedAttachments()[0])
67+
assert.Len(t, clog.HTTPLogs(), 2)
68+
assert.Equal(t, "http://mock.com/media/hello2", clog.HTTPLogs()[1].URL)
69+
5670
// a non-200 response should return an unavailable attachment
5771
att, err = courier.FetchAndStoreAttachment(ctx, mb, mockChannel, "http://mock.com/media/hello.mp3", clog)
5872
assert.NoError(t, err)
5973
assert.Equal(t, &courier.Attachment{ContentType: "unavailable", URL: "http://mock.com/media/hello.mp3"}, att)
6074

6175
// should have a logged HTTP request but no attachments will have been saved to storage
62-
assert.Len(t, clog.HTTPLogs(), 2)
63-
assert.Equal(t, "http://mock.com/media/hello.mp3", clog.HTTPLogs()[1].URL)
64-
assert.Len(t, mb.SavedAttachments(), 1)
76+
assert.Len(t, clog.HTTPLogs(), 3)
77+
assert.Equal(t, "http://mock.com/media/hello.mp3", clog.HTTPLogs()[2].URL)
78+
assert.Len(t, mb.SavedAttachments(), 2)
6579

6680
// same for a connection error
6781
att, err = courier.FetchAndStoreAttachment(ctx, mb, mockChannel, "http://mock.com/media/hello.pdf", clog)

0 commit comments

Comments
 (0)