public void OpenComplexType_SimpleDeserialization()
        {
            // Arrange
            const string Payload = "{" +
              "\"@odata.context\":\"http://localhost/odata/$metadata#OpenCustomers/$entity\"," +
              "\"CustomerId\":6,\"Name\":\"FirstName 6\"," +
              "\"Address\":{" +
                "\"Street\":\"Street 6\",\"City\":\"City 6\",\"Country\":\"Earth\",\"[email protected]\":\"#Guid\"," +
                "\"Token\":\"4DB52263-4382-4BCB-A63E-3129C1B5FA0D\"," +
                "\"Number\":990" +
              "}" +
            "}";

            const string RequestUri = "http://localhost/odata/OpenCustomers";

            HttpConfiguration configuration = new[] { typeof(OpenCustomersController) }.GetHttpConfiguration();
            configuration.MapODataServiceRoute("odata", "odata", GetEdmModel());
            HttpClient client = new HttpClient(new HttpServer(configuration));

            // Act
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, RequestUri);
            request.Content = new StringContent(Payload);
            request.Content.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/json");
            HttpResponseMessage response = client.SendAsync(request).Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            JObject result = JObject.Parse(response.Content.ReadAsStringAsync().Result);
            Assert.Equal("http://localhost/odata/$metadata#OpenCustomers/$entity", result["@odata.context"]);
            Assert.Equal("Earth", result["Address"]["Country"]);
            Assert.Equal(990, result["Address"]["Number"]);
            Assert.Equal("4DB52263-4382-4BCB-A63E-3129C1B5FA0D".ToLower(), result["Address"]["Token"]);
        }
        public async Task AttributeRouting_QueryProperty_AfterCallBoundFunction()
        {
            // Arrange
            const string RequestUri = @"http://localhost/Customers(12)/NS.GetOrder(orderId=4)/Amount";
            CustomersModelWithInheritance model = new CustomersModelWithInheritance();

            HttpConfiguration config = new[] { typeof(CustomersController) }.GetHttpConfiguration();
            config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
            config.MapODataServiceRoute("odata", "", model.Model);

            HttpServer server = new HttpServer(config);
            config.EnsureInitialized();

            HttpClient client = new HttpClient(server);
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, RequestUri);

            // Act
            HttpResponseMessage response = await client.SendAsync(request);
            string responseString = await response.Content.ReadAsStringAsync();

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            Assert.Equal("{\r\n  \"@odata.context\":\"http://localhost/$metadata#Edm.Int32\",\"value\":56\r\n}",
                responseString);
        }
        public void Post_OpenEntityType()
        {
            // Arrange
            const string Payload = "{" +
              "\"@odata.context\":\"http://localhost/odata/$metadata#OpenCustomers/$entity\"," +
              "\"CustomerId\":6,\"Name\":\"FirstName 6\"," +
              "\"Address\":{" +
                "\"Street\":\"Street 6\",\"City\":\"City 6\",\"Country\":\"Earth\",\"[email protected]\":\"#Guid\"," +
                "\"Token\":\"4DB52263-4382-4BCB-A63E-3129C1B5FA0D\"," +
                "\"Number\":990" +
              "}," +
              "\"Website\": \"WebSite #6\",\"Country\":\"My Dynamic Country\",\"[email protected]\":\"#Guid\",\"Token\":\"2c1f450a-a2a7-4fe1-a25d-4d9332fc0694\"," +
              "\"[email protected]\":\"#Collection(Double)\"," +
              "\"DoubleList\":[5.5, 4.4, 3.3]" +
            "}";

            const string RequestUri = "http://localhost/odata/SimpleOpenCustomers";

            var configuration = new[] { typeof(SimpleOpenCustomersController) }.GetHttpConfiguration();
            configuration.MapODataServiceRoute("odata", "odata", GetEdmModel());

            HttpClient client = new HttpClient(new HttpServer(configuration));

            // Act
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, RequestUri);
            request.Content = new StringContent(Payload);
            request.Content.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/json");
            HttpResponseMessage response = client.SendAsync(request).Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
        }
        public void OpenComplexType_SimpleSerialization(bool enableNullDynamicProperty)
        {
            // Arrange
            const string RequestUri = "http://localhost/odata/OpenCustomers(2)/Address";
            var configuration = new[] { typeof(OpenCustomersController) }.GetHttpConfiguration();
            configuration.SetSerializeNullDynamicProperty(enableNullDynamicProperty);
            configuration.MapODataServiceRoute("odata", "odata", GetEdmModel());

            HttpClient client = new HttpClient(new HttpServer(configuration));

            // Act
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, RequestUri);
            request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=full"));
            HttpResponseMessage response = client.SendAsync(request).Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            JObject result = JObject.Parse(response.Content.ReadAsStringAsync().Result);
            Assert.Equal("http://localhost/odata/$metadata#OpenCustomers(2)/Address", result["@odata.context"]);
            Assert.Equal("Street 2", result["Street"]);
            Assert.Equal("City 2", result["City"]);
            Assert.Equal("300", result["IntProp"]);
            Assert.Equal("My Dynamic Country", result["Country"]);
            Assert.Equal("2c1f450a-a2a7-4fe1-a25d-4d9332fc0694", result["Token"]);
            Assert.Equal("2015-03-02", result["Birthday"]);
            if (enableNullDynamicProperty)
            {
                Assert.NotNull(result["Region"]);
                Assert.Equal(JValue.CreateNull(), result["Region"]);
            }
            else
            {
                Assert.Null(result["Region"]);
            }
        }
