Skip to content

Commit

Permalink
Merge pull request #429 from microknights/feature/ISO-8859-1
Browse files Browse the repository at this point in the history
Support for ISO-8859-1
  • Loading branch information
kotovaleksandr authored Feb 12, 2020
2 parents 6afc329 + aa986b4 commit 12e364d
Show file tree
Hide file tree
Showing 7 changed files with 437 additions and 7 deletions.
34 changes: 34 additions & 0 deletions src/SoapCore.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SoapCore.Tests.Model;
using SoapCore.Tests.Utilities;

namespace SoapCore.Tests
{
Expand Down Expand Up @@ -42,6 +44,14 @@ public void PingSoap12()
Assert.AreEqual("hello, world", result);
}

[TestMethod]
public void PingSoap11Iso55891()
{
var client = CreateSoap11Iso88591Client();
var result = client.Ping("hello, world");
Assert.AreEqual("hello, world", result);
}

[TestMethod]
public void Ping()
{
Expand Down Expand Up @@ -157,6 +167,19 @@ public void ExceptionMessageSoap12()
Assert.AreEqual("Your error message here", e.Message);
}

[TestMethod]
public void ExceptionMessageSoap11iso88591()
{
var client = CreateSoap11Iso88591Client();

var e = Assert.ThrowsException<FaultException>(() =>
{
client.ThrowExceptionWithMessage("Your error message here");
});

Assert.AreEqual("Your error message here", e.Message);
}

[TestMethod]
public void ThrowsDetailedFault()
{
Expand Down Expand Up @@ -203,5 +226,16 @@ private ITestService CreateSoap12Client()
var serviceClient = channelFactory.CreateChannel();
return serviceClient;
}

private ITestService CreateSoap11Iso88591Client()
{
var transport = new HttpTransportBindingElement();
var textencoding = new CustomTextMessageBindingElement("iso-8859-1", "text/xml", MessageVersion.Soap11);
var binding = new CustomBinding(textencoding, transport);
var endpoint = new EndpointAddress(new Uri(string.Format("http://{0}:5050/WSA11ISO88591Service.svc", "localhost")));
var channelFactory = new ChannelFactory<ITestService>(binding, endpoint);
var serviceClient = channelFactory.CreateChannel();
return serviceClient;
}
}
}
23 changes: 23 additions & 0 deletions src/SoapCore.Tests/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -56,6 +57,17 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
app.UseSoapEndpoint<TestService>("/WSA10Service.svc", new CustomBinding(transportBinding, textEncodingBinding), SoapSerializer.DataContractSerializer);
});

app.UseWhen(ctx => ctx.Request.Path.Value.Contains("/WSA11ISO88591Service.svc"), app2 =>
{
var soapEncodingOptions = new SoapEncoderOptions
{
MessageVersion = MessageVersion.Soap11,
WriteEncoding = Encoding.GetEncoding("ISO-8859-1")
};

app.UseSoapEndpoint<TestService>("/WSA11ISO88591Service.svc", soapEncodingOptions, SoapSerializer.DataContractSerializer);
});

app.UseMvc();
}
#endif
Expand Down Expand Up @@ -114,6 +126,17 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF

app.UseSoapEndpoint<TestService>("/WSA10Service.svc", new CustomBinding(transportBinding, textEncodingBinding), SoapSerializer.DataContractSerializer);
});

app.UseWhen(ctx => ctx.Request.Path.Value.Contains("/WSA11ISO88591Service.svc"), app2 =>
{
var soapEncodingOptions = new SoapEncoderOptions
{
MessageVersion = MessageVersion.Soap11,
WriteEncoding = Encoding.GetEncoding("ISO-8859-1")
};

app.UseSoapEndpoint<TestService>("/WSA11ISO88591Service.svc", soapEncodingOptions, SoapSerializer.DataContractSerializer);
});
}
#endif
}
Expand Down
198 changes: 198 additions & 0 deletions src/SoapCore.Tests/Utilities/CustomTextMessageBindingElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
using System;
using System.ServiceModel.Channels;
using System.Xml;

