예제 #1
0
        private static async Task TestPatch(IEnumerable <KeyValuePair <string, string> > headers, HttpStatusCode expectedStatusCode, [CallerMemberName] string baselineFileName = null)
        {
            int id = ODataFeedTests.InsertTestProduct();

            string patchContentString = @"{
    ""@odata.type"":""#Microsoft.Restier.Samples.Northwind.Models.Product"",
    ""ProductName"":""Commons""
}";

            StringContent patchContent = new StringContent(patchContentString, UTF8Encoding.Default, "application/json");

            await ODataFeedTests.TestODataRequest("http://localhost/api/Northwind/Products(" + id + ")", new HttpMethod("PATCH"), patchContent, expectedStatusCode, headers, NormalizeProductId, baselineFileName);

            NorthwindContext ctx     = GetDbContext();
            Product          product = ctx.Products.FirstOrDefault(e => e.ProductID == id);

            Assert.Equal("Commons", product.ProductName);
            Assert.Equal(true, product.Discontinued);
            Assert.Equal("95", product.QuantityPerUnit);
            Assert.Equal((short)68, product.ReorderLevel);
            Assert.Equal(5.6m, product.UnitPrice);
            Assert.Equal((short)40, product.UnitsInStock);
            Assert.Equal((short)13, product.UnitsOnOrder);

            ctx.Products.Remove(product);
            ctx.SaveChanges();
        }
예제 #2
0
        private static async Task TestPut(IEnumerable <KeyValuePair <string, string> > headers, HttpStatusCode expectedStatusCode, [CallerMemberName] string baselineFileName = null)
        {
            int id = ODataFeedTests.InsertTestProduct();

            // NOTE: explicitly leaving CategoryID, Discontinued, QuantityPerUnit and UnitPrice
            // out of the PUT request content to test these values get set to their default value
            string putContentString = @"{
    ""@odata.type"":""#Microsoft.Restier.Samples.Northwind.Models.Product"",
    ""ProductName"":""TestPut"",
    ""ReorderLevel"":23,
    ""UnitsInStock"":15,
    ""UnitsOnOrder"":1,
    ""SupplierID"":1
}";

            StringContent putContent = new StringContent(putContentString, UTF8Encoding.Default, "application/json");

            await ODataFeedTests.TestODataRequest("http://localhost/api/Northwind/Products(" + id + ")", new HttpMethod("PUT"), putContent, expectedStatusCode, headers, NormalizeProductId, baselineFileName);

            NorthwindContext ctx     = GetDbContext();
            Product          product = ctx.Products.FirstOrDefault(e => e.ProductID == id);

            Assert.Equal("TestPut", product.ProductName);
            Assert.Equal(false, product.Discontinued);
            Assert.Null(product.QuantityPerUnit);
            Assert.Equal((short)23, product.ReorderLevel);
            Assert.Null(product.UnitPrice);
            Assert.Equal((short)15, product.UnitsInStock);
            Assert.Equal((short)1, product.UnitsOnOrder);
            Assert.Equal(1, product.SupplierID);
            Assert.Null(product.CategoryID);

            ctx.Products.Remove(product);
            ctx.SaveChanges();
        }
