// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; options.ReturnHttpNotAcceptable = true; options.InputFormatters.Add(new XmlSerializerInputFormatter()); var xmlOutputFormatter = new XmlSerializerOutputFormatter(); xmlOutputFormatter.SupportedMediaTypes.Add("application/xml-hateoas"); options.OutputFormatters.Add(xmlOutputFormatter); var jsonOutputFormatter = options.OutputFormatters.OfType <JsonOutputFormatter>().FirstOrDefault(); jsonOutputFormatter?.SupportedMediaTypes.Add("application/json-hateoas"); options.FormatterMappings.SetMediaTypeMappingForFormat( "xml-hateoas", MediaTypeHeaderValue.Parse("application/xml")); options.FormatterMappings.SetMediaTypeMappingForFormat( "json-hateoas", MediaTypeHeaderValue.Parse("application/json")); }) .AddJsonOptions(options => { // Force Camel Case to JSON options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); }) .AddXmlDataContractSerializerFormatters(); services.Configure <IISOptions>(option => { option.ForwardClientCertificate = false; }); services.AddCors(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddCors(options => { options.AddPolicy("AllowAll", builder => builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials() ); }); var xmlSerializer = new XmlSerializerOutputFormatter(); var mvcBuilder = services.AddMvc(opt => { opt.FormatterMappings.SetMediaTypeMappingForFormat("xml", new MediaTypeHeaderValue("application/xml")); }).AddXmlSerializerFormatters(); services.Configure <MvcOptions>(options => { options.Filters.Add(new CorsAuthorizationFilterFactory("AllowAll")); }); services.AddSingleton <IConfiguration>(Configuration); services.AddTransient <IIntervalDateQuoteRepository, IntervalDateQuoteRepository>(); services.AddTransient <ISectorPerformanceRepository, SectorPerformanceRepository>(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; var jsonOutputFormatter = options.OutputFormatters.OfType <JsonOutputFormatter>().FirstOrDefault(); jsonOutputFormatter?.SupportedMediaTypes.Clear(); jsonOutputFormatter?.SupportedMediaTypes.Add("application/vnd.noark5-v4+json"); options.InputFormatters.Add(new XmlSerializerInputFormatter(options)); var xmlOutputFormatter = new XmlSerializerOutputFormatter(); xmlOutputFormatter.SupportedMediaTypes.Clear(); xmlOutputFormatter.SupportedMediaTypes.Add("application/vnd.noark5-v4+xml"); options.OutputFormatters.Add(xmlOutputFormatter); }) .AddJsonOptions(options => { options.SerializerSettings.Formatting = Formatting.Indented; options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; }) .SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddOData(); }
public async Task XmlSerializerOutputFormatterWritesIndentedOutput() { // Arrange var sampleInput = new DummyClass { SampleInt = 10 }; var formatter = new XmlSerializerOutputFormatter( XmlOutputFormatter.GetDefaultXmlWriterSettings()); formatter.WriterSettings.Indent = true; var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); // Act await formatter.WriteAsync(outputFormatterContext); // Assert Assert.NotNull(outputFormatterContext.ActionContext.HttpContext.Response.Body); outputFormatterContext.ActionContext.HttpContext.Response.Body.Position = 0; var outputString = new StreamReader(outputFormatterContext.ActionContext.HttpContext.Response.Body, Encoding.UTF8).ReadToEnd(); Assert.Equal("<DummyClass xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n <SampleInt>10</SampleInt>\r\n</DummyClass>", outputString); }
public async Task SuppliedWriterSettings_TakeAffect() { // Arrange var writerSettings = FormattingUtilities.GetDefaultXmlWriterSettings(); writerSettings.OmitXmlDeclaration = false; var sampleInput = new DummyClass { SampleInt = 10 }; var formatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); var formatter = new XmlSerializerOutputFormatter(writerSettings); var expectedOutput = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<DummyClass xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SampleInt>10</SampleInt></DummyClass>"; // Act await formatter.WriteAsync(formatterContext); // Assert Assert.Same(writerSettings, formatter.WriterSettings); var responseStream = formatterContext.ActionContext.HttpContext.Response.Body; Assert.NotNull(responseStream); responseStream.Position = 0; var actualOutput = new StreamReader(responseStream, Encoding.UTF8).ReadToEnd(); Assert.Equal(expectedOutput, actualOutput); }
public async Task XmlSerializerOutputFormatterWritesIndentedOutput() { // Arrange var expectedOutput = "<DummyClass xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n <SampleInt>10</SampleInt>\r\n</DummyClass>"; var sampleInput = new DummyClass { SampleInt = 10 }; var formatter = new XmlSerializerOutputFormatter(); formatter.WriterSettings.Indent = true; var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public async Task XmlSerializerOutputFormatterWritesUTF16Output() { // Arrange var expectedOutput = "<?xml version=\"1.0\" encoding=\"utf-16\"?>" + "<DummyClass xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SampleInt>10</SampleInt></DummyClass>"; var sampleInput = new DummyClass { SampleInt = 10 }; var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType(), "application/xml; charset=utf-16"); var formatter = new XmlSerializerOutputFormatter(); formatter.WriterSettings.OmitXmlDeclaration = false; // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader( body, new UnicodeEncoding(bigEndian: false, byteOrderMark: false, throwOnInvalidBytes: true)).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public static MvcOptions ConfigureXmlFormatter([NotNull] this MvcOptions thisValue, [NotNull] Action <XmlWriterSettings> configureXml) { thisValue.RespectBrowserAcceptHeader = true; XmlSerializerInputFormatter input = thisValue.InputFormatters.OfType <XmlSerializerInputFormatter>() .FirstOrDefault(); if (input == null) { input = new XmlSerializerInputFormatter(thisValue); thisValue.InputFormatters.Add(input); } XmlSerializerOutputFormatter output = thisValue.OutputFormatters.OfType <XmlSerializerOutputFormatter>() .FirstOrDefault(); if (output == null) { output = new XmlSerializerOutputFormatter(); thisValue.OutputFormatters.Add(output); } configureXml(output.WriterSettings); thisValue.FormatterMappings.SetMediaTypeMappingForFormat("xml", MediaTypeHeaderValue.Parse("application/xml")); return(thisValue); }
public async Task XmlSerializerOutputFormatterWritesComplexTypes() { // Arrange var sampleInput = new TestLevelTwo { SampleString = "TestString", TestOne = new TestLevelOne { SampleInt = 10, sampleString = "TestLevelOne string" } }; var formatter = new XmlSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); // Act await formatter.WriteAsync(outputFormatterContext); // Assert Assert.NotNull(outputFormatterContext.ActionContext.HttpContext.Response.Body); outputFormatterContext.ActionContext.HttpContext.Response.Body.Position = 0; Assert.Equal("<TestLevelTwo xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SampleString>TestString</SampleString>" + "<TestOne><sampleString>TestLevelOne string</sampleString>" + "<SampleInt>10</SampleInt></TestOne></TestLevelTwo>", new StreamReader(outputFormatterContext.ActionContext.HttpContext.Response.Body, Encoding.UTF8) .ReadToEnd()); }
public async Task XmlSerializerOutputFormatterWritesOnModifiedWriterSettings() { // Arrange var sampleInput = new DummyClass { SampleInt = 10 }; var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); var formatter = new XmlSerializerOutputFormatter( new System.Xml.XmlWriterSettings { OmitXmlDeclaration = false, CloseOutput = false }); // Act await formatter.WriteAsync(outputFormatterContext); // Assert Assert.NotNull(outputFormatterContext.ActionContext.HttpContext.Response.Body); outputFormatterContext.ActionContext.HttpContext.Response.Body.Position = 0; Assert.Equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<DummyClass xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SampleInt>10</SampleInt></DummyClass>", new StreamReader(outputFormatterContext.ActionContext.HttpContext.Response.Body, Encoding.UTF8) .ReadToEnd()); }
public async Task XmlSerializerOutputFormatterWritesUTF16Output() { // Arrange var sampleInput = new DummyClass { SampleInt = 10 }; var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType(), "application/xml; charset=utf-16"); var formatter = new XmlSerializerOutputFormatter( XmlOutputFormatter.GetDefaultXmlWriterSettings()); formatter.WriterSettings.OmitXmlDeclaration = false; // Act await formatter.WriteAsync(outputFormatterContext); // Assert Assert.NotNull(outputFormatterContext.ActionContext.HttpContext.Response.Body); outputFormatterContext.ActionContext.HttpContext.Response.Body.Position = 0; Assert.Equal("<?xml version=\"1.0\" encoding=\"utf-16\"?>" + "<DummyClass xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SampleInt>10</SampleInt></DummyClass>", new StreamReader(outputFormatterContext.ActionContext.HttpContext.Response.Body, Encodings.UTF16EncodingLittleEndian).ReadToEnd()); }
// Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc(config => { config.RespectBrowserAcceptHeader = true; XmlSerializerOutputFormatter xml = new XmlSerializerOutputFormatter(); xml.WriterSettings.Indent = true; config.OutputFormatters.Add(xml); }) .AddJsonOptions(opt => { ApplyJsonOptions(opt); }) .AddApplicationPart(Assembly.GetEntryAssembly()).AddControllersAsServices(); services.Configure <RazorViewEngineOptions>(y => { y.ViewLocationFormats.Clear(); y.ViewLocationFormats.Add("/Services/Internals/Http/Views/{1}/{0}.cshtml"); y.ViewLocationFormats.Add("/Views/{1}/{0}.cshtml"); y.FileProviders.Add(new EmbeddedFileProvider(typeof(Startup).Assembly)); y.FileProviders.Add(new EmbeddedFileProvider(Assembly.GetEntryAssembly())); }); }
public void XmlSerializer_CanWriteResult_ReturnsFalse_ForNonWritableType() { // Arrange var formatter = new XmlSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(outputValue: null, outputType: typeof(Dictionary <string, string>)); // Act & Assert Assert.False(formatter.CanWriteResult(outputFormatterContext, MediaTypeHeaderValue.Parse("application/xml"))); }
public void XmlDataContractSerializer_CanWriteResult_ReturnsTrue_ForWritableType() { // Arrange var formatter = new XmlSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(outputValue: null, outputType: typeof(string)); // Act & Assert Assert.True(formatter.CanWriteResult(outputFormatterContext, MediaTypeHeaderValue.Parse("application/xml"))); }
public Task ExecuteAsync(ActionContext context, XmlResult result) { var serializerSettings = result.XmlSerializerSettings ?? FormattingUtilities.GetDefaultXmlWriterSettings(); TextOutputFormatter formatter; // create the proper formatter formatter = new XmlSerializerOutputFormatter(serializerSettings); XmlResultExecutorBase xmlBase = new XmlResultExecutorBase(WriterFactory, LoggerFactory, formatter); return(xmlBase.ExecuteAsync(context, result)); }
public void XmlSerializer_DoesNotThrow_OnNoLoggerAnd_WhenUnableToCreateSerializerForType() { // Arrange var formatter = new XmlSerializerOutputFormatter(); // no logger is being supplied here on purpose var outputFormatterContext = GetOutputFormatterContext(new Customer(10), typeof(Customer)); // Act var canWriteResult = formatter.CanWriteResult(outputFormatterContext); // Assert Assert.False(canWriteResult); }
public void XmlSerializer_CanWriteResult(object input, Type declaredType, bool expectedOutput) { // Arrange var formatter = new XmlSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(input, declaredType); // Act var result = formatter.CanWriteResult(outputFormatterContext, MediaTypeHeaderValue.Parse("application/xml")); // Assert Assert.Equal(expectedOutput, result); }
// Set up application services public void ConfigureServices(IServiceCollection services) { // Add MVC services to the services container services.AddMvc() .SetCompatibilityVersion(CompatibilityVersion.Latest); services.Configure <MvcOptions>(options => { options.InputFormatters.Clear(); options.OutputFormatters.Clear(); // Since both XmlSerializer and DataContractSerializer based formatters // have supported media types of 'application/xml' and 'text/xml', it // would be difficult for a test to choose a particular formatter based on // request information (Ex: Accept header). // So here we instead clear out the default supported media types and create new // ones which are distinguishable between formatters. var xmlSerializerInputFormatter = new XmlSerializerInputFormatter(new MvcOptions()); xmlSerializerInputFormatter.SupportedMediaTypes.Clear(); xmlSerializerInputFormatter.SupportedMediaTypes.Add( new MediaTypeHeaderValue("application/xml-xmlser")); xmlSerializerInputFormatter.SupportedMediaTypes.Add( new MediaTypeHeaderValue("text/xml-xmlser")); var xmlSerializerOutputFormatter = new XmlSerializerOutputFormatter(); xmlSerializerOutputFormatter.SupportedMediaTypes.Clear(); xmlSerializerOutputFormatter.SupportedMediaTypes.Add( new MediaTypeHeaderValue("application/xml-xmlser")); xmlSerializerOutputFormatter.SupportedMediaTypes.Add( new MediaTypeHeaderValue("text/xml-xmlser")); var dcsInputFormatter = new XmlDataContractSerializerInputFormatter(new MvcOptions()); dcsInputFormatter.SupportedMediaTypes.Clear(); dcsInputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xml-dcs")); dcsInputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/xml-dcs")); var dcsOutputFormatter = new XmlDataContractSerializerOutputFormatter(); dcsOutputFormatter.SupportedMediaTypes.Clear(); dcsOutputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xml-dcs")); dcsOutputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/xml-dcs")); options.InputFormatters.Add(dcsInputFormatter); options.InputFormatters.Add(xmlSerializerInputFormatter); options.OutputFormatters.Add(dcsOutputFormatter); options.OutputFormatters.Add(xmlSerializerOutputFormatter); xmlSerializerInputFormatter.WrapperProviderFactories.Add(new PersonWrapperProviderFactory()); xmlSerializerOutputFormatter.WrapperProviderFactories.Add(new PersonWrapperProviderFactory()); dcsInputFormatter.WrapperProviderFactories.Add(new PersonWrapperProviderFactory()); dcsOutputFormatter.WrapperProviderFactories.Add(new PersonWrapperProviderFactory()); }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { IConfigurationSection azureADConfig = Configuration.GetSection("AzureAD"); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.Authority = $"{azureADConfig.GetValue<string>("Authority")}/{azureADConfig.GetValue<string>("TenantId")}/"; options.Audience = azureADConfig.GetValue <string>("Audience"); }); services.AddVersionedApiExplorer( options => { options.GroupNameFormat = "'v'VVV"; options.SubstituteApiVersionInUrl = true; }); services.AddMvc(options => { options.OutputFormatters.RemoveType <StringOutputFormatter>(); options.OutputFormatters.Add(new XmlSerializerOutputFormatter()); JsonOutputFormatter jFormatter = options.OutputFormatters.FirstOrDefault(f => f.GetType() == typeof(JsonOutputFormatter)) as JsonOutputFormatter; jFormatter?.SupportedMediaTypes.Clear(); jFormatter?.SupportedMediaTypes.Add("application/atom+json"); jFormatter?.SupportedMediaTypes.Add("application/json"); XmlSerializerOutputFormatter xFormatter = options.OutputFormatters.FirstOrDefault(f => f.GetType() == typeof(XmlSerializerOutputFormatter)) as XmlSerializerOutputFormatter; xFormatter?.SupportedMediaTypes.Clear(); xFormatter?.SupportedMediaTypes.Add("application/atom+xml"); xFormatter?.SupportedMediaTypes.Add("application/xml"); }).AddJsonOptions(options => { options.SerializerSettings.Formatting = Formatting.Indented; }) // 2.1 versioning still needs to be enabled to support API versioning for endpoints .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddApiVersioning( o => { o.ReportApiVersions = true; o.AssumeDefaultVersionWhenUnspecified = true; o.DefaultApiVersion = new ApiVersion(1, 0); }); RegisterComponents(services); SwaggerSetup.ConfigureSwaggerServices(services); }
public void DefaultConstructor_ExpectedWriterSettings_Created() { // Arrange and Act var formatter = new XmlSerializerOutputFormatter(); // Assert var writerSettings = formatter.WriterSettings; Assert.NotNull(writerSettings); Assert.True(writerSettings.OmitXmlDeclaration); Assert.False(writerSettings.CloseOutput); Assert.False(writerSettings.CheckCharacters); }
public void XmlSerializer_CanWriteResult(object input, Type declaredType, bool expectedOutput) { // Arrange var formatter = new XmlSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(input, declaredType); outputFormatterContext.ContentType = new StringSegment("application/xml"); // Act var result = formatter.CanWriteResult(outputFormatterContext); // Assert Assert.Equal(expectedOutput, result); }
public async Task VerifyBodyIsNotClosedAfterOutputIsWritten() { // Arrange var sampleInput = new DummyClass { SampleInt = 10 }; var formatter = new XmlSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); // Act await formatter.WriteAsync(outputFormatterContext); // Assert Assert.NotNull(outputFormatterContext.ActionContext.HttpContext.Response.Body); Assert.True(outputFormatterContext.ActionContext.HttpContext.Response.Body.CanRead); }
public async Task XmlSerializerOutputFormatterCanWriteBasicTypes(object input, string expectedOutput) { // Arrange var formatter = new XmlSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(input, input.GetType()); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public async Task XmlSerializerOutputFormatterDoesntFlushOutputStream() { // Arrange var sampleInput = new DummyClass { SampleInt = 10 }; var formatter = new XmlSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); var response = outputFormatterContext.ActionContext.HttpContext.Response; response.Body = FlushReportingStream.GetThrowingStream(); // Act & Assert await formatter.WriteAsync(outputFormatterContext); }
public async Task XmlSerializerOutputFormatterCanWriteBasicTypes(object input, string expectedOutput) { // Arrange var formatter = new XmlSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(input, typeof(object)); // Act await formatter.WriteAsync(outputFormatterContext); // Assert Assert.NotNull(outputFormatterContext.ActionContext.HttpContext.Response.Body); outputFormatterContext.ActionContext.HttpContext.Response.Body.Position = 0; Assert.Equal(expectedOutput, new StreamReader(outputFormatterContext.ActionContext.HttpContext.Response.Body, Encoding.UTF8) .ReadToEnd()); Assert.True(outputFormatterContext.ActionContext.HttpContext.Response.Body.CanRead); }
public async Task XmlSerializerOutputFormatterWritesContentLengthResponse() { // Arrange var sampleInput = new DummyClass { SampleInt = 10 }; var formatter = new XmlSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); var response = outputFormatterContext.HttpContext.Response; response.Body = Stream.Null; // Act & Assert await formatter.WriteAsync(outputFormatterContext); Assert.NotNull(outputFormatterContext.HttpContext.Response.ContentLength); }
public void XmlSerializer_GetSupportedContentTypes_Returns_SupportedTypes(Type type, object expectedOutput) { // Arrange var formatter = new XmlSerializerOutputFormatter(); // Act var result = formatter.GetSupportedContentTypes("application/xml", type); // Assert if (expectedOutput != null) { Assert.Equal(expectedOutput, Assert.Single(result).ToString()); } else { Assert.Equal(expectedOutput, result); } }
public void XmlSerializer_GetSupportedContentTypes_Returns_SupportedTypes(Type declaredType, Type runtimeType, object expectedOutput) { // Arrange var formatter = new XmlSerializerOutputFormatter(); // Act var result = formatter.GetSupportedContentTypes( declaredType, runtimeType, MediaTypeHeaderValue.Parse("application/xml")); // Assert if (expectedOutput != null) { Assert.Equal(expectedOutput, Assert.Single(result).ToString()); } else { Assert.Equal(expectedOutput, result); } }
public void XmlSerializer_LogsWhenUnableToCreateSerializerForType( XmlSerializerOutputFormatter formatter, TestSink sink) { // Arrange var outputFormatterContext = GetOutputFormatterContext(new Customer(10), typeof(Customer)); // Act var canWriteResult = formatter.CanWriteResult(outputFormatterContext); // Assert Assert.False(canWriteResult); var write = Assert.Single(sink.Writes); Assert.Equal(LogLevel.Warning, write.LogLevel); Assert.Equal( $"An error occurred while trying to create an XmlSerializer for the type '{typeof(Customer).FullName}'.", write.State.ToString()); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; options.InputFormatters.Add(new XmlSerializerInputFormatter(options)); var xmlSerializerOutputFormatter = new XmlSerializerOutputFormatter(); xmlSerializerOutputFormatter.SupportedMediaTypes.Add("application/vnd.null.entity+xml"); xmlSerializerOutputFormatter.SupportedMediaTypes.Add("application/vnd.null.entity+xml; v=1.0"); xmlSerializerOutputFormatter.SupportedMediaTypes.Add("application/vnd.null.entity+xml; v=2.0"); options.OutputFormatters.Add(xmlSerializerOutputFormatter); }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddSingleton <IVehicleRepositorical>( new CollectionStubVehiclesRepository(new[] { new Vehicle { VehicleIdentifier = "3" }, new Vehicle { VehicleIdentifier = "4" } })); var defaultVersion = new ApiVersion(1, 0); services.AddApiVersioning(options => { options.ApiVersionReader = new MediaTypeApiVersionReader(); options.AssumeDefaultVersionWhenUnspecified = true; options.ReportApiVersions = true; options.AssumeDefaultVersionWhenUnspecified = true; options.Conventions .Controller <VehiclesControllerV2>() .HasApiVersion(2, 0); options.Conventions .Controller <VehiclesController>() .HasApiVersion(defaultVersion); options.DefaultApiVersion = defaultVersion; //options.AddConventionsFromAssembly(); }); }
public async Task XmlSerializerOutputFormatterWritesOnModifiedWriterSettings() { // Arrange var expectedOutput = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<DummyClass xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SampleInt>10</SampleInt></DummyClass>"; var sampleInput = new DummyClass { SampleInt = 10 }; var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); var formatter = new XmlSerializerOutputFormatter( new System.Xml.XmlWriterSettings { OmitXmlDeclaration = false, CloseOutput = false }); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public async Task SuppliedWriterSettings_TakeAffect() { // Arrange var writerSettings = FormattingUtilities.GetDefaultXmlWriterSettings(); writerSettings.OmitXmlDeclaration = false; var sampleInput = new DummyClass { SampleInt = 10 }; var formatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); var formatter = new XmlSerializerOutputFormatter(writerSettings); var expectedOutput = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<DummyClass xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SampleInt>10</SampleInt></DummyClass>"; // Act await formatter.WriteAsync(formatterContext); // Assert var body = formatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public async Task XmlSerializerOutputFormatterWritesComplexTypes() { // Arrange var expectedOutput = "<TestLevelTwo xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SampleString>TestString</SampleString>" + "<TestOne><sampleString>TestLevelOne string</sampleString>" + "<SampleInt>10</SampleInt></TestOne></TestLevelTwo>"; var sampleInput = new TestLevelTwo { SampleString = "TestString", TestOne = new TestLevelOne { SampleInt = 10, sampleString = "TestLevelOne string" } }; var formatter = new XmlSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public async Task VerifyBodyIsNotClosedAfterOutputIsWritten() { // Arrange var sampleInput = new DummyClass { SampleInt = 10 }; var formatter = new XmlSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); // Act await formatter.WriteAsync(outputFormatterContext); // Assert Assert.NotNull(outputFormatterContext.HttpContext.Response.Body); Assert.True(outputFormatterContext.HttpContext.Response.Body.CanRead); }
public async Task XmlSerializerOutputFormatterDoesntFlushOutputStream() { // Arrange var sampleInput = new DummyClass { SampleInt = 10 }; var formatter = new XmlSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); var response = outputFormatterContext.HttpContext.Response; response.Body = FlushReportingStream.GetThrowingStream(); // Act & Assert await formatter.WriteAsync(outputFormatterContext); }