public void DifferentWriteEncodingsTest() { Encoding[] validEncodings = new Encoding[] { Encoding.UTF8, Encoding.Unicode, Encoding.BigEndianUnicode, }; string[] charsetValues = new string[] { "utf-8", "utf-16LE", "utf-16BE" }; for (int i = 0; i < validEncodings.Length; i++) { Encoding encoding = validEncodings[i]; WebHttpBinding binding = new WebHttpBinding(); binding.WriteEncoding = encoding; WebHttpBehavior3 behavior = new WebHttpBehavior3(); string baseAddress = TestService.BaseAddress; using (ServiceHost host = new ServiceHost(typeof(TestService), new Uri(baseAddress))) { host.AddServiceEndpoint(typeof(ITestService), binding, "").Behaviors.Add(behavior); host.Open(); HttpWebRequest request = WebHttpBehavior3Test.CreateRequest("GET", baseAddress + "/EchoGet?a=1", null, null, null); HttpWebResponse resp = (HttpWebResponse)request.GetResponse(); Assert.AreEqual(HttpStatusCode.OK, resp.StatusCode); Assert.AreEqual("application/json; charset=" + charsetValues[i], resp.ContentType); Stream respStream = resp.GetResponseStream(); string responseBody = new StreamReader(respStream, encoding).ReadToEnd(); Assert.AreEqual("{\"a\":\"1\"}", responseBody); } } }
public void ApplyConfigTest() { string configFile = Assembly.GetExecutingAssembly().Location + ".config"; Console.WriteLine("Config File location: {0}", configFile); // Setting APP_CONFIG_FILE value on the AppDomain forcibly assign a config file when none was assigned before. // This can only be done once in the lifetime of the AppDomain. AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", configFile); ConfigurationManager.RefreshSection("configuration"); using (ServiceHost host = new ServiceHost(typeof(JQueryWCF2), new Uri[] { new Uri("http://localhost:55552") })) { foreach (ServiceEndpoint endpoint in host.Description.Endpoints) { Console.WriteLine("Verifying endpoint '{0}'.", endpoint.Name); WebHttpBehavior3 webHttpBehavior3 = null; foreach (IEndpointBehavior behavior in endpoint.Behaviors) { if (behavior is WebHttpBehavior3) { Console.WriteLine("Found WebHttpBehavior3 on the endpoint."); webHttpBehavior3 = (WebHttpBehavior3)endpoint.Behaviors[0]; break; } } if (webHttpBehavior3 != null) { WebHttp3Values expectedValues = null; switch (endpoint.Name) { case "implicitDefaults": case "explicitDefaults": expectedValues = new WebHttp3Values(true, WebMessageBodyStyle.Bare, WebMessageFormat.Xml, false, true); break; case "noDefaults": expectedValues = new WebHttp3Values(false, WebMessageBodyStyle.WrappedResponse, WebMessageFormat.Json, true, false); break; default: Assert.Fail("Endpoint '{0}' could not be verified as it was not expected in the test.", endpoint.Name); break; } Assert.AreEqual(webHttpBehavior3.AutomaticFormatSelectionEnabled, expectedValues.AutomaticFormatSelectionEnabledValue, "AutomaticFormatSelectionEnabled is not of the expected value."); Assert.AreEqual(webHttpBehavior3.DefaultBodyStyle, expectedValues.DefaultBodyStyleValue, "DefaultBodyStyle is not equal."); Assert.AreEqual(webHttpBehavior3.DefaultOutgoingResponseFormat, expectedValues.DefaultOutgoingResponseFormatValue, "DefaultOutgoingResponseFormat is not of the expected value."); Assert.AreEqual(webHttpBehavior3.FaultExceptionEnabled, expectedValues.FaultExceptionEnabledValue, "FaultExceptionEnabled is not of the expected value."); Assert.AreEqual(webHttpBehavior3.HelpEnabled, expectedValues.HelpEnabledValue, "HelpEnabled is not of the expected value."); } else { Assert.Fail("No WebHttpBehavior3 was found on the endpoint."); } } } }
void TestValidationError <TService, TInterface, TExpectedException>(Action <WebHttpBehavior3> behaviorChanges) where TExpectedException : Exception { string baseAddress = "http://localhost:8000/Service"; ServiceHost host = new ServiceHost(typeof(TService), new Uri(baseAddress)); WebHttpBehavior3 behavior = new WebHttpBehavior3(); if (behaviorChanges != null) { behaviorChanges(behavior); } host.AddServiceEndpoint(typeof(TInterface), new WebHttpBinding(), "").Behaviors.Add(behavior); try { host.Open(); Assert.Fail("Error, expected exception {0}, got none", typeof(TExpectedException).FullName); } catch (TExpectedException e) { Console.WriteLine("For interface {0}, caught: {1}", typeof(TInterface).Name, e); } finally { host.Abort(); } }
public void WebFaultExceptionOfJsonValue() { WebHttpBinding binding = new WebHttpBinding(); WebHttpBehavior3 behavior = new WebHttpBehavior3(); behavior.FaultExceptionEnabled = false; JsonValue[] details = new JsonValue[] { new JsonPrimitive(123), new JsonPrimitive("a string value"), new JsonArray { 123.456, null, false }, new JsonObject { { "a", 123 }, { "b", new JsonArray("a", "b", "c") }, { "c", new JsonObject() } }, }; HttpStatusCode[] statusCodes = new HttpStatusCode[] { HttpStatusCode.RequestEntityTooLarge, HttpStatusCode.RequestedRangeNotSatisfiable, HttpStatusCode.NotImplemented }; foreach (HttpStatusCode statusCode in statusCodes) { foreach (JsonValue detail in details) { foreach (bool useExactJsonValueType in new bool[] { false, true }) { string requestBody = detail.ToString(); string url = WebHttpBehavior3Tests.Endpoint + "/ThrowWebFaultExceptionOfJsonValue?statusCode=" + (int)statusCode + "&useExactJsonValueType=" + useExactJsonValueType.ToString().ToLowerInvariant(); Test(binding, behavior, "POST", url, "text/json", requestBody, statusCode, WebHttpBehavior3Tests.ApplicationJsonContentTypeWithCharset, requestBody); } } } }
public void FormsUrlEncodedMaxDepthQuotaTest() { string errorContentType = "text/html"; int anyMaxDepth = 5; WebHttpBinding binding = new WebHttpBinding(); binding.ReaderQuotas.MaxDepth = anyMaxDepth; WebHttpBehavior3 behavior = new WebHttpBehavior3(); string formsEncodedWithinQuota = "a[b][c][d]=e".Replace("[", "%5B").Replace("]", "%5D"); string formsEncodedExceededQuota = "a[b][c][d][e]=g".Replace("[", "%5B").Replace("]", "%5D"); string jsonWithinQuota = "{\"a\":{\"b\":{\"c\":{\"d\":\"e\"}}}}"; string jsonExceededQuota = "{\"a\":{\"b\":{\"c\":{\"d\":{\"e\":\"f\"}}}}}"; string baseAddress = TestService.BaseAddress; using (ServiceHost host = new ServiceHost(typeof(TestService), new Uri(baseAddress))) { host.AddServiceEndpoint(typeof(ITestService), binding, "").Behaviors.Add(behavior); host.Open(); this.TestSendRequest("POST", baseAddress + "/EchoPost", "application/json", jsonWithinQuota, HttpStatusCode.OK, WebHttpBehavior3Test.ApplicationJsonContentType, jsonWithinQuota); this.TestSendRequest("POST", baseAddress + "/EchoPost", "application/x-www-form-urlencoded", formsEncodedWithinQuota, HttpStatusCode.OK, WebHttpBehavior3Test.ApplicationJsonContentType, jsonWithinQuota); this.TestSendRequest("POST", baseAddress + "/EchoPost", "application/json", jsonExceededQuota, HttpStatusCode.BadRequest, errorContentType, null); this.TestSendRequest("POST", baseAddress + "/EchoPost", "application/x-www-form-urlencoded", formsEncodedExceededQuota, HttpStatusCode.BadRequest, errorContentType, null); } }
public void HelpEnabledTest() { WebHttpBehavior3 target = new WebHttpBehavior3(); Assert.IsTrue(target.HelpEnabled); target.HelpEnabled = false; Assert.IsFalse(target.HelpEnabled); }
public void AutomaticFormatSelectionEnabledTest() { WebHttpBehavior3 target = new WebHttpBehavior3(); Assert.IsTrue(target.AutomaticFormatSelectionEnabled); target.AutomaticFormatSelectionEnabled = false; Assert.IsFalse(target.AutomaticFormatSelectionEnabled); }
public void FaultExceptionEnabledTest() { WebHttpBehavior3 target = new WebHttpBehavior3(); Assert.IsFalse(target.FaultExceptionEnabled); target.FaultExceptionEnabled = true; Assert.IsTrue(target.FaultExceptionEnabled); }
public void WebFaultExceptionOfJsonValueWithHttpResponseMessageProperty() { WebHttpBinding binding = new WebHttpBinding(); WebHttpBehavior3 behavior = new WebHttpBehavior3(); behavior.FaultExceptionEnabled = false; HttpStatusCode statusCode = HttpStatusCode.Conflict; Test(binding, behavior, "POST", WebHttpBehavior3Tests.Endpoint + "/ThrowWebFaultExceptionOfJsonValueChangingContentType?statusCode=" + (int)statusCode, "text/json", "123", statusCode, WebHttpBehavior3Tests.ApplicationJsonContentTypeWithCharset, "123"); }
public void DefaultPropertiesTest() { WebHttpBehavior3 target = new WebHttpBehavior3(); Assert.AreEqual(true, target.AutomaticFormatSelectionEnabled); Assert.AreEqual(WebMessageBodyStyle.Bare, target.DefaultBodyStyle); Assert.AreEqual(WebMessageFormat.Xml, target.DefaultOutgoingRequestFormat); Assert.AreEqual(WebMessageFormat.Xml, target.DefaultOutgoingResponseFormat); Assert.AreEqual(false, target.FaultExceptionEnabled); Assert.AreEqual(true, target.HelpEnabled); }
public void NormalWebFaultException() { WebHttpBinding binding = new WebHttpBinding(); WebHttpBehavior3 behavior = new WebHttpBehavior3(); behavior.FaultExceptionEnabled = false; HttpStatusCode[] statusCodes = new HttpStatusCode[] { HttpStatusCode.BadRequest, HttpStatusCode.InternalServerError, HttpStatusCode.PreconditionFailed }; foreach (HttpStatusCode statusCode in statusCodes) { Test(binding, behavior, "GET", WebHttpBehavior3Tests.Endpoint + "/ThrowWebFaultException?statusCode=" + (int)statusCode, null, null, statusCode, null, null); } }
public void TestAutomaticFormatSelectionFromAcceptHeader() { int x, y; string expectedJson, expectedXml; WebHttpBehavior3Tests.CreateInputValuesAndExpectedResults(3, out x, out y, out expectedJson, out expectedXml); string xmlInput = String.Format(CultureInfo.InvariantCulture, "<AddJsonOrXml xmlns=\"http://tempuri.org/\"><x>{0}</x><y>{1}</y></AddJsonOrXml>", x, y); string jsonInput = String.Format(CultureInfo.InvariantCulture, "{{\"x\":{0}, \"y\":{1}}}", x, y); WebHttpBinding binding = new WebHttpBinding(); WebHttpBehavior3 behavior = new WebHttpBehavior3(); behavior.AutomaticFormatSelectionEnabled = true; using (ServiceHost host = new ServiceHost(typeof(JQueryWCFService), new Uri(WebHttpBehavior3Tests.Endpoint))) { host.AddServiceEndpoint(typeof(IJQueryWCF), binding, "").Behaviors.Add(behavior); try { host.Open(); foreach (bool useJsonInput in new bool[] { false, true }) { foreach (bool useAcceptJson in new bool[] { false, true }) { string input = useJsonInput ? jsonInput : xmlInput; string requestContentType = useJsonInput ? "application/json" : "application/xml"; string expectedResponseContentType = useAcceptJson ? WebHttpBehavior3Tests.ApplicationJsonContentTypeWithCharset : "application/xml; charset=utf-8"; string expectedResponseBody = useAcceptJson ? expectedJson : expectedXml; string acceptHeader = useAcceptJson ? "application/json" : "application/xml"; Console.WriteLine("Sending {0} request with Accept: {1}", requestContentType, acceptHeader); Dictionary <string, string> headers = new Dictionary <string, string>(); headers.Add("Accept", acceptHeader); string address = WebHttpBehavior3Tests.Endpoint + "/AddJsonOrXml"; HttpWebResponse httpResponse = WebHttpBehavior3Tests.SendRequest("POST", address, requestContentType, input, Encoding.UTF8, headers); WebHttpBehavior3Tests.ValidateHttpResponse(httpResponse, HttpStatusCode.OK, expectedResponseContentType, expectedResponseBody); } } } catch { host.Abort(); throw; } } }
static void Test <TService, IContract>(WebHttpBinding binding, WebHttpBehavior3 behavior, string method, string address, string contentType, string body, HttpStatusCode expectedHttpStatus, string expectedResponseContentType, string expectedResponseBody) { using (ServiceHost host = new ServiceHost(typeof(TService), new Uri(WebHttpBehavior3Tests.Endpoint))) { host.AddServiceEndpoint(typeof(IContract), binding, "").Behaviors.Add(behavior); try { host.Open(); WebHttpBehavior3Tests.Test(method, address, contentType, body, expectedHttpStatus, expectedResponseContentType, expectedResponseBody); } catch { host.Abort(); throw; } } }
public void WebFaultExceptionOfT() { int seed = MethodBase.GetCurrentMethod().Name.GetHashCode(); Random rndGen = new Random(seed); WebHttpBinding binding = new WebHttpBinding(); WebHttpBehavior3 behavior = new WebHttpBehavior3(); behavior.FaultExceptionEnabled = false; HttpStatusCode[] statusCodes = new HttpStatusCode[] { HttpStatusCode.Unauthorized, HttpStatusCode.ServiceUnavailable, HttpStatusCode.RequestTimeout }; foreach (HttpStatusCode statusCode in statusCodes) { string detail = PrimitiveCreator.CreateRandomString(rndGen, 30, "abcdefghijklmnopqrstuvwxyz"); string requestBody = "{\"statusCode\":" + (int)statusCode + ",\"detail\":\"" + detail + "\"}"; Test(binding, behavior, "POST", WebHttpBehavior3Tests.Endpoint + "/ThrowWebFaultExceptionOfT", "text/json", requestBody, statusCode, WebHttpBehavior3Tests.ApplicationJsonContentTypeWithCharset, "\"" + detail + "\""); } }
static void JsonpTests <TService, IContract>(string relativeAddress, HttpStatusCode expectedHttpStatus, string expectedResponseContentType, string expectedResponseBody) { WebHttpBinding binding = new WebHttpBinding(); binding.CrossDomainScriptAccessEnabled = true; WebHttpBehavior3 behavior = new WebHttpBehavior3(); Test <TService, IContract>( binding, behavior, "GET", WebHttpBehavior3Tests.Endpoint + relativeAddress, null, null, expectedHttpStatus, expectedResponseContentType, expectedResponseBody); }
public void TestAutomaticFormatSelectionFromInput() { int x, y; string expectedJson, expectedXml; WebHttpBehavior3Tests.CreateInputValuesAndExpectedResults(3, out x, out y, out expectedJson, out expectedXml); string xmlInput = String.Format(CultureInfo.InvariantCulture, "<AddJsonOrXml xmlns=\"http://tempuri.org/\"><x>{0}</x><y>{1}</y></AddJsonOrXml>", x, y); string jsonInput = String.Format(CultureInfo.InvariantCulture, "{{\"x\":{0}, \"y\":{1}}}", x, y); WebHttpBinding binding = new WebHttpBinding(); WebHttpBehavior3 behavior = new WebHttpBehavior3(); behavior.AutomaticFormatSelectionEnabled = true; Test <JQueryWCFService, IJQueryWCF>(binding, behavior, "POST", WebHttpBehavior3Tests.Endpoint + "/AddJsonOrXml", "application/xml; charset=utf-8", xmlInput, HttpStatusCode.OK, "application/xml; charset=utf-8", expectedXml); Test <JQueryWCFService, IJQueryWCF>(binding, behavior, "POST", WebHttpBehavior3Tests.Endpoint + "/AddJsonOrXml", "application/json", jsonInput, HttpStatusCode.OK, WebHttpBehavior3Tests.ApplicationJsonContentTypeWithCharset, expectedJson); }
public void JsonpOnPost() { WebHttpBinding binding = new WebHttpBinding(); binding.CrossDomainScriptAccessEnabled = true; WebHttpBehavior3 behavior = new WebHttpBehavior3(); Test( binding, behavior, "POST", WebHttpBehavior3Tests.Endpoint + "/EchoPost?callback=MyFunc", WebHttpBehavior3Tests.FormUrlEncodedContentType, "a=1&b=2", HttpStatusCode.OK, JsonpContentType, "MyFunc({\"a\":\"1\",\"b\":\"2\"});"); }
public void JsonValueReturnContentTypeIsJsonWithAllDefaultResponseFormats() { WebHttpBinding binding = new WebHttpBinding(); WebMessageFormat[] defaultResponseFormats = new WebMessageFormat[] { WebMessageFormat.Json, WebMessageFormat.Xml }; foreach (WebMessageFormat format in defaultResponseFormats) { WebHttpBehavior3 behavior = new WebHttpBehavior3 { DefaultOutgoingResponseFormat = format }; Test( binding, behavior, "POST", WebHttpBehavior3Tests.Endpoint + "/EchoPost", WebHttpBehavior3Tests.FormUrlEncodedContentType, "a=1&b=2", HttpStatusCode.OK, WebHttpBehavior3Tests.ApplicationJsonContentTypeWithCharset, "{\"a\":\"1\",\"b\":\"2\"}"); Test( binding, behavior, "POST", WebHttpBehavior3Tests.Endpoint + "/EchoPostWithWebInvoke", WebHttpBehavior3Tests.FormUrlEncodedContentType, "a=1&b=2", HttpStatusCode.OK, WebHttpBehavior3Tests.ApplicationJsonContentTypeWithCharset, "{\"a\":\"1\",\"b\":\"2\"}"); Test( binding, behavior, "GET", WebHttpBehavior3Tests.Endpoint + "/EchoGet?a=1&b=2", null, null, HttpStatusCode.OK, WebHttpBehavior3Tests.ApplicationJsonContentTypeWithCharset, "{\"a\":\"1\",\"b\":\"2\"}"); } }
static void Test(WebHttpBinding binding, WebHttpBehavior3 behavior, string method, string address, string contentType, string body, HttpStatusCode expectedHttpStatus, string expectedResponseContentType, string expectedResponseBody) { Test <Service, ITest>(binding, behavior, method, address, contentType, body, expectedHttpStatus, expectedResponseContentType, expectedResponseBody); }