示例#1
0
        private async Task <IImage> UploadVideoInternalAsync(Stream image,
                                                             string album                        = null,
                                                             string name                         = null,
                                                             string title                        = null,
                                                             string description                  = null,
                                                             IProgress <int> progress            = null,
                                                             int?bufferSize                      = 4096,
                                                             CancellationToken cancellationToken = default)
        {
            const string url = "upload";

            using (var request = ImageRequestBuilder.UploadVideoStreamRequest(url,
                                                                              image,
                                                                              album,
                                                                              name,
                                                                              title,
                                                                              description,
                                                                              progress,
                                                                              bufferSize))
            {
                var response = await SendRequestAsync <Image>(request,
                                                              cancellationToken).ConfigureAwait(false);

                return(response);
            }
        }
        public void UploadVideoStreamRequest_WithUrlNull_ThrowsArgumentNullException()
        {
            using var ms = new MemoryStream(new byte[9]);
            var exception = Record.Exception(() => ImageRequestBuilder.UploadVideoStreamRequest(null, ms));

            Assert.NotNull(exception);
            Assert.IsType <ArgumentNullException>(exception);

            var argNullException = (ArgumentNullException)exception;

            Assert.Equal("url", argNullException.ParamName);
        }
        public void UploadVideoStreamRequest_WithVideoNull_ThrowsArgumentNullException()
        {
            var apiClient = new ApiClient("123");
            var mockUrl   = $"{apiClient.BaseAddress}upload";

            var exception = Record.Exception(() => ImageRequestBuilder.UploadVideoStreamRequest(mockUrl, null));

            Assert.NotNull(exception);
            Assert.IsType <ArgumentNullException>(exception);

            var argNullException = (ArgumentNullException)exception;

            Assert.Equal("video", argNullException.ParamName);
        }
        public async Task UploadVideoStreamProgressRequest_Equal()
        {
            var apiClient = new ApiClient("123");
            var mockUrl   = $"{apiClient.BaseAddress}upload";

            using var ms = new MemoryStream(new byte[9]);
            var imageLength     = ms.Length;
            var currentProgress = 0;

            int report(int progress) => currentProgress = progress;

            var progress = new Progress <int>(percent => report(percent));

            using var request = ImageRequestBuilder.UploadVideoStreamRequest(mockUrl, ms, "TheAlbum", "TheName", "TheTitle",
                                                                             "TheDescription", progress, 9999);

            Assert.NotNull(request);
            Assert.Equal("https://api.imgur.com/3/upload", request.RequestUri.ToString());
            Assert.Equal(HttpMethod.Post, request.Method);

            var content       = (MultipartFormDataContent)request.Content;
            var streamContent =
                (ProgressStreamContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "video");
            var type        = (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "type");
            var name        = streamContent.Headers.ContentDisposition.FileName;
            var album       = (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "album");
            var title       = (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "title");
            var description =
                (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "description");

            Assert.NotNull(streamContent);
            Assert.Equal(9999, streamContent._bufferSize);
            Assert.NotNull(type);
            Assert.NotNull(name);
            Assert.NotNull(album);
            Assert.NotNull(title);
            Assert.NotNull(description);

            var image = await streamContent.ReadAsByteArrayAsync();

            Assert.Equal(imageLength, image.Length);
            Assert.Equal("file", await type.ReadAsStringAsync());
            Assert.Equal("TheName", name);
            Assert.Equal("TheAlbum", await album.ReadAsStringAsync());
            Assert.Equal("TheTitle", await title.ReadAsStringAsync());
            Assert.Equal("TheDescription", await description.ReadAsStringAsync());
        }