public void HttpClientExtensionsTest_LmsApi_GetTimestamp_InvalidMethod()
        {
            // 略過憑證檢查
            ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;

            using (HttpClient client = new HttpClientWrapper())
            {
                try
                {
                    client.PostJson<object, string>(
                        "https://lmsapi.eos.net.tw:8443/api/TimeStampService/GetTimeStamp",
                        null,
                        null);

                    Assert.Fail("Did not throw expected exception HttpServiceException.");
                }
                catch (HttpServiceException ex)
                {
                    Assert.AreEqual("The requested resource does not support http method 'POST'.", ex.Message);
                }
            }

            // 復原
            ServicePointManager.ServerCertificateValidationCallback = null;
        }
        public void ExceptionFilterTest_BusinessError()
        {
            using (var server = new HttpServerWrapper())
            using (HttpClient client = new HttpClientWrapper())
            {
                var exceptionFilter = new ExceptionFilter();
                exceptionFilter.Exception += (sender, e) =>
                {
                    e.Handled = true;
                    e.ReturnCode = "7533967";
                    e.Message = "中毒太深";
                };
                server.Configuration.Filters.Add(exceptionFilter);

                try
                {
                    client.PostJson<object, object>(
                        "/api/ApiServerTest/BusinessErrorTest",
                        null,
                        null
                    );

                    Assert.Fail("Did not throw expected exception InvocationNotAcceptableException.");
                }
                catch (InvocationNotAcceptableException ex)
                {
                    Assert.AreEqual("中毒太深", ex.Message);
                    Assert.AreEqual("7533967", ex.ErrorCode);
                }
            }
        }
        public void ModelStateFilterTest_InvalidModel()
        {
            using (var server = new HttpServerWrapper())
            using (HttpClient client = new HttpClientWrapper())
            {
                server.Configuration.Filters.Add(new ModelStateFilter());

                try
                {
                    client.PostJson<InvalidModelRequest, object>(
                        "/api/ApiServerTest/InvalidModelTest",
                        () =>
                        {
                            return new InvalidModelRequest
                            {
                                UserId = null,
                                BackyardId = null,
                                CatSubIds = null,
                            };
                        },
                        null
                    );

                    Assert.Fail("Did not throw expected exception BadInvocationException.");
                }
                catch (InvalidModelException ex)
                {
                    Assert.IsNotNull(ex.ModelState);
                    Assert.AreEqual("The UserId field is required.", ex.ModelState["request.UserId"][0]);
                    Assert.AreEqual("The BackyardId field is required.", ex.ModelState["request.BackyardId"][0]);
                    Assert.AreEqual("The CatSubIds field is required.", ex.ModelState["request.CatSubIds"][0]);
                }
            }
        }
 public void HttpTimestampHandlerTest_RouteHandler_IIS()
 {
     using (HttpClient client = new HttpClientWrapper("http://apifoundation.self.monday:9999"))
     {
         client.GetJson<JObject>(
             "/api3/!timestamp!/get",
             response =>
             {
                 Assert.IsNotNull(response);
                 Assert.IsNotNull(response["Timestamp"]);
             }
         );
     }
 }
 public void HttpTimestampHandlerTest_RouteHandler()
 {
     using (var server = new EncryptedHttpRouteWrapper())
     using (HttpClient client = new HttpClientWrapper())
     {
         client.GetJson<JObject>(
             "/api3/!timestamp!/get",
             response =>
             {
                 Assert.IsNotNull(response);
                 Assert.IsNotNull(response["Timestamp"]);
             }
         );
     }
 }
        public void HttpClientExtensionsTest_LmsApi_GetTimestamp()
        {
            // 略過憑證檢查
            ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;

            using (HttpClient client = new HttpClientWrapper())
            {
                client.GetJson<string>(
                    "https://lmsapi.eos.net.tw:8443/api/TimeStampService/GetTimeStamp",
                    response =>
                    {
                        Assert.IsNotNull(response);
                        Assert.IsInstanceOfType(response, typeof(string));
                    }
                );
            }

            // 復原
            ServicePointManager.ServerCertificateValidationCallback = null;
        }
        public void ExceptionFilterTest_BusinessError_IIS()
        {
            using (HttpClient client = new HttpClientWrapper("http://apifoundation.self.monday:9999"))
            {
                try
                {
                    client.PostJson<object, object>(
                        "/api/ApiServerTest/BusinessErrorTest",
                        null,
                        null
                    );

                    Assert.Fail("Did not throw expected exception InvocationNotAcceptableException.");
                }
                catch (InvocationNotAcceptableException ex)
                {
                    Assert.AreEqual("中毒太深", ex.Message);
                    Assert.AreEqual("7533967", ex.ErrorCode);
                }
            }
        }
 public void HttpClientExtensionsTest_ErpApi_GetUserAuthority()
 {
     using (HttpClient client = new HttpClientWrapper())
     {
         client.PostJson<object, JObject>(
             "http://erpapi.self.monday:9999/api/User/GetAuthority",
             () =>
             {
                 return new
                 {
                     BackyardId = "jacktsai",
                     Url = "/test.aspx",
                 };
             },
             response =>
             {
                 Assert.IsInstanceOfType(response, typeof(JObject));
                 Assert.AreEqual("jacktsai", response["BackyardId"]);
                 Assert.AreEqual("/test.aspx", response["Url"]);
             }
         );
     }
 }
        public void HttpClientExtensionsTest_LmsApi_InvalidCertificate()
        {
            using (HttpClient client = new HttpClientWrapper())
            {
                try
                {
                    client.PostJson<object, string>(
                        "https://lmsapi.eos.net.tw:8443/AnyController/AnyAction",
                        null,
                        null);
                }
                catch (HttpRequestException ex)
                {
                    Assert.IsNotNull(ex);
                    Assert.IsNotNull(ex.InnerException);

                    if (ex.InnerException is WebException)
                    {
                        var inner = ex.InnerException;
                        if (inner.InnerException is AuthenticationException)
                        {
                            return;
                        }
                    }
                }
            }

            Assert.Fail("Did not throw expected exception AuthenticationException.");
        }
        public void HttpClientExtensionsTest_NoServer()
        {
            using (HttpClient client = new HttpClientWrapper())
            {
                try
                {
                    client.PostJson<object, object>(
                        "http://localhost:9876/api/AnyController/AnyAction",
                        null,
                        null);
                }
                catch (HttpRequestException ex)
                {
                    Assert.IsNotNull(ex);
                    Assert.IsNotNull(ex.InnerException);

                    if (ex.InnerException is WebException)
                    {
                        Assert.AreEqual("Unable to connect to the remote server", ex.InnerException.Message);
                        return;
                    }
                }
            }

            Assert.Fail("Did not throw expected exception HttpRequestException.");
        }
        public void HttpClientExtensionsTest_NoController()
        {
            using (var server = new HttpServerWrapper())
            using (HttpClient client = new HttpClientWrapper())
            {
                try
                {
                    client.PostJson<object, object>(
                        "/api/NoController/NoAction",
                        null,
                        null);

                    Assert.Fail("Did not throw expected exception HttpServiceException.");
                }
                catch (HttpServiceException ex)
                {
                    Assert.IsNotNull(ex);
                    Assert.IsTrue(ex.Message.StartsWith("No HTTP resource was found"));
                    Assert.IsTrue(ex.MessageDetail.StartsWith("No type was found"));
                }
            }
        }
        public void ExceptionFilterTest_ProgramError_IIS()
        {
            using (HttpClient client = new HttpClientWrapper("http://apifoundation.self.monday:9999"))
            {
                try
                {
                    client.PostJson<object, object>(
                        "/api/ApiServerTest/ProgramErrorTest",
                        null,
                        null
                    );

                    Assert.Fail("Did not throw expected exception HttpServiceException.");
                }

                catch (HttpServiceException ex)
                {
                    Assert.AreEqual("模擬非商業邏輯錯誤。", ex.ExceptionMessage);
                    Assert.AreEqual(typeof(InvalidOperationException).FullName, ex.ExceptionType);
                }
            }
        }
        public void ExceptionFilterTest_ProgramError()
        {
            using (var server = new HttpServerWrapper())
            using (HttpClient client = new HttpClientWrapper())
            {
                server.Configuration.Filters.Add(new ExceptionFilter());

                try
                {
                    client.PostJson<object, object>(
                        "/api/ApiServerTest/ProgramErrorTest",
                        null,
                        null
                    );

                    Assert.Fail("Did not throw expected exception HttpServiceException.");
                }
                catch (HttpServiceException ex)
                {
                    Assert.AreEqual("模擬非商業邏輯錯誤。", ex.ExceptionMessage);
                    Assert.AreEqual(typeof(InvalidOperationException).FullName, ex.ExceptionType);
                }
            }
        }