MCPcopy Index your code
hub / github.com/coder/coder / TestDownload

Function TestDownload

coderd/files_test.go:178–261  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

176}
177
178func TestDownload(t *testing.T) {
179 t.Parallel()
180
181 // Shared instance — see TestPostFiles for rationale.
182 client := coderdtest.New(t, nil)
183 _ = coderdtest.CreateFirstUser(t, client)
184 t.Run("NotFound", func(t *testing.T) {
185 t.Parallel()
186 ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
187 defer cancel()
188
189 _, _, err := client.Download(ctx, uuid.New())
190 var apiErr *codersdk.Error
191 require.ErrorAs(t, err, &apiErr)
192 require.Equal(t, http.StatusNotFound, apiErr.StatusCode())
193 })
194
195 t.Run("InsertTar_DownloadTar", func(t *testing.T) {
196 t.Parallel()
197 // given
198 ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
199 defer cancel()
200
201 tarball := archivetest.TestTarFileBytes()
202
203 // when
204 resp, err := client.Upload(ctx, codersdk.ContentTypeTar, bytes.NewReader(tarball))
205 require.NoError(t, err)
206 data, contentType, err := client.Download(ctx, resp.ID)
207 require.NoError(t, err)
208
209 // then
210 require.Len(t, data, len(tarball))
211 require.Equal(t, codersdk.ContentTypeTar, contentType)
212 require.Equal(t, tarball, data)
213 archivetest.AssertSampleTarFile(t, data)
214 })
215
216 t.Run("InsertZip_DownloadTar", func(t *testing.T) {
217 t.Parallel()
218 // given
219 zipContent := archivetest.TestZipFileBytes()
220
221 ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
222 defer cancel()
223
224 // when
225 resp, err := client.Upload(ctx, codersdk.ContentTypeZip, bytes.NewReader(zipContent))
226 require.NoError(t, err)
227 data, contentType, err := client.Download(ctx, resp.ID)
228 require.NoError(t, err)
229
230 // then
231 require.Equal(t, codersdk.ContentTypeTar, contentType)
232
233 // Note: creating a zip from a tar will result in some loss of information
234 // as zip files do not store UNIX user:group data.
235 archivetest.AssertSampleTarFile(t, data)

Callers

nothing calls this directly

Calls 15

StatusCodeMethod · 0.95
NewFunction · 0.92
CreateFirstUserFunction · 0.92
TestTarFileBytesFunction · 0.92
AssertSampleTarFileFunction · 0.92
TestZipFileBytesFunction · 0.92
CreateZipFromTarFunction · 0.92
AssertSampleZipFileFunction · 0.92
DownloadMethod · 0.80
DownloadWithFormatMethod · 0.80
RunMethod · 0.65
NewMethod · 0.65

Tested by

no test coverage detected