Example #5
0
        public void Get_OpenEntityType()
        {
            // Arrange
            const string RequestUri = "http://localhost/odata/SimpleOpenCustomers(9)";
            var configuration = new[] { typeof(SimpleOpenCustomersController) }.GetHttpConfiguration();
            configuration.MapODataServiceRoute("odata", "odata", GetEdmModel());

            HttpClient client = new HttpClient(new HttpServer(configuration));

            // Act
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, RequestUri);
            request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=full"));
            HttpResponseMessage response = client.SendAsync(request).Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            JObject result = JObject.Parse(response.Content.ReadAsStringAsync().Result);
            Assert.Equal("http://localhost/odata/$metadata#SimpleOpenCustomers/$entity", result["@odata.context"]);
            Assert.Equal("#System.Web.OData.SimpleVipCustomer", result["@odata.type"]);
            Assert.Equal(9, result["CustomerId"]);
            Assert.Equal("VipCustomer", result["Name"]);
            Assert.Equal("#Collection(Int32)", result["*****@*****.**"]);
            Assert.Equal(new JArray(new[] { 200, 100, 300, 0, 400 }), result["ListProp"]);
            Assert.Equal("0001-01-01", result["DateList"][0]);
            Assert.Equal("9999-12-31", result["DateList"][1]);
        }
Example #6
0
 public ODataCountTest()
 {
     IEdmModel model = GetEdmModel();
     HttpConfiguration configuration = new[] { typeof(DollarCountEntitiesController) }.GetHttpConfiguration();
     configuration.MapODataServiceRoute("odata", "odata", model);
     var server = new HttpServer(configuration);
     _client = new HttpClient(server);
 }
        public ODataFunctionTests()
        {
            DefaultODataPathHandler pathHandler = new DefaultODataPathHandler();
            HttpConfiguration configuration =
                new[] { typeof(MetadataController), typeof(FCustomersController) }.GetHttpConfiguration();
            var model = GetUnTypedEdmModel();

            // without attribute routing
            configuration.MapODataServiceRoute("odata1", "odata", model, pathHandler, ODataRoutingConventions.CreateDefault());

            // only with attribute routing
            IList<IODataRoutingConvention> routingConventions = new List<IODataRoutingConvention>
            {
                new AttributeRoutingConvention(model, configuration)
            };
            configuration.MapODataServiceRoute("odata2", "attribute", model, pathHandler, routingConventions);

            _client = new HttpClient(new HttpServer(configuration));
        }
        public void AGet_Minimial()
        {
            // Arrange
            var configuration = new[] { typeof(AccountsController) }.GetHttpConfiguration();
            configuration.MapODataServiceRoute("odata", "odata", GetEdmModel());

            HttpClient client = new HttpClient(new HttpServer(configuration));

            // Act
            HttpResponseMessage response = client.GetAsync(_requestRooturl + "Accounts?$expand=PayoutPI&$format=application/json;odata.metadata=minimal").Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            Console.WriteLine(response.Content.ReadAsStringAsync().Result);
        }
        public void Get_OpenEntityTypeWithOrderbyAndFilter(string uri, int[] customerIds)
        {
            // Arrange
            var configuration = new[] { typeof(SimpleOpenCustomersController) }.GetHttpConfiguration();
            configuration.MapODataServiceRoute("odata", "odata", GetEdmModel());

            HttpClient client = new HttpClient(new HttpServer(configuration));

            // Act
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri);
            request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=full"));
            HttpResponseMessage response = client.SendAsync(request).Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            JObject result = JObject.Parse(response.Content.ReadAsStringAsync().Result);
            var resultArray = result["value"] as JArray;
            Assert.Equal(2, resultArray.Count);
            for (var i = 0; i < customerIds.Length; i++)
                Assert.Equal(customerIds[i], resultArray[i]["CustomerId"]);
        }