예제 #3
0
        public async Task TestBatch()
        {
            int id = ODataFeedTests.InsertTestProduct();

            string batchContentString =
                @"--batch_35114042-958d-48fd-8189-bd93264b31de
Content-Type: multipart/mixed; boundary=changeset_3ffaecfa-069f-4ad7-bb41-bcc2481ea0dd

--changeset_3ffaecfa-069f-4ad7-bb41-bcc2481ea0dd
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1

POST http://localhost/api/Northwind/Products HTTP/1.1
OData-Version: 4.0;NetFx
OData-MaxVersion: 4.0;NetFx
Content-Type: application/json;odata.metadata=minimal
Accept: application/json;odata.metadata=minimal
Accept-Charset: UTF-8
User-Agent: Microsoft ADO.NET Data Services

{""@odata.type"":""#Microsoft.Restier.Samples.Northwind.Models.Product"",""CategoryID"":null,""Discontinued"":false,""ProductID"":0,""ProductName"":""Horizon"",""QuantityPerUnit"":""4"",""ReorderLevel"":10,""SupplierID"":null,""UnitPrice"":2.5,""UnitsInStock"":100,""UnitsOnOrder"":0}
--changeset_3ffaecfa-069f-4ad7-bb41-bcc2481ea0dd
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 2

POST http://localhost/api/Northwind/Products HTTP/1.1
OData-Version: 4.0;NetFx
OData-MaxVersion: 4.0;NetFx
Content-Type: application/json;odata.metadata=minimal
Accept: application/json;odata.metadata=minimal
Accept-Charset: UTF-8
User-Agent: Microsoft ADO.NET Data Services

{""@odata.type"":""#Microsoft.Restier.Samples.Northwind.Models.Product"",""CategoryID"":null,""Discontinued"":true,""ProductID"":0,""ProductName"":""Commons"",""QuantityPerUnit"":""5"",""ReorderLevel"":11,""SupplierID"":null,""UnitPrice"":15.99,""UnitsInStock"":200,""UnitsOnOrder"":10}
--changeset_3ffaecfa-069f-4ad7-bb41-bcc2481ea0dd
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 4

DELETE http://localhost/api/Northwind/Products(" + id.ToString() + @") HTTP/1.1
OData-Version: 4.0;NetFx
OData-MaxVersion: 4.0;NetFx
Accept: application/json;odata.metadata=minimal
Accept-Charset: UTF-8
User-Agent: Microsoft ADO.NET Data Services


--changeset_3ffaecfa-069f-4ad7-bb41-bcc2481ea0dd--
--batch_35114042-958d-48fd-8189-bd93264b31de--
";

            StringContent batchContent = new StringContent(batchContentString, UTF8Encoding.Default);

            batchContent.Headers.ContentType = new MediaTypeHeaderValue("multipart/mixed");
            batchContent.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("boundary", "batch_35114042-958d-48fd-8189-bd93264b31de"));

            await ODataFeedTests.TestODataRequest("http://localhost/api/Northwind/$batch", HttpMethod.Post, batchContent, HttpStatusCode.OK, null, NormalizeBatch);

            ODataFeedTests.EnsureProductDeleted(id);

            NorthwindContext ctx = GetDbContext();

            Product[] insertedProducts = ctx.Products
                                         .Where(b => b.ProductName == "Horizon" || b.ProductName == "Commons")
                                         .OrderBy(b => b.ProductName)
                                         .ToArray();

            Assert.Equal(2, insertedProducts.Length);

            Assert.Equal("Commons", insertedProducts[0].ProductName);
            Assert.Equal(true, insertedProducts[0].Discontinued);
            Assert.Equal("5", insertedProducts[0].QuantityPerUnit);
            Assert.Equal((short)11, insertedProducts[0].ReorderLevel);
            Assert.Equal(15.99m, insertedProducts[0].UnitPrice);
            Assert.Equal((short)200, insertedProducts[0].UnitsInStock);
            Assert.Equal((short)10, insertedProducts[0].UnitsOnOrder);

            Assert.Equal("Horizon", insertedProducts[1].ProductName);
            Assert.Equal(false, insertedProducts[1].Discontinued);
            Assert.Equal("4", insertedProducts[1].QuantityPerUnit);
            Assert.Equal((short)10, insertedProducts[1].ReorderLevel);
            Assert.Equal(2.5m, insertedProducts[1].UnitPrice);
            Assert.Equal((short)100, insertedProducts[1].UnitsInStock);
            Assert.Equal((short)0, insertedProducts[1].UnitsOnOrder);

            ctx.Products.RemoveRange(insertedProducts);
            ctx.SaveChanges();
        }