Exemple #1
0
        /// <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);
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        /// <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);
        }
Exemple #4
0
        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);
        }
Exemple #5
0
        public void Behavior_Configured_Behavior_From_Host()
        {
            ConfigAssert.Execute("Microsoft.ApplicationServer.Http.CIT.Unit.ConfiguredHttpBehaviorWithServiceTest.config", () =>
            {
                HttpBehavior[] behaviors = GetBehaviorsFromServiceHost(typeof(CustomerService), new Uri("http://somehost"));
                Assert.AreEqual(1, behaviors.Length, "Expected 1 behavior");
                HttpBehavior behavior = behaviors[0];

                Assert.IsFalse(behavior.HelpEnabled, "HelpEnabled wrong");
                Assert.AreEqual(TrailingSlashMode.Ignore, behavior.TrailingSlashMode, "TrailingSlashMode wrong");
            });
        }
        public void HttpGetWithRedirect4()
        {
            // this time with HTTPBehavior
            // here both response code and location are wrong
            // make sure an exception is thrown

            HttpClient   client       = new HttpClient();
            HttpBehavior httpBehavior = new HttpBehavior();

            httpBehavior.AddStep(200);
            HttpGet      getMethod = new HttpGet(new Uri(Constants.HTTP_GET_302));
            HttpResponse response  = client.Execute(getMethod, httpBehavior);
        }
        public void HttpGetWithRedirect7()
        {
            // this time with HTTPBehavior
            // make sure we get only the first response
            // and do not make the second call
            HttpClient   client       = new HttpClient();
            HttpBehavior httpBehavior = new HttpBehavior();

            httpBehavior.AddStep(302, Constants.HTTP_REDIRECT_TARGET_1);
            HttpGet      getMethod = new HttpGet(new Uri(Constants.HTTP_GET_302));
            HttpResponse response  = client.Execute(getMethod, httpBehavior);

            Assert.AreEqual(302, response.ResponseCode);
            Assert.AreEqual(Constants.HTTP_REDIRECT_TARGET_1, response.Location);
        }
Exemple #8
0
        public void WebGet_HttpRequest_And_HttpResponse_As_OutParameters_Test()
        {
            ContractDescription contract            = ContractDescription.GetContract(typeof(HelpPageService));
            ContractDescription contractForHelpPage = HttpBehavior.GenerateClientContractDescription(contract);

            Dictionary <string, Type> inputParameters = new Dictionary <string, Type>();

            AssertValidateParameters("WebGetWithHttpRequestResponseAsOutParameters", contractForHelpPage, inputParameters, true);

            Dictionary <string, Type> outputParameters = new Dictionary <string, Type>();

            AssertValidateParameters("WebGetWithHttpRequestResponseAsOutParameters", contractForHelpPage, outputParameters, false);

            AssertValidateReturnParameter("WebGetWithHttpRequestResponseAsOutParameters", contractForHelpPage, typeof(Message));
        }
        public static void DoGetWithRedirects2()
        {
            HttpClient httpClient = new HttpClient();
            HttpGet    httpGet    = new HttpGet(new Uri("http://www.codescales.com/home"));

            HttpBehavior httpBehavior = new HttpBehavior();

            httpBehavior.AddStep(301, "http://www.codescales.com");
            httpBehavior.AddStep(200);

            HttpResponse httpResponse = httpClient.Execute(httpGet, httpBehavior);

            Console.WriteLine("Response Code: " + httpResponse.ResponseCode);
            Console.WriteLine("Response Code: " + httpResponse.Location);
            Console.WriteLine("Response Content: " + EntityUtils.ToString(httpResponse.Entity));
        }
Exemple #10
0
        public void Variables_Different_In_CaseTest()
        {
            ContractDescription contract            = ContractDescription.GetContract(typeof(HelpPageService));
            ContractDescription contractForHelpPage = HttpBehavior.GenerateClientContractDescription(contract);

            Dictionary <string, Type> inputParameters = new Dictionary <string, Type>();

            inputParameters.Add("vArIaBlE1", typeof(string));
            AssertValidateParameters("WebGetWithTemplateStringOperationAndParameterDifferentInCase", contractForHelpPage, inputParameters, true);

            Dictionary <string, Type> outputParameters = new Dictionary <string, Type>();

            AssertValidateParameters("WebGetWithTemplateStringOperationAndParameterDifferentInCase", contractForHelpPage, outputParameters, false);

            AssertValidateReturnParameter("WebGetWithTemplateStringOperationAndParameterDifferentInCase", contractForHelpPage, typeof(void));
        }
