public void Test()
        {
            var requestFactory = new TestRequestFactory();
            requestFactory.RequestTimeout = TimeSpan.FromSeconds(30);
            requestFactory.PrepareResponse = r =>
                                                 {
                                                     byte[] responseBytes = Encoding.UTF8.GetBytes("{\"Id\":1}");
                                                     r.ResponseStream.Write(responseBytes, 0, responseBytes.Length);
                                                     r.ResponseStream.Seek(0, SeekOrigin.Begin);
                                                 };
            var client = new SampleClient("http://foo.com", requestFactory);

            var gate = new AutoResetEvent(false);
            TestClass response = null;
            client.BeginGetTestClass(ar =>
                                         {

                                             response = client.EndGetTestClass(ar);
                                             gate.Set();
                                         }, null);

            if (!gate.WaitOne(40000))
            {
                throw new Exception("timed out");
            }

            Assert.AreEqual(1, response.Id);

        }
        public void TestServer()
        {
            var gate = new AutoResetEvent(false);
            Exception exception = null;
            TestClass result = null;
            var client = new SampleClient(RootUrl);
            client.Serializer = Serializer;
            client.BeginGetTestClass(ar =>
            {

                try
                {

                    result = client.EndGetTestClass(ar);
                }
                catch (Exception ex)
                {
                    exception = ex;
                }
                gate.Set();
            }, null);

            if (!gate.WaitOne(60000))
            {
                throw new Exception("timed out");
            }
            if (exception != null)
            {
                throw exception;
            }

            Assert.AreEqual(1, result.Id);
        }
        public void TestServerList()
        {
            var gate = new AutoResetEvent(false);
            Exception exception = null;
            List<SampleItem> result = null;
            var client = new SampleClient(RootUrl.TrimEnd('/'))
                             {
                                 Serializer = new Serializer()
                             };
            client.BeginListService1(ar =>
            {

                try
                {

                    result = client.EndListService1(ar);
                }
                catch (Exception ex)
                {
                    exception = ex;
                }
                gate.Set();
            }, null);

            if (!gate.WaitOne(60000))
            {
                throw new Exception("timed out");
            }
            if (exception != null)
            {
                throw exception;
            }

            Assert.AreEqual(1, result[0].Id);
        }
