Skip to content

Commit

Permalink
Add NetHttpsBinding test coverage.
Browse files Browse the repository at this point in the history
* Adding tests for NetHttpsBinding.
* Fixes dotnet#507
  • Loading branch information
StephenBonikowsky committed May 16, 2016
1 parent e671f7c commit f641d96
Show file tree
Hide file tree
Showing 8 changed files with 346 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ public static string HttpsSoap12_Address
}
}

public static string HttpBaseAddress_NetHttps
{
get
{
return GetEndpointAddress("NetHttps.svc//NetHttps", protocol: "https");
}
}

#region Secure WebSocket Addresses
public static string WebSocketHttpsDuplexBinaryStreamed_Address
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

public static class Binding_Http_NetHttpsBindingTests
{
[Fact]
[OuterLoop]
public static void DefaultCtor_NetHttps_Echo_RoundTrips_String()
{
string testString = "Hello";
ChannelFactory<IWcfService> factory = null;
IWcfService serviceProxy = null;

try
{
// *** SETUP *** \\
NetHttpsBinding netHttpsBinding = new NetHttpsBinding();
factory = new ChannelFactory<IWcfService>(netHttpsBinding, new EndpointAddress(Endpoints.HttpBaseAddress_NetHttps));
serviceProxy = factory.CreateChannel();

// *** EXECUTE *** \\
string result = serviceProxy.Echo(testString);

// *** VALIDATE *** \\
Assert.True(String.Equals(testString, result), String.Format("Expected result was {0}. Actual was {1}", testString, result));

// *** CLEANUP *** \\
factory.Close();
((ICommunicationObject)serviceProxy).Close();
}
finally
{
// *** ENSURE CLEANUP *** \\
ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
}
}

[Fact]
[OuterLoop]
public static void TransportWithMessageCredential_NotSupported_NetHttps()
{
string testString = "Hello";
ChannelFactory<IWcfService> factory = null;
IWcfService serviceProxy = null;

// BasicHttpsSecurityMode.TransportWithMessageCredential is accessible but not supported.
// Verify the correct exception and message is thrown.
// When/if Message Security is supported this test will fail and will serve as a reminder to add test coverage.
Assert.Throws<PlatformNotSupportedException>(() =>
{
try
{
// *** SETUP *** \\
NetHttpsBinding netHttpsBinding = new NetHttpsBinding(BasicHttpsSecurityMode.TransportWithMessageCredential);
factory = new ChannelFactory<IWcfService>(netHttpsBinding, new EndpointAddress(Endpoints.HttpBaseAddress_NetHttps));
serviceProxy = factory.CreateChannel();

// *** EXECUTE *** \\
string result = serviceProxy.Echo(testString);
}
finally
{
// *** ENSURE CLEANUP *** \\
ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
}
});
}

[Fact]
[OuterLoop]
public static void NonDefaultCtor_NetHttps_Echo_RoundTrips_String()
{
string testString = "Hello";
ChannelFactory<IWcfService> factory = null;
IWcfService serviceProxy = null;

try
{
// *** SETUP *** \\
NetHttpsBinding netHttpsBinding = new NetHttpsBinding(BasicHttpsSecurityMode.Transport);
factory = new ChannelFactory<IWcfService>(netHttpsBinding, new EndpointAddress(Endpoints.HttpBaseAddress_NetHttps));
serviceProxy = factory.CreateChannel();

// *** EXECUTE *** \\
string result = serviceProxy.Echo(testString);

// *** VALIDATE *** \\
Assert.NotNull(result);
Assert.Equal(testString, result);

// *** CLEANUP *** \\
factory.Close();
((ICommunicationObject)serviceProxy).Close();
}
finally
{
// *** ENSURE CLEANUP *** \\
ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<ProjectReference Include='$(WcfScenarioTestCommonProj)'>
<Project>{9098B41C-9C2E-4FC6-B7D8-FC3411736A22}</Project>
<Name>ScenarioTests.Common</Name>
</ProjectReference>
<ProjectReference Include="$(WcfInfrastructureCommonProj)">
<Project>{AFD69665-89A3-42AE-A32E-AB2CBBE6EE7E}</Project>
<Name>Infrastructure.Common</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using Infrastructure.Common;
using Xunit;

public static class DuplexChannelShapeTests
public partial class DuplexChannelShapeTests : ConditionalWcfTest
{
// Creating a ChannelFactory using a binding's 'BuildChannelFactory' method and providing a channel shape...
// returns a concrete type determined by the channel shape requested and other binding related settings.
Expand Down Expand Up @@ -132,4 +133,53 @@ public static void IDuplexSessionChannel_Async_Tcp_NetTcpBinding()
ScenarioTestHelpers.CloseCommunicationObjects(channel, factory);
}
}

[Fact]
[OuterLoop]
public static void IRequestChannel_Https_NetHttpsBinding()
{
IChannelFactory<IRequestChannel> factory = null;
IRequestChannel channel = null;
Message replyMessage = null;

try
{
// *** SETUP *** \\
NetHttpsBinding binding = new NetHttpsBinding(BasicHttpsSecurityMode.Transport);

// Create the channel factory
factory = binding.BuildChannelFactory<IRequestChannel>(new BindingParameterCollection());
factory.Open();

// Create the channel.
channel = factory.CreateChannel(new EndpointAddress(Endpoints.HttpBaseAddress_NetHttps));
channel.Open();

// Create the Message object to send to the service.
Message requestMessage = Message.CreateMessage(
binding.MessageVersion,
action,
new CustomBodyWriter(clientMessage));

// *** EXECUTE *** \\
// Send the Message and receive the Response.
replyMessage = channel.Request(requestMessage);

// *** VALIDATE *** \\
var replyReader = replyMessage.GetReaderAtBodyContents();
string actualResponse = replyReader.ReadElementContentAsString();
string expectedResponse = "[client] This is my request.[service] Request received, this is my Reply.";
Assert.Equal(expectedResponse, actualResponse);

// *** CLEANUP *** \\
replyMessage.Close();
channel.Close();
factory.Close();
}
finally
{
// *** ENSURE CLEANUP *** \\
ScenarioTestHelpers.CloseCommunicationObjects(channel, factory);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Xml;
using Infrastructure.Common;
using Xunit;

public partial class DuplexChannelShapeTests : ConditionalWcfTest
{
// Creating a ChannelFactory using a binding's 'BuildChannelFactory' method and providing a channel shape...
// returns a concrete type determined by the channel shape requested and other binding related settings.
// The tests in this file use the IDuplexChannel shape.

[ConditionalFact(nameof(Root_Certificate_Installed))]
[OuterLoop]
[ActiveIssue(1157)]
public static void IDuplexSessionChannel_Https_NetHttpsBinding()
{
IChannelFactory<IDuplexSessionChannel> factory = null;
IDuplexSessionChannel channel = null;
Message replyMessage = null;

try
{
// *** SETUP *** \\
NetHttpsBinding binding = new NetHttpsBinding(BasicHttpsSecurityMode.Transport);

// Create the channel factory
factory = binding.BuildChannelFactory<IDuplexSessionChannel>(new BindingParameterCollection());
factory.Open();

// Create the channel.
channel = factory.CreateChannel(new EndpointAddress(Endpoints.HttpBaseAddress_NetHttps));
channel.Open();

// Create the Message object to send to the service.
Message requestMessage = Message.CreateMessage(
binding.MessageVersion,
action,
new CustomBodyWriter(clientMessage));
requestMessage.Headers.MessageId = new UniqueId(Guid.NewGuid());

// *** EXECUTE *** \\
// Send the Message and receive the Response.
channel.Send(requestMessage);
replyMessage = channel.Receive(TimeSpan.FromSeconds(5));

// *** VALIDATE *** \\
// If the incoming Message did not contain the same UniqueId used for the MessageId of the outgoing Message we would have received a Fault from the Service
string expectedMessageID = requestMessage.Headers.MessageId.ToString();
string actualMessageID = replyMessage.Headers.RelatesTo.ToString();
Assert.True(String.Equals(expectedMessageID, actualMessageID), String.Format("Expected Message ID was {0}. Actual was {1}", expectedMessageID, actualMessageID));

// Validate the Response
var replyReader = replyMessage.GetReaderAtBodyContents();
string actualResponse = replyReader.ReadElementContentAsString();
string expectedResponse = "[client] This is my request.[service] Request received, this is my Reply.";
Assert.Equal(expectedResponse, actualResponse);

// *** CLEANUP *** \\
replyMessage.Close();
channel.Close();
factory.Close();
}
finally
{
// *** ENSURE CLEANUP *** \\
ScenarioTestHelpers.CloseCommunicationObjects(channel, factory);
}
}

[ConditionalFact(nameof(Root_Certificate_Installed))]
[OuterLoop]
[ActiveIssue(1157)]
public static void IDuplexSessionChannel_Http_BasicHttpBinding()
{
IChannelFactory<IDuplexSessionChannel> factory = null;
IDuplexSessionChannel channel = null;
Message replyMessage = null;

try
{
// *** SETUP *** \\
BasicHttpBinding binding = new BasicHttpBinding();

// Create the channel factory
factory = binding.BuildChannelFactory<IDuplexSessionChannel>(new BindingParameterCollection());
factory.Open();

// Create the channel.
channel = factory.CreateChannel(new EndpointAddress(Endpoints.HttpBaseAddress_NetHttps));
channel.Open();

// Create the Message object to send to the service.
Message requestMessage = Message.CreateMessage(
binding.MessageVersion,
action,
new CustomBodyWriter(clientMessage));
requestMessage.Headers.MessageId = new UniqueId(Guid.NewGuid());

// *** EXECUTE *** \\
// Send the Message and receive the Response.
channel.Send(requestMessage);
replyMessage = channel.Receive(TimeSpan.FromSeconds(5));

// *** VALIDATE *** \\
// If the incoming Message did not contain the same UniqueId used for the MessageId of the outgoing Message we would have received a Fault from the Service
Assert.Equal(requestMessage.Headers.MessageId.ToString(), replyMessage.Headers.RelatesTo.ToString());

// Validate the Response
var replyReader = replyMessage.GetReaderAtBodyContents();
string actualResponse = replyReader.ReadElementContentAsString();
string expectedResponse = "[client] This is my request.[service] Request received, this is my Reply.";
Assert.Equal(expectedResponse, actualResponse);

// *** CLEANUP *** \\
replyMessage.Close();
channel.Close();
factory.Close();
}
finally
{
// *** ENSURE CLEANUP *** \\
ScenarioTestHelpers.CloseCommunicationObjects(channel, factory);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Channels;

namespace WcfService
{
public class NetHttpsTestServiceHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
NetHttpsTestServiceHost serviceHost = new NetHttpsTestServiceHost(serviceType, baseAddresses);
return serviceHost;
}
}
public class NetHttpsTestServiceHost : TestServiceHostBase<IWcfService>
{
protected override string Address { get { return "NetHttps"; } }

protected override Binding GetBinding()
{
return new NetHttpsBinding();
}

public NetHttpsTestServiceHost(Type serviceType, params Uri[] baseAddresses)
: base(serviceType, baseAddresses)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<add factory="WcfService.HttpsWindowsTestServiceHostFactory" service="WcfService.WcfService" relativeAddress="~\HttpsWindows.svc" />
<add factory="WcfService.HttpWindowsTestServiceHostFactory" service="WcfService.WcfService" relativeAddress="~\HttpWindows.svc" />
<add factory="WcfService.NetHttpTestServiceHostFactory" service="WcfService.WcfService" relativeAddress="~\NetHttp.svc" />
<add factory="WcfService.NetHttpsTestServiceHostFactory" service="WcfService.WcfService" relativeAddress="~\NetHttps.svc" />
<add factory="WcfService.ServiceContractAsyncIntOutTestServiceHostFactory" service="WcfService.ServiceContractIntOutService" relativeAddress="~\ServiceContractAsyncIntOut.svc" />
<add factory="WcfService.ServiceContractAsyncUniqueTypeOutTestServiceHostFactory" service="WcfService.ServiceContractUniqueTypeOutService" relativeAddress="~\ServiceContractAsyncUniqueTypeOut.svc" />
<add factory="WcfService.ServiceContractAsyncIntRefTestServiceHostFactory" service="WcfService.ServiceContractIntRefService" relativeAddress="~\ServiceContractAsyncIntRef.svc" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ private static void Main()
NetHttpTestServiceHost netHttpTestServiceHostServiceHost = new NetHttpTestServiceHost(typeof(WcfService.WcfService), netHttpTestServiceHostbaseAddress);
netHttpTestServiceHostServiceHost.Open();

Uri[] netHttpsTestServiceHostbaseAddress = new Uri[] { new Uri(string.Format("{0}/NetHttps.svc", httpsBaseAddress)) };
NetHttpsTestServiceHost netHttpsTestServiceHostServiceHost = new NetHttpsTestServiceHost(typeof(WcfService.WcfService), netHttpsTestServiceHostbaseAddress);
netHttpsTestServiceHostServiceHost.Open();

Uri[] serviceContractAsyncIntOutTestServiceHostbaseAddress = new Uri[] { new Uri(string.Format("{0}/ServiceContractAsyncIntOut.svc", httpBaseAddress)) };
ServiceContractAsyncIntOutTestServiceHost serviceContractAsyncIntOutTestServiceHostServiceHost = new ServiceContractAsyncIntOutTestServiceHost(typeof(WcfService.ServiceContractIntOutService), serviceContractAsyncIntOutTestServiceHostbaseAddress);
serviceContractAsyncIntOutTestServiceHostServiceHost.Open();
Expand Down

0 comments on commit f641d96

Please sign in to comment.