Exemple #1
0
        public void UploadImageBinaryRequest_WithUrlNull_ThrowsArgumentNullException()
        {
            var requestBuilder = new ImageRequestBuilder();
            var image          = File.ReadAllBytes("banana.gif");

            requestBuilder.UploadImageBinaryRequest(null, image);
        }
Exemple #2
0
        public async Task UploadImageBinaryRequest_AreEqual()
        {
            var client         = new ImgurClient("123", "1234");
            var requestBuilder = new ImageRequestBuilder();
            var url            = $"{client.EndpointUrl}image";

            var image   = File.ReadAllBytes("banana.gif");
            var request = requestBuilder.UploadImageBinaryRequest(url, image, "TheAlbum", "TheTitle",
                                                                  "TheDescription");

            Assert.IsNotNull(request);
            Assert.AreEqual("https://api.imgur.com/3/image", request.RequestUri.ToString());
            Assert.AreEqual(HttpMethod.Post, request.Method);

            var content      = (MultipartFormDataContent)request.Content;
            var imageContent =
                (ByteArrayContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "image");
            var album       = (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "album");
            var type        = (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "type");
            var title       = (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "title");
            var description =
                (StringContent)content.FirstOrDefault(x => x.Headers.ContentDisposition.Name == "description");

            Assert.IsNotNull(imageContent);
            Assert.IsNotNull(type);
            Assert.IsNotNull(album);
            Assert.IsNotNull(title);
            Assert.IsNotNull(description);

            Assert.AreEqual(image.Length, imageContent.Headers.ContentLength);
            Assert.AreEqual("file", await type.ReadAsStringAsync());
            Assert.AreEqual("TheAlbum", await album.ReadAsStringAsync());
            Assert.AreEqual("TheTitle", await title.ReadAsStringAsync());
            Assert.AreEqual("TheDescription", await description.ReadAsStringAsync());
        }
Exemple #3
0
        public void UploadImageBinaryRequest_WithImageNull_ThrowsArgumentNullException()
        {
            var client         = new ImgurClient("123", "1234");
            var requestBuilder = new ImageRequestBuilder();
            var url            = $"{client.EndpointUrl}image";

            requestBuilder.UploadImageBinaryRequest(url, null);
        }
Exemple #4
0
        /// <summary>
        ///     Upload a new image using a binary file.
        /// </summary>
        /// <param name="image">A binary file.</param>
        /// <param name="albumId">
        ///     The id of the album you want to add the image to. For anonymous albums, {albumId} should be the
        ///     deletehash that is returned at creation.
        /// </param>
        /// <param name="name">The name of the file.</param>
        /// <param name="title">The title of the image.</param>
        /// <param name="description">The description of the image.</param>
        /// <exception cref="ArgumentNullException">
        ///     Thrown when a null reference is passed to a method that does not accept it as a
        ///     valid argument.
        /// </exception>
        /// <exception cref="ImgurException">Thrown when an error is found in a response from an Imgur endpoint.</exception>
        /// <exception cref="MashapeException">Thrown when an error is found in a response from a Mashape endpoint.</exception>
        /// <returns></returns>
        private async Task <IImage> UploadImageBinaryInternalAsync(byte[] image, string albumId = null, string name = null, string title = null,
                                                                   string description           = null)
        {
            const string url = nameof(image);

            using (var request = ImageRequestBuilder.UploadImageBinaryRequest(url, image, albumId, name, title, description))
            {
                var returnImage = await SendRequestAsync <Image>(request).ConfigureAwait(false);

                return(returnImage);
            }
        }
Exemple #5
0
        public void UploadImageUrlRequest_WithUrlNull_ThrowsArgumentNullException()
        {
            var requestBuilder = new ImageRequestBuilder();

            var exception = Record.Exception(() => ImageRequestBuilder.UploadImageBinaryRequest(null, new byte[9]));

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

            var argNullException = (ArgumentNullException)exception;

            Assert.Equal(argNullException.ParamName, "url");
        }
Exemple #6
0
        public void UploadImageBinaryRequest_WithImageNull_ThrowsArgumentNullException()
        {
            var client         = new ImgurClient("123", "1234");
            var requestBuilder = new ImageRequestBuilder();
            var mockUrl        = $"{client.EndpointUrl}image";

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

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

            var argNullException = (ArgumentNullException)exception;

            Assert.Equal(argNullException.ParamName, "image");
        }
        /// <summary>
        ///     Upload a new image using a binary file.
        /// </summary>
        /// <param name="image">A binary file.</param>
        /// <param name="albumId">
        ///     The id of the album you want to add the image to. For anonymous albums, {albumId} should be the
        ///     deletehash that is returned at creation.
        /// </param>
        /// <param name="name">The name of the file.</param>
        /// <param name="title">The title of the image.</param>
        /// <param name="description">The description of the image.</param>
        /// <exception cref="ArgumentNullException">
        ///     Thrown when a null reference is passed to a method that does not accept it as a
        ///     valid argument.
        /// </exception>
        /// <exception cref="ImgurException">Thrown when an error is found in a response from an Imgur endpoint.</exception>
        /// <exception cref="MashapeException">Thrown when an error is found in a response from a Mashape endpoint.</exception>
        /// <returns></returns>
        public Basic <Image> UploadImageBinary(byte[] image, string albumId = null, string name = null, string title = null, string description = null)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            const string url = nameof(image);

            using (var request = ImageRequestBuilder.UploadImageBinaryRequest(url, image, albumId, name, title, description))
            {
                var httpResponse = HttpClient.SendAsync(request).Result;
                var jsonString   = httpResponse.Content.ReadAsStringAsync().Result;
                var output       = Newtonsoft.Json.JsonConvert.DeserializeObject <Basic <Image> >(httpResponse.Content.ReadAsStringAsync().Result.ToString());
                return(output);
            }
        }
Exemple #8
0
        /// <summary>
        ///     Upload a new image using a binary file.
        /// </summary>
        /// <param name="image">A binary file.</param>
        /// <param name="albumId">
        ///     The id of the album you want to add the image to. For anonymous albums, {albumId} should be the
        ///     deletehash that is returned at creation.
        /// </param>
        /// <param name="name">The name of the file.</param>
        /// <param name="title">The title of the image.</param>
        /// <param name="description">The description of the image.</param>
        /// <exception cref="ArgumentNullException">
        ///     Thrown when a null reference is passed to a method that does not accept it as a
        ///     valid argument.
        /// </exception>
        /// <exception cref="ImgurException">Thrown when an error is found in a response from an Imgur endpoint.</exception>
        /// <exception cref="MashapeException">Thrown when an error is found in a response from a Mashape endpoint.</exception>
        /// <returns></returns>
        public async Task <IImage> UploadImageBinaryAsync(byte[] image, string albumId = null, string name = null, string title = null,
                                                          string description           = null)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            const string url = nameof(image);

            using (var request = ImageRequestBuilder.UploadImageBinaryRequest(url, image, albumId, name, title, description))
            {
                var returnImage = await SendRequestAsync <Image>(request).ConfigureAwait(false);

                return(returnImage);
            }
        }
Exemple #9
0
        public void UploadImageUrlRequest_WithUrlNull_ThrowsArgumentNullException()
        {
            var requestBuilder = new ImageRequestBuilder();

            requestBuilder.UploadImageBinaryRequest(null, null);
        }