Example #10
0
        public void Get_OpenEntityType_Enum_Collection_Property()
        {
            // Arrange
            const string RequestUri = "http://localhost/odata/UntypedSimpleOpenCustomers(1)";
            var configuration = new[] { typeof(UntypedSimpleOpenCustomersController) }.GetHttpConfiguration();
            configuration.MapODataServiceRoute("odata", "odata", GetUntypedEdmModel());

            HttpClient client = new HttpClient(new HttpServer(configuration));

            // Act
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, RequestUri);
            request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=full"));
            HttpResponseMessage response = client.SendAsync(request).Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            JObject result = JObject.Parse(response.Content.ReadAsStringAsync().Result);
            Assert.Equal("http://localhost/odata/$metadata#UntypedSimpleOpenCustomers/$entity", result["@odata.context"]);
            Assert.Equal("#Collection(NS.Color)", result["*****@*****.**"]);
            Assert.Equal(new JArray(new[] { "Red", "0" , "Red"}), result["Colors"]);
            Assert.Equal("Red", result["Color"]);
        }
 private static HttpConfiguration GetQueryOptionConfiguration(bool caseInsensitive)
 {
     HttpConfiguration config = new[] { typeof(ParserExtenstionCustomersController) }.GetHttpConfiguration();
     config.EnableCaseInsensitive(caseInsensitive);
     config.MapODataServiceRoute("query", "query", GetEdmModel());
     return config;
 }
        public void Queryable_UsesRouteModel_ForMultipleModels()
        {
            // Model 1 only has Name, Model 2 only has Age
            ODataModelBuilder builder1 = ODataModelBuilderMocks.GetModelBuilderMock<ODataModelBuilder>();
            var personType1 = builder1.EntityType<FormatterPerson>().Property(p => p.Name);
            builder1.EntitySet<FormatterPerson>("People").HasIdLink(p => new Uri("http://link/"), false);
            var model1 = builder1.GetEdmModel();

            ODataModelBuilder builder2 = ODataModelBuilderMocks.GetModelBuilderMock<ODataModelBuilder>();
            builder2.EntityType<FormatterPerson>().Property(p => p.Age);
            builder2.EntitySet<FormatterPerson>("People").HasIdLink(p => new Uri("http://link/"), false);
            var model2 = builder2.GetEdmModel();

            var config = new[] { typeof(PeopleController) }.GetHttpConfiguration();
            config.MapODataServiceRoute("OData1", "v1", model1);
            config.MapODataServiceRoute("OData2", "v2", model2);

            using (HttpServer host = new HttpServer(config))
            using (HttpClient client = new HttpClient(host))
            {
                // Model 1 has the Name property but not the Age property
                AssertRespondsWithExpectedStatusCode(client, "http://localhost/v1/People?$orderby=Name", HttpStatusCode.OK);
                AssertRespondsWithExpectedStatusCode(client, "http://localhost/v1/People?$orderby=Age", HttpStatusCode.BadRequest);

                // Model 2 has the Age property but not the Name property
                AssertRespondsWithExpectedStatusCode(client, "http://localhost/v2/People?$orderby=Name", HttpStatusCode.BadRequest);
                AssertRespondsWithExpectedStatusCode(client, "http://localhost/v2/People?$orderby=Age", HttpStatusCode.OK);
            }
        }
        public void DollarMetadata_Works_WithDerivedOpenComplexType()
        {
            // Arrange
            const string expectMetadata =
@"<?xml version='1.0' encoding='utf-8'?>
<edmx:Edmx Version='4.0' xmlns:edmx='http://docs.oasis-open.org/odata/ns/edmx'>
  <edmx:DataServices>
    <Schema Namespace='System.Web.OData.Formatter' xmlns='http://docs.oasis-open.org/odata/ns/edm'>
      <ComplexType Name='ComplexBaseType'>
        <Property Name='BaseProperty' Type='Edm.String' />
      </ComplexType>
      <ComplexType Name='ComplexDerivedOpenType' BaseType='System.Web.OData.Formatter.ComplexBaseType' OpenType='true'>
        <Property Name='DerivedProperty' Type='Edm.String' />
      </ComplexType>
    </Schema>
    <Schema Namespace='Default' xmlns='http://docs.oasis-open.org/odata/ns/edm'>
      <EntityContainer Name='Container' />
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>";
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.ComplexType<ComplexBaseType>();
            IEdmModel model = builder.GetEdmModel();

            var config = new[] { typeof(MetadataController) }.GetHttpConfiguration();
            config.MapODataServiceRoute(model);
            HttpServer server = new HttpServer(config);
            HttpClient client = new HttpClient(server);

            // Act
            var response = client.GetAsync("http://localhost/$metadata").Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            Assert.Equal("application/xml", response.Content.Headers.ContentType.MediaType);
            Assert.Equal(expectMetadata.Replace("'", "\""), response.Content.ReadAsStringAsync().Result);
        }
        public void DollarMetadata_Works_WithEntityTypeWithEnumKeys()
        {
            // Arrange
            const string expectMetadata =
                "      <EntityType Name=\"EnumModel\">\r\n" +
                "        <Key>\r\n" +
                "          <PropertyRef Name=\"Simple\" />\r\n" +
                "        </Key>\r\n" +
                "        <Property Name=\"Simple\" Type=\"NS.SimpleEnum\" Nullable=\"false\" />\r\n" +
                "      </EntityType>\r\n" +
                "      <EnumType Name=\"SimpleEnum\" />";

            ODataModelBuilder builder = new ODataModelBuilder();
            builder.EntityType<EnumModel>().HasKey(e => e.Simple).Namespace = "NS";
            builder.EnumType<SimpleEnum>().Namespace = "NS";
            IEdmModel model = builder.GetEdmModel();

            var config = new[] { typeof(MetadataController) }.GetHttpConfiguration();
            config.MapODataServiceRoute(model);
            HttpServer server = new HttpServer(config);
            HttpClient client = new HttpClient(server);

            // Act
            var response = client.GetAsync("http://localhost/$metadata").Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            Assert.Equal("application/xml", response.Content.Headers.ContentType.MediaType);

            string payload = response.Content.ReadAsStringAsync().Result;
            Assert.Contains(expectMetadata, payload);
        }
        private static HttpConfiguration GetConfiguration(bool caseInsensitive, bool unqualifiedNameCall)
        {
            IEdmModel model = ODataRoutingModel.GetModel();
            HttpConfiguration config = new[]
            {
                typeof(MetadataController),
                typeof(ProductsController),
                typeof(RoutingCustomersController),
            }.GetHttpConfiguration();

            config.EnableCaseInsensitive(caseInsensitive);
            config.EnableUnqualifiedNameCall(unqualifiedNameCall);

            config.MapODataServiceRoute("odata", "odata", model);
            return config;
        }
        public void DefaultResolver_DoesnotWorks_UnqualifiedNameTemplate()
        {
            // Arrange
            IEdmModel model = GetEdmModel();
            HttpConfiguration config = new[] { typeof(ParserExtenstionCustomers2Controller) }.GetHttpConfiguration();
            config.EnableUnqualifiedNameCall(false);
            config.MapODataServiceRoute("odata", "odata", model);
            HttpClient client = new HttpClient(new HttpServer(config));

            // Act
            HttpRequestMessage request = new HttpRequestMessage(
                HttpMethod.Get,
                "http://localhost/odata/ParserExtenstionCustomers2");

            // Assert
            Assert.Throws<InvalidOperationException>(() => client.SendAsync(request).Result);
        }
        public void OpenComplexType_DeleteComplexTypeProperty()
        {
            // Arrange
            const string requestUri = "http://localhost/odata/OpenCustomers(1)/Address";

            HttpConfiguration configuration = new[] { typeof(OpenCustomersController) }.GetHttpConfiguration();
            configuration.MapODataServiceRoute("odata", "odata", GetEdmModel());
            HttpClient client = new HttpClient(new HttpServer(configuration));

            // Act
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Delete, requestUri);
            HttpResponseMessage response = client.SendAsync(request).Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
        }
 private HttpConfiguration GetConfiguration()
 {
     var config = new[] { typeof(MetadataController) }.GetHttpConfiguration();
     config.MapODataServiceRoute(ODataTestUtil.GetEdmModel());
     return config;
 }
 private static HttpClient GetClient()
 {
     HttpConfiguration config =
         new[] { typeof(MetadataController), typeof(DateAndTimeOfDayModelsController) }.GetHttpConfiguration();
     config.MapODataServiceRoute("odata", "odata", GetEdmModel());
     return new HttpClient(new HttpServer(config));
 }
        public void RequiredAttribute_Works_OnComplexTypeProperty()
        {
            // Arrange
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<FormatterAccount>("Accounts");

            var config = new[] { typeof(MetadataController) }.GetHttpConfiguration();
            config.MapODataServiceRoute(builder.GetEdmModel());

            HttpServer server = new HttpServer(config);
            HttpClient client = new HttpClient(server);

            // Act
            var responseString = client.GetStringAsync("http://localhost/$metadata").Result;

            // Assert
            Assert.Contains(
                "<Property Name=\"Address\" Type=\"System.Web.OData.Formatter.FormatterAddress\" Nullable=\"false\" />",
                responseString);

            Assert.Contains(
                "<Property Name=\"Addresses\" Type=\"Collection(System.Web.OData.Formatter.FormatterAddress)\" Nullable=\"false\" />",
                responseString);
        }
        public void Controller_DoesNotAppear_InApiDescriptions()
        {
            var config = new[] { typeof(MetadataController) }.GetHttpConfiguration();
            config.Routes.MapHttpRoute("Default", "{controller}/{action}");
            config.MapODataServiceRoute(new ODataConventionModelBuilder().GetEdmModel());
            config.EnsureInitialized();
            var explorer = config.Services.GetApiExplorer();

            var apis = explorer.ApiDescriptions.Select(api => api.ActionDescriptor.ControllerDescriptor.ControllerName);

            Assert.DoesNotContain("ODataMetadata", apis);
        }
        public void DollarMetadata_Works_WithMultipleModels()
        {
            ODataConventionModelBuilder builder1 = new ODataConventionModelBuilder();
            builder1.EntitySet<FormatterPerson>("People1");
            var model1 = builder1.GetEdmModel();

            ODataConventionModelBuilder builder2 = new ODataConventionModelBuilder();
            builder2.EntitySet<FormatterPerson>("People2");
            var model2 = builder2.GetEdmModel();

            var config = new[] { typeof(MetadataController) }.GetHttpConfiguration();
            HttpServer server = new HttpServer(config);
            config.MapODataServiceRoute("OData1", "v1", model1);
            config.MapODataServiceRoute("OData2", "v2", model2);

            HttpClient client = new HttpClient(server);
            AssertHasEntitySet(client, "http://localhost/v1/$metadata", "People1");
            AssertHasEntitySet(client, "http://localhost/v2/$metadata", "People2");
        }
        public void ExtensionResolver_Works_EnumPrefixFree(string parameter, bool enableEnumPrefix, HttpStatusCode statusCode)
        {
            // Arrange
            IEdmModel model = GetEdmModel();
            HttpConfiguration config = new[] { typeof(ParserExtenstionCustomersController) }.GetHttpConfiguration();
            config.EnableEnumPrefixFree(enableEnumPrefix);
            config.MapODataServiceRoute("odata", "odata", model);
            HttpClient client = new HttpClient(new HttpServer(config));

            // Act
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get,
                String.Format("http://localhost/odata/ParserExtenstionCustomers/Default.GetCustomerByGender({0})", parameter));
            HttpResponseMessage response = client.SendAsync(request).Result;

            // Assert
            Assert.Equal(statusCode, response.StatusCode);

            if (statusCode == HttpStatusCode.OK)
            {
                Assert.Equal("GetCustomerByGender/Male", (response.Content as ObjectContent<string>).Value);
            }
        }
        public void DollarMetadata_Works_WithAbstractEntityTypeWithoutKey()
        {
            // Arrange
            const string expectMetadata =
"      <EntityType Name=\"AbstractEntityType\" Abstract=\"true\">\r\n" +
"        <Property Name=\"IntProperty\" Type=\"Edm.Int32\" Nullable=\"false\" />\r\n" +
"      </EntityType>";

            ODataModelBuilder builder = new ODataModelBuilder();
            builder.EntityType<AbstractEntityType>().Abstract().Property(a => a.IntProperty);
            IEdmModel model = builder.GetEdmModel();

            var config = new[] { typeof(MetadataController) }.GetHttpConfiguration();
            config.MapODataServiceRoute(model);
            HttpServer server = new HttpServer(config);
            HttpClient client = new HttpClient(server);

            // Act
            var response = client.GetAsync("http://localhost/$metadata").Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            Assert.Equal("application/xml", response.Content.Headers.ContentType.MediaType);

            string payload = response.Content.ReadAsStringAsync().Result;
            Assert.Contains(expectMetadata, payload);
            Assert.DoesNotContain("<key>", payload);
        }
        public void ExtensionResolver_Works_EnumPrefixFree_QueryOption(string query, bool enableEnumPrefix, HttpStatusCode statusCode, string output)
        {
            // Arrange
            IEdmModel model = GetEdmModel();
            HttpConfiguration config = new[] { typeof(ParserExtenstionCustomersController) }.GetHttpConfiguration();
            config.EnableEnumPrefixFree(enableEnumPrefix);
            config.MapODataServiceRoute("odata", "odata", model);
            HttpClient client = new HttpClient(new HttpServer(config));

            // Act
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get,
                String.Format("http://localhost/odata/ParserExtenstionCustomers?{0}", query));
            HttpResponseMessage response = client.SendAsync(request).Result;

            // Assert
            Assert.Equal(statusCode, response.StatusCode);

            if (statusCode == HttpStatusCode.OK)
            {
                JObject content = response.Content.ReadAsAsync<JObject>().Result;
                Assert.Equal(output, String.Join(",", content["value"].Select(e => e["Id"])));
            }
        }
        public void OpenComplexType_PutComplexTypeProperty()
        {
            // Arrange
            const string payload = "{\"value\":{" +
              "\"Street\":\"UpdatedStreet\"," +
              "\"City\":\"UpdatedCity\"," +
              "\"[email protected]\":\"#Date\"," +
              "\"Publish\":\"2016-02-02\"" +
            "}}";

            const string requestUri = "http://localhost/odata/OpenCustomers(1)/Address";

            HttpConfiguration configuration = new[] { typeof(OpenCustomersController) }.GetHttpConfiguration();
            configuration.MapODataServiceRoute("odata", "odata", GetEdmModel());
            HttpClient client = new HttpClient(new HttpServer(configuration));

            // Act
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, requestUri);
            request.Content = new StringContent(payload);
            request.Content.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/json");
            HttpResponseMessage response = client.SendAsync(request).Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
        }
        public void ExtensionResolver_Works_UnqualifiedNameTemplate()
        {
            // Arrange
            IEdmModel model = GetEdmModel();
            HttpConfiguration config = new[] { typeof(ParserExtenstionCustomers2Controller) }.GetHttpConfiguration();
            config.EnableUnqualifiedNameCall(true);
            config.MapODataServiceRoute("odata", "odata", model);
            HttpClient client = new HttpClient(new HttpServer(config));

            // Act
            HttpRequestMessage request = new HttpRequestMessage(
                HttpMethod.Get,
                "http://localhost/odata/ParserExtenstionCustomers2/GetCustomerTitleById(id=32)");
            HttpResponseMessage response = client.SendAsync(request).Result;

            // Assert
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal("GetCustomerTitleById/32", (response.Content as ObjectContent<string>).Value);
        }
        public void DollarMetadata_Works_WithFunctionParameterNullable_ReturnTypeNullable()
        {
            // Arrange
            const string expectMetadata =
@"<Schema Namespace='Default' xmlns='http://docs.oasis-open.org/odata/ns/edm'>
      <Function Name='NullableFunction' IsBound='true'>
        <Parameter Name='bindingParameter' Type='System.Web.OData.Formatter.FormatterPerson' />
        <Parameter Name='param' Type='Edm.String' Unicode='false' />
        <ReturnType Type='System.Web.OData.Formatter.FormatterAddress' />
      </Function>
      <Function Name='NonNullableFunction' IsBound='true'>
        <Parameter Name='bindingParameter' Type='System.Web.OData.Formatter.FormatterPerson' />
        <Parameter Name='param' Type='Edm.String' Nullable='false' Unicode='false' />
        <ReturnType Type='System.Web.OData.Formatter.FormatterAddress' Nullable='false' />
      </Function>
      <EntityContainer Name='Container' />
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>";

            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            EntityTypeConfiguration<FormatterPerson> person = builder.EntityType<FormatterPerson>();

            FunctionConfiguration function = person.Function("NullableFunction").Returns<FormatterAddress>();
            function.Parameter<string>("param");

            function = person.Function("NonNullableFunction").Returns<FormatterAddress>();
            function.OptionalReturn = false;
            function.Parameter<string>("param").OptionalParameter = false;
            IEdmModel model = builder.GetEdmModel();

            var config = new[] { typeof(MetadataController) }.GetHttpConfiguration();
            config.MapODataServiceRoute(model);
            HttpServer server = new HttpServer(config);
            HttpClient client = new HttpClient(server);

            // Act
            var response = client.GetAsync("http://localhost/$metadata").Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            Assert.Equal("application/xml", response.Content.Headers.ContentType.MediaType);
            Assert.Contains(expectMetadata.Replace("'", "\""), response.Content.ReadAsStringAsync().Result);
        }
