コード例 #1
0
ファイル: SingletonTest.cs プロジェクト: tvdburgt/WebApi
        public async Task SingletonCRUD(string model, string singletonName)
        {
            string requestUri = string.Format(this.BaseAddress + "/{0}/{1}", model, singletonName);

            // Reset data source
            await ResetDataSource(model, singletonName);
            await ResetDataSource(model, "Partners");

            // GET singleton
            HttpResponseMessage response = this.Client.GetAsync(requestUri).Result;
            dynamic             result   = JObject.Parse(response.Content.ReadAsStringAsync().Result);

            // PUT singleton with {"ID":1,"Name":"singletonName","Revenue":2000,"Category":"IT"}
            result["Revenue"] = 2000;
            response          = await HttpClientExtensions.PutAsJsonAsync(this.Client, requestUri, result);

            response.EnsureSuccessStatusCode();

            // GET singleton/Revenue
            response = this.Client.GetAsync(requestUri + "/Revenue").Result;
            result   = JObject.Parse(response.Content.ReadAsStringAsync().Result);
            Assert.Equal(2000, (int)result["value"]);

            // PATCH singleton with {"@odata.type":"#WebStack.QA.Test.OData.Singleton.Company","Revenue":3000}
            HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PATCH"), requestUri);

            request.Content = new StringContent(string.Format(@"{{""@odata.type"":""#{0}"",""Revenue"":3000}}", typeof(Company)));
            request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
            response = Client.SendAsync(request).Result;
            response.EnsureSuccessStatusCode();

            // GET singleton
            response = this.Client.GetAsync(requestUri).Result;
            result   = JObject.Parse(response.Content.ReadAsStringAsync().Result);
            Assert.Equal(3000, (int)result["Revenue"]);

            // Negative: Add singleton
            // POST singleton
            var company = new Company();
            var content = new ObjectContent(company.GetType(), company, new JsonMediaTypeFormatter());

            response = this.Client.PostAsync(requestUri, content).Result;
            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);

            // Negative: Delete singleton
            // DELETE singleton
            response = this.Client.DeleteAsync(requestUri).Result;
            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
        }
コード例 #2
0
ファイル: SingletonTest.cs プロジェクト: tvdburgt/WebApi
        public async Task EntitySetNavigationLinkCRUD(string model, string format)
        {
            string requestUri    = string.Format(this.BaseAddress + "/{0}/Partners(1)/Company", model);
            string navigationUri = requestUri + "/$ref";
            string formatQuery   = string.Format("?$format={0}", format);

            //Reset data source
            await ResetDataSource(model, "MonstersInc");
            await ResetDataSource(model, "Partners");

            // PUT Partners(1)/Company/$ref
            string             idLinkBase = string.Format(this.BaseAddress + "/{0}/MonstersInc", model);
            HttpRequestMessage request    = new HttpRequestMessage(HttpMethod.Put, navigationUri);

            request.Content = new StringContent("{ \"@odata.id\" : \"" + idLinkBase + "\"}");
            request.Content.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/json");
            var response = Client.SendAsync(request).Result;

            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);

            // GET Partners(1)/Company
            response = this.Client.GetAsync(requestUri).Result;
            var result = JObject.Parse(response.Content.ReadAsStringAsync().Result);

            Assert.Equal("MonstersInc", (string)result["Name"]);

            // PUT Partners(1)/Company
            result["Revenue"] = 2000;
            response          = await HttpClientExtensions.PutAsJsonAsync(this.Client, requestUri, result);

            response.EnsureSuccessStatusCode();

            // GET Partners(1)/Company/Revenue
            response = this.Client.GetAsync(requestUri + formatQuery).Result;
            result   = JObject.Parse(response.Content.ReadAsStringAsync().Result);
            Assert.Equal(2000, (int)result["Revenue"]);

            // PATCH Partners(1)/Company
            request         = new HttpRequestMessage(new HttpMethod("PATCH"), requestUri);
            request.Content = new StringContent(string.Format(@"{{""@odata.type"":""#{0}"",""Revenue"":3000}}", typeof(Company)));
            request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
            response = Client.SendAsync(request).Result;
            response.EnsureSuccessStatusCode();

            // GET Partners(1)/Company/Revenue
            response = this.Client.GetAsync(requestUri + formatQuery).Result;
            result   = JObject.Parse(response.Content.ReadAsStringAsync().Result);
            Assert.Equal(3000, (int)result["Revenue"]);

            // DELETE Partners(1)/Company/$ref
            response = Client.DeleteAsync(navigationUri).Result;
            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);

            // GET Partners(1)/Company
            response = this.Client.GetAsync(requestUri + formatQuery).Result;
            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);

            // Negative: POST Partners(1)/Company
            var company = new Company();
            var content = new ObjectContent(company.GetType(), company, new JsonMediaTypeFormatter());

            response = this.Client.PostAsync(requestUri, content).Result;
            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
        }