Exemple #11
0
        public void WebGet_With_Default_UriTemplate_And_HttpRequest_As_Parameter_Test()
        {
            ContractDescription contract            = ContractDescription.GetContract(typeof(HelpPageService));
            ContractDescription contractForHelpPage = HttpBehavior.GenerateClientContractDescription(contract);

            Dictionary <string, Type> inputParameters = new Dictionary <string, Type>();

            inputParameters.Add("request", typeof(HttpRequestMessage));
            AssertValidateParameters("WebGetWithHttpRequestAsParameter", contractForHelpPage, inputParameters, true);

            Dictionary <string, Type> outputParameters = new Dictionary <string, Type>();

            AssertValidateParameters("WebGetWithHttpRequestAsParameter", contractForHelpPage, outputParameters, false);

            AssertValidateReturnParameter("WebGetWithHttpRequestAsParameter", contractForHelpPage, typeof(void));
        }
Exemple #12
0
        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);
        }
Exemple #13
0
        public void UriTemplate_Variables_Test_With_WebInvoke()
        {
            ContractDescription contract            = ContractDescription.GetContract(typeof(HelpPageService));
            ContractDescription contractForHelpPage = HttpBehavior.GenerateClientContractDescription(contract);

            Dictionary <string, Type> inputParameters = new Dictionary <string, Type>();

            inputParameters.Add("VARIABLE1", typeof(string));
            inputParameters.Add("VARIABLE2", typeof(string));
            AssertValidateParameters("WebInvokeWithTemplateStringOperation", contractForHelpPage, inputParameters, true);

            Dictionary <string, Type> outputParameters = new Dictionary <string, Type>();

            AssertValidateParameters("WebInvokeWithTemplateStringOperation", contractForHelpPage, outputParameters, false);

            AssertValidateReturnParameter("WebInvokeWithTemplateStringOperation", contractForHelpPage, typeof(void));
        }
Exemple #14
0
        public void WebGet_With_Template_Parameter_Mismatch_Test()
        {
            ContractDescription contract            = ContractDescription.GetContract(typeof(HelpPageService));
            ContractDescription contractForHelpPage = HttpBehavior.GenerateClientContractDescription(contract);

            Dictionary <string, Type> inputParameters = new Dictionary <string, Type>();

            inputParameters.Add("X", typeof(string));
            inputParameters.Add("Y", typeof(string));
            AssertValidateParameters("WebGetWithTemplateAndParameterNotMatching", contractForHelpPage, inputParameters, true);

            Dictionary <string, Type> outputParameters = new Dictionary <string, Type>();

            AssertValidateParameters("WebGetWithTemplateAndParameterNotMatching", contractForHelpPage, outputParameters, false);

            AssertValidateReturnParameter("WebGetWithTemplateAndParameterNotMatching", contractForHelpPage, typeof(void));
        }
        public void HttpGetWithRedirect3()
        {
            // this time with HTTPBehavior
            // make sure we were redirected and no exception is thrown
            HttpClient   client       = new HttpClient();
            HttpBehavior httpBehavior = new HttpBehavior();

            httpBehavior.AddStep(302, Constants.HTTP_REDIRECT_TARGET_1);
            httpBehavior.AddStep(200);
            HttpGet      getMethod = new HttpGet(new Uri(Constants.HTTP_GET_302));
            HttpResponse response  = client.Execute(getMethod, httpBehavior);

            Assert.AreEqual(200, response.ResponseCode);
            Assert.AreEqual(Constants.HTTP_REDIRECT_TARGET_1, response.RequestUri.AbsoluteUri);
            string responseString = EntityUtils.ToString(response.Entity);

            Console.Write(responseString);
        }
Exemple #16
0
        /// <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);
        }