Example #29
0
        public void CanDispatch_ActionPayloadWithEntity_ToBoundAction_UnTyped()
        {
            // Arrange
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/UntypedCustomers/NS.MyAction");
            request.Headers.Add("accept", "application/json");

            HttpConfiguration configuration = new[] { typeof(UntypedCustomersController) }.GetHttpConfiguration();
            configuration.MapODataServiceRoute(GetUntypeModel());
            HttpClient client = new HttpClient(new HttpServer(configuration));

            request.Content = new StringContent(EntityPayload);
            request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

            // Act
            HttpResponseMessage response = client.SendAsync(request).Result;
            string responseString = response.Content.ReadAsStringAsync().Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            Assert.Contains("\"@odata.context\":\"http://localhost/$metadata#Edm.Boolean\",\"value\":true", responseString);
        }
        public void DollarMetadata_Works_WithDerivedEntityTypeWithOwnKeys()
        {
            // Arrange
            const string expectMetadata =
"    <Schema Namespace=\"System.Web.OData.Formatter\" xmlns=\"http://docs.oasis-open.org/odata/ns/edm\">\r\n" +
"      <EntityType Name=\"AbstractEntityType\" Abstract=\"true\">\r\n" +
"        <Property Name=\"IntProperty\" Type=\"Edm.Int32\" Nullable=\"false\" />\r\n" +
"      </EntityType>\r\n" +
"      <EntityType Name=\"SubEntityType\" BaseType=\"System.Web.OData.Formatter.AbstractEntityType\">\r\n" +
"        <Key>\r\n" +
"          <PropertyRef Name=\"SubKey\" />\r\n" +
"        </Key>\r\n" +
"        <Property Name=\"SubKey\" Type=\"Edm.Int32\" Nullable=\"false\" />\r\n" +
"      </EntityType>\r\n" +
"      <EntityType Name=\"AnotherSubEntityType\" BaseType=\"System.Web.OData.Formatter.AbstractEntityType\">\r\n" +
"        <Key>\r\n" +
"          <PropertyRef Name=\"AnotherKey\" />\r\n" +
"        </Key>\r\n" +
"        <Property Name=\"AnotherKey\" Type=\"Edm.Double\" Nullable=\"false\" />\r\n" +
"      </EntityType>\r\n" +
"    </Schema>";

            ODataModelBuilder builder = new ODataModelBuilder();
            builder.EntityType<AbstractEntityType>().Abstract().Property(a => a.IntProperty);
            builder.EntityType<SubEntityType>().HasKey(b => b.SubKey).DerivesFrom<AbstractEntityType>();
            builder.EntityType<AnotherSubEntityType>().HasKey(d => d.AnotherKey).DerivesFrom<AbstractEntityType>();
            IEdmModel model = builder.GetEdmModel();

            var config = new[] { typeof(MetadataController) }.GetHttpConfiguration();
            config.MapODataServiceRoute(model);
            HttpServer server = new HttpServer(config);
            HttpClient client = new HttpClient(server);

            // Act
            var response = client.GetAsync("http://localhost/$metadata").Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            Assert.Equal("application/xml", response.Content.Headers.ContentType.MediaType);

            string payload = response.Content.ReadAsStringAsync().Result;
            Assert.Contains(expectMetadata, payload);
        }