예제 #4
0
        static void Main(string[] args)
        {

            var gate = new AutoResetEvent(false);
            Exception exception = null;
            
            var server = new CassiniDevServer();
            var path = new ContentLocator("WcfRestService1").LocateContent();
            server.StartServer(path);

            var client = new SampleClient(server.NormalizeUrl("").TrimEnd('/'));
            var recorder = new Recorder(client);
            recorder .Start();



            client.BeginListService1(ar =>
                                         {
                                             try
                                             {
                                                 List<SampleItem> result = client.EndListService1(ar);
                                                 Console.WriteLine(DateTime.Now + " " + result.Count);
                                             }
                                             catch (Exception ex)
                                             {

                                                 exception = ex;
                                             }
                                             finally
                                             {
                                                 gate.Set();
                                             }


                                         }, null);


            Wait(exception, gate);


            server.StopServer();
            server.Dispose();


            var recording = recorder.GetRequests();
            recorder.Dispose();

            var serializedRecording = client.Serializer.SerializeObject(recording);

            client.Dispose();

            File.WriteAllText("output.txt", serializedRecording);
        }
        public void CanSerializeRecursiveTypes()
        {
            var client = new SampleClient(RootUrl);
            client.Serializer = Serializer;

            var obj = new TestTypes.RecursiveClass()
                          {
                              Id = "1",
                              Nested = new RecursiveClass() { Id = "2" }
                          };
            string json = client.Serializer.SerializeObject(obj);
            RecursiveClass obj2 = client.Serializer.DeserializeObject<RecursiveClass>(json);
        }
 public void CanSerializeSelfReferencingRecursiveTypes()
 {
     var client = new SampleClient(RootUrl);
     client.Serializer = Serializer;
     var nested = new RecursiveClass { Id = "2" };
     var obj = new RecursiveClass
     {
         Id = "1",
         Nested = nested
     };
     nested.Nested = obj;
     var json = client.Serializer.SerializeObject(obj);
     var obj2 = client.Serializer.DeserializeObject<RecursiveClass>(json);
 }
        public void CanRetryFailedRequests()
        {

            var gate = new AutoResetEvent(false);

            var client = new SampleClient(RootUrl.TrimEnd('/'));
            Exception exception = null;
            client.BeginGetTestClassWithException(ar =>
            {

                try
                {
                    var result = client.EndGetTestClassWithException(ar);
                }
                catch (Exception ex)
                {

                    exception = ex;
                    
                }
                gate.Set();
            }, null);

            if (!gate.WaitOne(10000))
            {
                throw new Exception("timed out");
            }
            if(exception==null)
            {
                Assert.Fail("was expecting an exception after retrying");
            }
            var logOutput = GetLogOutput();

            Assert.IsTrue(Regex.IsMatch(logOutput, "failed 3 times"));
            Console.WriteLine(exception.ToString());
        }
        public void CanRetryFailedRequests()
        {

            var server = new CassiniDevServer();
            server.StartServer(ContentLocation);

            var gate = new AutoResetEvent(false);

            var client = new SampleClient(server.RootUrl);
            Exception exception = null;
            client.BeginGetTestClassWithException(ar =>
            {

                try
                {
                    var result = client.EndGetTestClassWithException(ar);
                }
                catch (Exception ex)
                {

                    exception = ex;

                }
                finally
                {
                    server.Dispose();
                }
                gate.Set();
            }, null);

            if (!gate.WaitOne(10000))
            {
                throw new Exception("timed out");
            }
            if (exception == null)
            {
                Assert.Fail("was expecting an exception after retrying");
            }
            Console.WriteLine(exception.ToString());
            Assert.IsTrue(Regex.IsMatch(exception.Message, "failed 3 times"));

        }
        public void aTestServer()
        {
            var server = new CassiniDevServer();
            server.StartServer(ContentLocation);

            var gate = new AutoResetEvent(false);
            Exception exception = null;
            var client = new SampleClient(server.RootUrl);
            client.BeginGetTestClass(ar =>
            {

                try
                {
                    var result = client.EndGetTestClass(ar);
                }
                catch (Exception ex)
                {
                    exception = ex;
                }
                finally
                {
                    server.Dispose();
                }
                gate.Set();
            }, null);

            if (!gate.WaitOne(10000))
            {
                throw new Exception("timed out");
            }
            if (exception != null)
            {
                throw exception;
            }
        }
        public void TestServer()
        {
            var gate = new AutoResetEvent(false);
            Exception exception = null;
            var client = new SampleClient(RootUrl.TrimEnd('/'));
            client.BeginGetTestClass(ar =>
                                         {

                                             try
                                             {
                                                 var result = client.EndGetTestClass(ar);
                                             }
                                             catch (Exception ex)
                                             {
                                                 exception = ex;
                                             }
                                             gate.Set();
                                         }, null);

            if (!gate.WaitOne(10000))
            {
                throw new Exception("timed out");
            }
            if (exception != null)
            {
                throw exception;
            }
        }
        public void TestServerCreate()
        {
            var gate = new AutoResetEvent(false);
            Exception exception = null;
            SampleItem item = new SampleItem(){StringValue = "foo"};
            SampleItem result = null;
            var client = new SampleClient(RootUrl.TrimEnd('/'))
            {
                Serializer = new Serializer()
            };
            client.BeginCreateService1(item ,ar =>
            {

                try
                {

                    result = client.EndCreateService1(ar);
                }
                catch (Exception ex)
                {
                    exception = ex;
                }
                gate.Set();
            }, null);

            if (!gate.WaitOne(60000))
            {
                throw new Exception("timed out");
            }
            if (exception != null)
            {
                throw exception;
            }

            Assert.AreEqual(1, result.Id);
        }