public void ValidTestPostJson()
        {
            //Arrange
            Base64ImageModel[] imageInfoArray = new Base64ImageModel[2];

            using var stream = new FileStream("Data\\Desert.jpg", FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] array = new byte[stream.Length];
            stream.Read(array, 0, Convert.ToInt32(stream.Length));

            using var stream2 = new FileStream("Data\\Chrysanthemum.jpg", FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] array2 = new byte[stream.Length];
            stream.Read(array, 0, Convert.ToInt32(stream.Length));

            imageInfoArray[0] = new Base64ImageModel()
            {
                data = array, fileName = "Desert.jpg"
            };
            imageInfoArray[1] = new Base64ImageModel()
            {
                data = array, fileName = "Chrysanthemum.jpg"
            };

            //Act
            var result = _controller.PostJson(imageInfoArray);

            //Assert
            Assert.IsType <OkResult>(result.Result);
        }
Exemplo n.º 2
0
        public async Task Post_EndpointsReturnSuccess(string url)
        {
            // Arrange
            var client      = _factory.CreateClient();
            var postContent = new MultipartContent();

            //Define uploading files
            using var stream = new FileStream("Data/Desert.jpg", FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] file1 = new byte[stream.Length];
            await stream.ReadAsync(file1, 0, Convert.ToInt32(stream.Length));

            using var stream2 = new FileStream("Data/Chrysanthemum.jpg", FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] file2 = new byte[stream2.Length];
            await stream2.ReadAsync(file2, 0, Convert.ToInt32(stream.Length));

            #region form
            MultipartFormDataContent form = new MultipartFormDataContent();

            form.Add(new ByteArrayContent(file1), "file1", "Desert.jpg");
            form.Add(new ByteArrayContent(file1), "file2", "Chrysanthemum.jpg");
            #endregion
            #region json
            Base64ImageModel[] imageInfoArray = new Base64ImageModel[2];
            imageInfoArray[0] = new Base64ImageModel()
            {
                data = file1, fileName = "Desert.jpg"
            };
            imageInfoArray[1] = new Base64ImageModel()
            {
                data = file2, fileName = "Chrysanthemum.jpg"
            };

            string jsonValue = JsonConvert.SerializeObject(imageInfoArray);

            #endregion
            #region url
            string imageUrl = "https://cdn.pixabay.com/photo/2020/04/17/14/16/landscape-5055384_960_720.jpg";


            var query = HttpUtility.ParseQueryString("");
            query["url"] = imageUrl;
            string imageContentUrl = url + "?" + query.ToString();

            #endregion

            // Act
            var formResponse = await client.PostAsync(url, form);

            var jsonResponse = await client.PostAsync(url, new StringContent(jsonValue, Encoding.UTF8, "application/json"));

            var urlResponse = await client.PostAsync(imageContentUrl, new StringContent(""));

            // Assert
            formResponse.EnsureSuccessStatusCode(); // Status Code 200-299
            jsonResponse.EnsureSuccessStatusCode();
            urlResponse.EnsureSuccessStatusCode();
        }
        public void InvalidTestPostJson()
        {
            //Arrange
            Base64ImageModel[] imageInfoArray  = new Base64ImageModel[1];
            Base64ImageModel[] imageInfoArray2 = new Base64ImageModel[1];

            imageInfoArray2[0] = new Base64ImageModel()
            {
                fileName = "Desert"
            };

            //Act
            var result  = _controller.PostJson(imageInfoArray);
            var result2 = _controller.PostJson(imageInfoArray2);

            //Assert
            Assert.IsNotType <OkResult>(result.Result);
            Assert.IsNotType <OkResult>(result2.Result);
        }