public void TextMediaTypeFormatter_CanReadTextStream(string encoding) { var mediaTypeFormatter = new TextMediaTypeFormatter(); var expected = "this is my text £!"; HttpContent content = new StringContent(expected, Encoding.GetEncoding(encoding)); var formatterLogger = new Mock<IFormatterLogger>(); var result = mediaTypeFormatter.ReadFromStreamAsync(typeof(string), content.ReadAsStreamAsync().Result, content, formatterLogger.Object); Assert.Equal(result.Result.ToString(), expected); }
public async Task Can_Read_the_spec_sample() { // Arrange var sample = TestHelper.SPEC_SAMPLE_JSON; var sampleContent = new StringContent(sample); // Act var doc = await Formatter.ReadFromStreamAsync(typeof(ISirenEntity), await sampleContent.ReadAsStreamAsync(), sampleContent, null); // Assert var sirenDoc = Assert.IsAssignableFrom<ISirenEntity>(doc); TestHelper.AssertIsSpecSample(sirenDoc); }
public void PrimitiveTypesDeserializeAsOData(Type valueType, object value, MediaTypeHeaderValue mediaType, string resourceName) { string entity = Resources.GetString(resourceName); Assert.NotNull(entity); object expectedValue = value; ODataConventionModelBuilder modelBuilder = new ODataConventionModelBuilder(); modelBuilder.EntitySet<WorkItem>("WorkItems"); IEdmModel model = modelBuilder.GetEdmModel(); object actualValue; using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/WorkItems(10)/ID")) { HttpConfiguration config = new HttpConfiguration(); config.Routes.MapODataServiceRoute("default", "", model); request.SetConfiguration(config); request.ODataProperties().RouteName = "default"; request.ODataProperties().Model = model; ODataMediaTypeFormatter formatter = CreateFormatter(request); formatter.SupportedMediaTypes.Add(mediaType); using (StringContent content = new StringContent(entity)) { content.Headers.ContentType = mediaType; using (Stream stream = content.ReadAsStreamAsync().Result) { actualValue = formatter.ReadFromStreamAsync(valueType, stream, content, new Mock<IFormatterLogger>().Object).Result; } } } Assert.Equal(expectedValue, actualValue); }
public void Posting_NonDerivedType_To_Action_Expecting_BaseType_Throws() { StringContent content = new StringContent("{ '__metadata' : { 'type' : 'System.Web.Http.OData.Builder.TestModels.Motorcycle' } }"); content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose"); IODataRequestMessage oDataRequest = new ODataMessageWrapper(content.ReadAsStreamAsync().Result, content.Headers); ODataMessageReader reader = new ODataMessageReader(oDataRequest, new ODataMessageReaderSettings(), _model); ODataDeserializerProvider deserializerProvider = new DefaultODataDeserializerProvider(); ODataDeserializerContext context = new ODataDeserializerContext { Model = _model }; context.Path = new ODataPath(new ActionPathSegment(_model.EntityContainers().Single().FunctionImports().Single(f => f.Name == "PostMotorcycle_When_Expecting_Car"))); Assert.Throws<ODataException>( () => new ODataEntityDeserializer(deserializerProvider).Read(reader, typeof(Car), context), "An entry with type 'System.Web.Http.OData.Builder.TestModels.Motorcycle' was found, " + "but it is not assignable to the expected type 'System.Web.Http.OData.Builder.TestModels.Car'. " + "The type specified in the entry must be equal to either the expected type or a derived type."); }
private static IODataRequestMessage CreateJsonLightRequest(string body) { HttpContent content = new StringContent(body); HttpContentHeaders headers = content.Headers; headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata.metadata=full"); return new ODataMessageWrapper(content.ReadAsStreamAsync().Result, headers); }