public async Task TestDownload() { byte[] data = { 1, 2, 3 }; Mock.Expect(HttpMethod.Get, "http://localhost/endpoint") .Respond(_ => new ByteArrayContent(data)); using (var downloadStream = await _endpoint.DownloadAsync()) using (var memStream = new MemoryStream()) { await downloadStream.CopyToAsync(memStream); memStream.ToArray().Should().Equal(data); } }
/// <summary> /// Downloads the blob's content to a file. /// </summary> /// <param name="endpoint">The blob endpoint.</param> /// <param name="path">The path of the file to write the download data to.</param> /// <param name="cancellationToken">Used to cancel the request.</param> /// <returns>A stream with the blob's content.</returns> /// <exception cref="InvalidDataException"><see cref="HttpStatusCode.BadRequest"/></exception> /// <exception cref="AuthenticationException"><see cref="HttpStatusCode.Unauthorized"/></exception> /// <exception cref="UnauthorizedAccessException"><see cref="HttpStatusCode.Forbidden"/></exception> /// <exception cref="KeyNotFoundException"><see cref="HttpStatusCode.NotFound"/> or <see cref="HttpStatusCode.Gone"/></exception> /// <exception cref="InvalidOperationException"><see cref="HttpStatusCode.Conflict"/></exception> /// <exception cref="HttpRequestException">Other non-success status code.</exception> public static async Task DownloadAsync(this IBlobEndpoint endpoint, string path, CancellationToken cancellationToken = default) { using (var fileStream = File.Create(path)) using (var downloadStream = await endpoint.DownloadAsync(cancellationToken)) await downloadStream.CopyToAsync(fileStream); }