コード例 #1
0
        public async Task DeleteProduct_Returns_NotFound()
        {
            using (var client = new TestClientProvider().Client)
            {
                client.DefaultRequestHeaders.Add("Api_Key", "MySceretApiKey");
                var response = await client.DeleteAsync("/api/product/DeleteProduct?id=" + Guid.Empty);

                Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
            }
        }
コード例 #2
0
        public async Task CreateProduct_Returns_Created_Product()
        {
            using (var client = new TestClientProvider().Client)
            {
                client.DefaultRequestHeaders.Add("Api_Key", "MySceretApiKey");
                Guid productid = Guid.Empty;
                var  payload   = JsonSerializer.Serialize(
                    new Product()
                {
                    productName = "Test Product",
                    description = "Duis aliquam convallis nunc. Proin at turpis a pede posuere nonummy. Integer non velit.\n\nDonec diam neque, vestibulum eget, vulputate ut, ultrices vel, augue. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec pharetra, magna vestibulum aliquet ultrices, erat tortor sollicitudin mi, sit amet lobortis sapien sapien non mi. Integer ac neque.",
                    color       = "Crimson",
                    publishDate = Convert.ToDateTime("2019-07-07T22:17:03Z"),
                    price       = 200,
                    photo       = "material_1.jpg"
                }
                    );


                HttpContent content = new StringContent(payload, Encoding.UTF8, "application/json");

                var response = await client.PostAsync($"/api/product/Create", content);


                using (var responseStream = await response.Content.ReadAsStreamAsync())
                {
                    var product = await JsonSerializer.DeserializeAsync <Product>(responseStream,
                                                                                  new JsonSerializerOptions()
                    {
                        PropertyNameCaseInsensitive = true
                    });

                    productid = product.id;

                    Assert.NotNull(product);
                    Assert.NotEqual <Guid>(Guid.Empty, productid);
                }

                var deleteResponse = await client.DeleteAsync($"/api/product/DeleteProduct?id={productid}");

                using (var deleteStream = await deleteResponse.Content.ReadAsStreamAsync())
                {
                    var deletedid = await JsonSerializer.DeserializeAsync <Guid>(deleteStream,
                                                                                 new JsonSerializerOptions()
                    {
                        PropertyNameCaseInsensitive = true
                    });
                }
            }
        }
コード例 #3
0
        public async Task DeleteProduct_ReturnsDeleted_Id()
        {
            using (var client = new TestClientProvider().Client)
            {
                Guid productId = Guid.Empty;


                // Skapa en produkt
                var payload = JsonSerializer.Serialize(
                    new Product()
                {
                    Description = "Testproduktbeskrivning",
                    Name        = "Testprodukt",
                    Price       = 123.45M,
                    ImageUrl    = "/images/Babolat.jpg"
                });

                HttpContent content = new StringContent(payload, Encoding.UTF8, "application/json");

                var createResponse = await client.PostAsync($"/api/products/create", content);

                using (var responseStream = await createResponse.Content.ReadAsStreamAsync())
                {
                    var product = await JsonSerializer.DeserializeAsync <Product>(responseStream,
                                                                                  new JsonSerializerOptions()
                    {
                        PropertyNameCaseInsensitive = true
                    });

                    productId = product.Id;
                }

                // Radera produkten

                var deleteResponse = await client.DeleteAsync($"/api/products/delete?id={productId}");

                using (var responseStream = await deleteResponse.Content.ReadAsStreamAsync())
                {
                    var deletedId = await JsonSerializer.DeserializeAsync <Guid>(responseStream,
                                                                                 new JsonSerializerOptions()
                    {
                        PropertyNameCaseInsensitive = true
                    });

                    Assert.Equal(productId, deletedId);
                }
            }
        }
コード例 #4
0
        public async void Dispose()
        {
            using (var client = new TestClientProvider().Client)
            {
                var deletedResponse = await client.DeleteAsync("/api/products/delete/" + Product.Id);

                using (var responseStream = await deletedResponse.Content.ReadAsStreamAsync())
                {
                    var deletedId = await JsonSerializer.DeserializeAsync <int>(responseStream,
                                                                                new JsonSerializerOptions()
                    {
                        PropertyNameCaseInsensitive = true
                    });
                }
            }
        }
コード例 #5
0
        public async Task CreateProduct_Returns_BadRequest()
        {
            Guid productid = Guid.Empty;

            using (var client = new TestClientProvider().Client)
            {
                client.DefaultRequestHeaders.Add("Api_Key", "MySceretApiKey");
                var payload = JsonSerializer.Serialize(
                    new Product()
                {
                });

                HttpContent content = new StringContent(payload, Encoding.UTF8, "application/json");

                var response = await client.PostAsync($"/api/product/Create", content);

                using (var responseStream = await response.Content.ReadAsStreamAsync())
                {
                    var product = await JsonSerializer.DeserializeAsync <Product>(responseStream,
                                                                                  new JsonSerializerOptions()
                    {
                        PropertyNameCaseInsensitive = true
                    });

                    productid = product.id;

                    Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
                }
                var deleteResponse = await client.DeleteAsync($"/api/product/DeleteProduct?id={productid}");

                using (var deleteStream = await deleteResponse.Content.ReadAsStreamAsync())
                {
                    var deletedid = await JsonSerializer.DeserializeAsync <Guid>(deleteStream,
                                                                                 new JsonSerializerOptions()
                    {
                        PropertyNameCaseInsensitive = true
                    });
                }
            }
        }