namespace SoapCore.Tests.Utilities
{
public class CustomTextMessageBindingElement : MessageEncodingBindingElement //, IWsdlExportExtension
{
private readonly XmlDictionaryReaderQuotas _readerQuotas;
private MessageVersion _msgVersion;
private string _mediaType;
private string _encoding;

public CustomTextMessageBindingElement(string encoding, string mediaType, MessageVersion msgVersion)
{
if (encoding == null)
{
throw new ArgumentNullException("encoding");
}

if (mediaType == null)
{
throw new ArgumentNullException("mediaType");
}

if (msgVersion == null)
{
throw new ArgumentNullException("msgVersion");
}

_msgVersion = msgVersion;
_mediaType = mediaType;
_encoding = encoding;
_readerQuotas = new XmlDictionaryReaderQuotas();
}

public CustomTextMessageBindingElement(string encoding, string mediaType)
: this(encoding, mediaType, MessageVersion.Soap11)
{
}

public CustomTextMessageBindingElement(string encoding)
: this(encoding, "text/xml")
{
}

public CustomTextMessageBindingElement()
: this("UTF-8")
{
}

private CustomTextMessageBindingElement(CustomTextMessageBindingElement binding)
: this(binding.Encoding, binding.MediaType, binding.MessageVersion)
{
_readerQuotas = new XmlDictionaryReaderQuotas();
binding.ReaderQuotas.CopyTo(_readerQuotas);
}

public override MessageVersion MessageVersion
{
get
{
return _msgVersion;
}

set
{
if (value == null)
{
throw new ArgumentNullException("value");
}

_msgVersion = value;
}
}

public string MediaType
{
get
{
return _mediaType;
}

set
{
if (value == null)
{
throw new ArgumentNullException("value");
}

_mediaType = value;
}
}

public string Encoding
{
get
{
return _encoding;
}

set
{
if (value == null)
{
throw new ArgumentNullException("value");
}

_encoding = value;
}
}

// This encoder does not enforces any quotas for the unsecure messages. The
// quotas are enforced for the secure portions of messages when this encoder
// is used in a binding that is configured with security.
public XmlDictionaryReaderQuotas ReaderQuotas
{
get { return _readerQuotas; }
}

public override MessageEncoderFactory CreateMessageEncoderFactory()
{
return new CustomTextMessageEncoderFactory(MediaType, Encoding, MessageVersion);
}

public override BindingElement Clone()
{
return new CustomTextMessageBindingElement(this);
}

public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}

context.BindingParameters.Add(this);
return context.BuildInnerChannelFactory<TChannel>();
}

public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}

return context.CanBuildInnerChannelFactory<TChannel>();
}

// public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
// {
// if (context == null)
// throw new ArgumentNullException("context");
//
// context.BindingParameters.Add(this);
// return context.BuildInnerChannelListener<TChannel>();
// }
//
// public override bool CanBuildChannelListener<TChannel>(BindingContext context)
// {
// if (context == null)
// throw new ArgumentNullException("context");
//
// context.BindingParameters.Add(this);
// return context.CanBuildInnerChannelListener<TChannel>();
// }
public override T GetProperty<T>(BindingContext context)
{
if (typeof(T) == typeof(XmlDictionaryReaderQuotas))
{
return (T)(object)_readerQuotas;
}
else
{
return base.GetProperty<T>(context);
}
}

// #region IWsdlExportExtension Members
//
// void IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext context)
// {
// }
//
// void IWsdlExportExtension.ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context)
// {
// // The MessageEncodingBindingElement is responsible for ensuring that the WSDL has the correct
// // SOAP version. We can delegate to the WCF implementation of TextMessageEncodingBindingElement for
// TextMessageEncodingBindingElement mebe = new TextMessageEncodingBindingElement();
// mebe.MessageVersion = msgVersion;
// ((IWsdlExportExtension) mebe).ExportEndpoint(exporter, context);
// }
//
// #endregion
}
}
Loading

0 comments on commit 12e364d

Please sign in to comment.