/// <summary> /// Asserts that an <see cref="HttpRequestMessage"/> sent to the self-hosted HTTP <paramref name="serviceSingleton"/> /// results in the actual <see cref="HttpResponseMessage"/> given by the <paramref name="onGetActualResponse"/> <see cref="Action<>"/>. /// </summary> /// <param name="serviceSingleton">The singleton for the self-hosted HTTP service. Should not be <c>null</c>.</param> /// <param name="behavior">The <see cref="HttpBehavior"/> to use with the HTTP service.</param> /// <param name="binding">The <see cref="HttpBinding"/> to use with the HTTP service.</param> /// <param name="request">The <see cref="HttpRequestMessage"/> instance to send to the self-hosted HTTP service. Should not be <c>null</c>.</param> /// <param name="onGetActualResponse">The actual <see cref="HttpResponseMessage"/> instance provided as an <see cref="Action<>"/>. Should not be <c>null</c>.</param> public static void Execute(object serviceSingleton, HttpBehavior behavior, HttpBinding binding, HttpRequestMessage request, Action<HttpResponseMessage> onGetActualResponse) { Assert.IsNotNull(serviceSingleton, "The 'serviceSingleton' parameter should not be null."); Assert.IsNotNull(request, "The 'request' parameter should not be null."); Assert.IsNotNull(onGetActualResponse, "The 'onGetActualResponse' parameter should not be null."); ServiceHostAssert.Execute((baseAddress) => GetServiceHost(serviceSingleton, behavior, binding, baseAddress), request, onGetActualResponse); }
public void Validate_Throws_With_Non_ManualAddressing() { HttpBehavior behavior = new HttpBehavior(); ServiceEndpoint endpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(CustomerService))); BindingElementCollection bindingElements = new HttpBinding().CreateBindingElements(); (bindingElements[bindingElements.Count - 1] as HttpTransportBindingElement).ManualAddressing = false; endpoint.Binding = new CustomBinding(bindingElements); endpoint.Address = new EndpointAddress("http://somehost"); ExceptionAssert.Throws<InvalidOperationException>( "Non-manual addressing should throw", Http.SR.InvalidManualAddressingValue(endpoint.Address.Uri.AbsoluteUri), () => behavior.Validate(endpoint)); }
public void Validate_Throws_With_Non_Http_Binding() { HttpBehavior behavior = new HttpBehavior(); ServiceEndpoint endpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(CustomerService))); NetTcpBinding netTcpBinding = new NetTcpBinding(); endpoint.Binding = netTcpBinding; endpoint.Address = new EndpointAddress("http://somehost"); ExceptionAssert.Throws<InvalidOperationException>( "Non-Http Binding should throw", Http.SR.InvalidUriScheme(endpoint.Address.Uri.AbsoluteUri), () => behavior.Validate(endpoint)); }
public void Validate_Throws_With_Non_HttpMessageEncodingBindingElement() { HttpBehavior behavior = new HttpBehavior(); ServiceEndpoint endpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(CustomerService))); WebHttpBinding webHttpBinding = new WebHttpBinding(); endpoint.Binding = webHttpBinding; endpoint.Address = new EndpointAddress("http://somehost"); ExceptionAssert.Throws<InvalidOperationException>( "Non-HttpMessageEncodingBindingElement should throw", Http.SR.InvalidMessageEncodingBindingElement( endpoint.Address.Uri.AbsoluteUri, typeof(MessageEncodingBindingElement).Name, typeof(HttpMessageEncodingBindingElement).Name), () => behavior.Validate(endpoint)); }
public void Validate_Throws_With_Endpoint_Address_Headers() { HttpBehavior behavior = new HttpBehavior(); ServiceEndpoint endpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(CustomerService))); endpoint.Binding = new HttpBinding(); AddressHeader[] headers = new AddressHeader[] { AddressHeader.CreateAddressHeader("hello") }; endpoint.Address = new EndpointAddress(new Uri("http://somehost"), headers); ExceptionAssert.Throws<InvalidOperationException>( "Address with headers should throw", Http.SR.HttpServiceEndpointCannotHaveMessageHeaders(endpoint.Address), () => behavior.Validate(endpoint)); }
public void Validate_Throws_With_Null_Binding_On_Endpoint() { HttpBehavior behavior = new HttpBehavior(); ServiceEndpoint endpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(CustomerService))); ExceptionAssert.Throws<InvalidOperationException>( "Validate should throw for null binding", Http.SR.HttpBehaviorBindingRequired(typeof(HttpBehavior).Name), () => behavior.Validate(endpoint)); }
private static ServiceHost GetServiceHost(object serviceSingleton, HttpBehavior behavior, HttpBinding binding, Uri baseAddress) { ServiceHost host = new ServiceHost(serviceSingleton, baseAddress); ConfigureInstanceContextMode(host, InstanceContextMode.Single); AddEndpoint(host, serviceSingleton.GetType(), behavior, binding); return host; }
public void TrailingSlashMode_Redirects_By_Default() { HttpBehavior httpBehavior = new HttpBehavior(); string actualBaseAddress = null; ServiceHost actualHost = GetServiceHost(typeof(TrailingSlashModeService), httpBehavior, new HttpBinding(), out actualBaseAddress); using (HttpClient client = new HttpClient()) { client.Channel = new WebRequestChannel(); using (HttpResponseMessage actualResponse = client.Get(actualBaseAddress + "/")) { Assert.AreEqual(actualResponse.RequestMessage.RequestUri.ToString(), actualBaseAddress, "The server should have redirected to the address without the trailing slash."); } } actualHost.Close(); }
public void TrailingSlashMode_Throws_If_Set_To_Invalid_Value() { HttpBehavior behavior = new HttpBehavior(); ExceptionAssert.Throws<ArgumentOutOfRangeException>( "TrailingSlashMode should throw if invalid value set", () => behavior.TrailingSlashMode = (TrailingSlashMode)99, (e) => Assert.AreEqual("value", e.ParamName)); }
public void HttpBehavior_Ctor_Defaults() { HttpBehavior behavior = new HttpBehavior(); Assert.AreEqual(TrailingSlashMode.AutoRedirect, behavior.TrailingSlashMode, "TrailingSlashMode"); Assert.IsFalse(behavior.HelpEnabled, "HelpEnabled"); }
public void TrailingSlashMode_Dispatches_If_Set_To_Ignore() { HttpBehavior httpBehavior = new HttpBehavior(); httpBehavior.TrailingSlashMode = TrailingSlashMode.Ignore; string actualBaseAddress = null; ServiceHost actualHost = GetServiceHost(typeof(TrailingSlashModeService), httpBehavior, new HttpBinding(), out actualBaseAddress); using (HttpClient client = new HttpClient()) { client.Channel = new WebRequestChannel(); using (HttpResponseMessage actualResponse = client.Get(actualBaseAddress + "/")) { Assert.AreEqual(actualResponse.RequestMessage.RequestUri.ToString(), actualBaseAddress + "/", "The server should have dispatched without redirecting."); } } actualHost.Close(); }
public void HttpBehavior_ApplyDispatchBehavior_Throws_For_Null_EndpointDispatcher() { HttpBehavior behavior = new HttpBehavior(); ContractDescription cd = ContractDescription.GetContract(typeof(CustomerService)); ServiceEndpoint endpoint = new ServiceEndpoint(cd); EndpointDispatcher dispatcher = new EndpointDispatcher(new EndpointAddress("http://someuri"), cd.Name, cd.Namespace); ExceptionAssert.ThrowsArgumentNull("endpointDispatcher", () => behavior.ApplyDispatchBehavior(endpoint, null)); }
public void HttpBehavior_ApplyClientBehavior_Throws() { HttpBehavior behavior = new HttpBehavior(); ContractDescription cd = ContractDescription.GetContract(typeof(CustomerService)); ServiceEndpoint endpoint = new ServiceEndpoint(cd); EndpointDispatcher dispatcher = new EndpointDispatcher(new EndpointAddress("http://someuri"), cd.Name, cd.Namespace); ExceptionAssert.Throws<NotSupportedException>( "ApplyClientBehavior throws always", Http.SR.ApplyClientBehaviorNotSupportedByHttpBehavior(typeof(HttpBehavior).Name), () => ((IEndpointBehavior)behavior).ApplyClientBehavior(null, null)); }
public void Validate_Throws_With_Null_Endpoint() { HttpBehavior behavior = new HttpBehavior(); ExceptionAssert.ThrowsArgumentNull("endpoint", () => behavior.Validate(null)); }
/// <summary> /// Asserts that an <see cref="HttpRequestMessage"/> sent to the self-hosted HTTP <paramref name="serviceType"/> /// results in a response that is equivalent to the expected <see cref="HttpResponseMessage"/>. /// </summary> /// <param name="serviceType">The service type to use for the self-hosted HTTP service. Should not be <c>null</c>.</param> /// <param name="behavior">The <see cref="HttpBehavior"/> to use with the HTTP service.</param> /// <param name="binding">The <see cref="HttpBinding"/> to use with the HTTP service.</param> /// <param name="request">The <see cref="HttpRequestMessage"/> instance to send to the self-hosted HTTP service. Should not be <c>null</c>.</param> /// <param name="expectedResponse">The expected <see cref="HttpResponseMessage"/>. Should not be <c>null</c>.</param> public static void Execute(Type serviceType, HttpBehavior behavior, HttpBinding binding, HttpRequestMessage request, HttpResponseMessage expectedResponse) { Assert.IsNotNull(serviceType, "The 'serviceType' parameter should not be null."); Assert.IsNotNull(request, "The 'request' parameter should not be null."); Assert.IsNotNull(expectedResponse, "The 'expectedResponse' parameter should not be null."); ServiceHostAssert.Execute((baseAddress) => GetServiceHost(serviceType, behavior, binding, baseAddress), request, expectedResponse); }
public void Validate_Does_Not_Throw_For_HttpBinding() { HttpBehavior behavior = new HttpBehavior(); ServiceEndpoint endpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(CustomerService))); endpoint.Binding = new HttpBinding(); endpoint.Address = new EndpointAddress("http://somehost"); behavior.Validate(endpoint); }
private static void AddEndpoint(ServiceHost host, Type serviceType, HttpBehavior behavior, HttpBinding binding) { if (behavior == null) { behavior = new HttpBehavior(); behavior.HelpEnabled = true; } if (binding == null) { binding = new HttpBinding(); } ServiceEndpoint endpoint = host.AddServiceEndpoint(serviceType, binding, string.Empty); endpoint.Behaviors.Add(behavior); }
public void Validate_Throws_If_Response_Message_Headers_Present() { HttpBehavior behavior = new HttpBehavior(); ContractDescription description = ContractDescription.GetContract(typeof(CustomerService)); ServiceEndpoint endpoint = new ServiceEndpoint(description); endpoint.Binding = new HttpBinding(); endpoint.Address = new EndpointAddress("http://somehost"); OperationDescription od = description.Operations[0]; od.Messages[1].Headers.Add(new MessageHeaderDescription("someName", "someNamespace")); ExceptionAssert.Throws<InvalidOperationException>( "An operation with response message headers should throw", Http.SR.InvalidOperationWithMessageHeaders(od.Name, od.DeclaringContract.Name), () => behavior.Validate(endpoint)); }
private static ServiceHost GetServiceHost(Type serviceType, HttpBehavior behavior, HttpBinding binding, Uri baseAddress) { ServiceHost host = new ServiceHost(serviceType, baseAddress); ConfigureInstanceContextMode(host, InstanceContextMode.PerCall); AddEndpoint(host, serviceType, behavior, binding); return host; }
public void Validate_Throws_If_XmlSerializerFormat_With_Rpc_Format_Style() { HttpBehavior behavior = new HttpBehavior(); ContractDescription description = ContractDescription.GetContract(typeof(CustomerService)); ServiceEndpoint endpoint = new ServiceEndpoint(description); endpoint.Binding = new HttpBinding(); endpoint.Address = new EndpointAddress("http://somehost"); OperationDescription od = description.Operations[0]; XmlSerializerFormatAttribute attr = new XmlSerializerFormatAttribute() { Style = OperationFormatStyle.Rpc }; od.Behaviors.Add(new XmlSerializerOperationBehavior(od, attr)); ExceptionAssert.Throws<InvalidOperationException>( "XmlSerializerFormat with RPC should throw", Http.SR.InvalidXmlSerializerFormatAttribute( od.Name, od.DeclaringContract.Name, typeof(XmlSerializerFormatAttribute).Name), () => behavior.Validate(endpoint)); }
/// <summary> /// Creates the <see cref="HttpBehavior"/> instance. /// </summary> /// <returns>A new <see cref="HttpBehavior"/> instance.</returns> protected override object CreateBehavior() { HttpBehavior httpBehavior = new HttpBehavior(); if (this.IsSet(HttpConfigurationStrings.HelpEnabled)) { httpBehavior.HelpEnabled = this.HelpEnabled; } if (this.IsSet(HttpConfigurationStrings.TrailingSlashMode)) { httpBehavior.TrailingSlashMode = this.TrailingSlashMode; } if (this.IsSet(HttpConfigurationStrings.OperationHandlerFactory)) { httpBehavior.OperationHandlerFactory = HttpBehaviorElement.GetHttpOperationHandlerFactory(this.OperationHandlerFactory); } return httpBehavior; }
public void Validate_Throws_With_Non_MessageVersion_None() { HttpBehavior behavior = new HttpBehavior(); ServiceEndpoint endpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(CustomerService))); endpoint.Binding = new BasicHttpBinding(); endpoint.Address = new EndpointAddress("http://somehost"); ExceptionAssert.Throws<InvalidOperationException>( "Non-MessageVersion.None should throw", Http.SR.InvalidMessageVersion(endpoint.Address.Uri.AbsoluteUri), () => behavior.Validate(endpoint)); }