Last active
December 12, 2015 10:39
-
-
Save k0stya/4760685 to your computer and use it in GitHub Desktop.
Helper class that allows to do integration testing of WCF services using AspNetDevelopmentServerAttribute.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example of usage | |
[TestMethod] | |
[AspNetDevelopmentServer("WcfService", "Physical path to your WCF project")] | |
public void TestMethod1() | |
{ | |
ServiceClient target = new ServiceClient(); | |
Assert.IsTrue(WcfWebServiceHelper.TryUrlRedirection(target, TestContext, "WcfService")); | |
int value = 0; | |
string expected = "You entered: 0"; | |
string actual; | |
actual = target.GetData(value); | |
Assert.AreEqual(expected, actual); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WcfWebServiceHelper | |
{ | |
public static bool TryUrlRedirection(object client, TestContext context, string identifier) | |
{ | |
bool result = true; | |
try | |
{ | |
PropertyInfo property = client.GetType().GetProperty("Endpoint"); | |
string webServer = context.Properties[ | |
string.Format("AspNetDevelopmentServer.{0}", identifier)].ToString(); | |
Uri webServerUri = new Uri(webServer); | |
ServiceEndpoint endpoint = (ServiceEndpoint)property.GetValue(client, null); | |
EndpointAddressBuilder builder = new EndpointAddressBuilder(endpoint.Address); | |
builder.Uri = new Uri( | |
endpoint.Address.Uri.OriginalString.Replace( | |
endpoint.Address.Uri.Authority, webServerUri.Authority)); | |
endpoint.Address = builder.ToEndpointAddress(); | |
} | |
catch (Exception e) | |
{ | |
context.WriteLine(e.Message); | |
result = false; | |
} | |
return result; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment