public void Test_Post_Sync_With_Body()
        {
            var mockHttp = new MockHttpMessageHandler();

            // Setup a respond for the user api (including a wildcard in the URL)
            mockHttp.When("http://localhost/api/user/*")
            .Respond("application/json", "{'name' : 'foobar2'}");

            APGatewayBuilder <APGateway> builder = new APGatewayBuilder <APGateway> ();

            builder.Uri("http://localhost/api/user/foo");

            APGateway gateway = builder.Build();

            gateway.RestClient = new APRestClient(mockHttp);

            Dictionary <string, string> body = new Dictionary <string, string> ();

            body.Add("foo", "bar");
            var str = gateway.PostSync("foo", body);

            Assert.AreEqual("{'name' : 'foobar2'}", str);

            mockHttp.Flush();
        }
Exemple #2
0
        public void test_Handler_Client_Disabled_Caching()
        {
            APGatewayBuilder <APGateway> builder = new APGatewayBuilder <APGateway>();

            builder.Method(HTTPMethod.GET.ToString());
            builder.Uri("http://localhost/api/user/cacheMe/");
            APGateway gw = builder.Build();

            var mockHttp = new MockHttpMessageHandler();

            // Setup a respond for the user api (including a wildcard in the URL)
            mockHttp.When("http://localhost/api/user/cacheMe/*")
            .Respond("application/json", "{'name' : 'foobar'}");                     // Respond with JSON

            InMemoryCacheHandler cache = new InMemoryCacheHandler();

            gw.RestClient = new APRestClient(mockHttp, cache);
            var listener = new CacheEventListener(gw, cache);

            cache.AddListener(listener);

            var body = gw.UseCaching(false).GetSync("/cacheMe");

            Assert.AreEqual("{'name' : 'foobar'}", body);
            Assert.AreEqual(0, cache.Count());

            mockHttp.Flush();
        }
Exemple #3
0
        public void SetupGateway()
        {
            APGatewayBuilder g = new APGatewayBuilder();

            g.Uri("http://localhost");

            APGateway gw = g.Build();

            Assert.True(true);
        }
        public void Test_CreateGatewayObject()
        {
            APGatewayBuilder <APGateway> builder = new APGatewayBuilder <APGateway>();

            builder.Uri("http://localhost");

            APGateway gw = builder.Build();

            Assert.IsNotNull(gw);
            Assert.AreEqual("http://localhost", gw.Uri);
        }
        public void TestGatewaySetup()
        {
            APGatewayBuilder <APGateway> builder = new APGatewayBuilder <APGateway>();

            builder.Uri("http://localhost/api/v1");

            APGateway gw = builder.Build();

            Assert.IsNotNull(gw);

            Assert.AreEqual("http://localhost/api/v1", gw.Uri);
        }
Exemple #6
0
        public void test_Handler()
        {
            APGatewayBuilder <APGateway> builder = new APGatewayBuilder <APGateway>();

            builder.Method(HTTPMethod.GET.ToString());
            builder.Uri("http://localhost/api/user/cacheMe");
            APGateway gw = builder.Build();

            var mockHttp = new MockHttpMessageHandler();

            // Setup a respond for the user api (including a wildcard in the URL)
            mockHttp.When("http://localhost/api/user/cacheMe/*")
            .Respond("application/json", "{'name' : 'foobar'}");         // Respond with JSON

            InMemoryCacheHandler cache = new InMemoryCacheHandler();

            gw.RestClient = new APRestClient(mockHttp, cache);
            var listener = new CacheEventListener(gw, cache);

            cache.AddListener(listener);

            // Count listener
            Assert.AreEqual(1, cache.countListeners());

            var body = gw.GetSync("/cacheThis");

            Assert.AreEqual("{'name' : 'foobar'}", body);
            Assert.AreEqual(1, cache.Count());

            Assert.AreEqual("{'name' : 'foobar'}", cache.GetFromCache(uri: "http://localhost/api/user/cacheMe/cacheThis"));

            // Should reduce the number of listeners
            Assert.AreEqual(0, cache.countListeners());

            mockHttp.Flush();

            // Clear out mock
            //mockHttp.Clear ();

            body = gw.GetSync("foo");
            Assert.AreEqual("{'name' : 'foobar'}", body);
        }
Exemple #7
0
        public void test_NoCache()
        {
            APGatewayBuilder <APGateway> builder = new APGatewayBuilder <APGateway>();

            builder.Method(HTTPMethod.GET.ToString());
            builder.Uri("http://localhost/api/user/");
            APGateway gw = builder.Build();

            var mockHttp = new MockHttpMessageHandler();

            HttpResponseMessage message = new HttpResponseMessage();

            message.Headers.CacheControl         = new System.Net.Http.Headers.CacheControlHeaderValue();
            message.Headers.CacheControl.NoCache = true;
            message.Content = new StringContent("foo");

            // Setup a respond for the user api (including a wildcard in the URL)
            mockHttp.When("http://localhost/api/user/*")
            .Respond(message);         // Respond with JSON

            InMemoryCacheHandler cache = new InMemoryCacheHandler();

            gw.RestClient = new APRestClient(mockHttp, cache);

            cache.AddListener(new CacheEventListener(gw, cache));


            // Count listener
            Assert.AreEqual(1, cache.countListeners());

            var body = gw.GetSync("foo");

            Assert.AreEqual("foo", body);
            Assert.AreEqual(0, cache.Count());

            // Should reduce the number of listeners
            Assert.AreEqual(0, cache.countListeners());

            mockHttp.Flush();
        }
        public void Test_UpdateUrl()
        {
            APGatewayBuilder <APGateway> builder = new APGatewayBuilder <APGateway>();

            builder.Uri("http://localhost/api/v1/foo");

            APGateway gw = builder.Build();

            var mockHttp = new MockHttpMessageHandler();

            // Setup a respond for the user api (including a wildcard in the URL)
            mockHttp.When("http://localhost/api/v1/*")
            .Respond("application/json", "{'name' : 'foobar2'}");

            // Set a special handler for mocking
            gw.RestClient = new APRestClient(mockHttp);

            var str = gw.GetSync();

            Assert.AreEqual("{'name' : 'foobar2'}", str);

            mockHttp.Flush();
        }
        public void Test_Get_Async()
        {
            var mockHttp = new MockHttpMessageHandler();

            // Setup a respond for the user api (including a wildcard in the URL)
            mockHttp.When("http://localhost/api/user/*")
            .Respond("application/json", "{'name' : 'foobar2'}");

            APGatewayBuilder <APGateway> builder = new APGatewayBuilder <APGateway>();

            builder.Uri("http://localhost/api/user/foo");

            APGateway gateway = builder.Build();

            gateway.RestClient = new APRestClient(mockHttp);

            // Use a countdown latch for the asynchronous call
            System.Threading.CountdownEvent CountDown = new System.Threading.CountdownEvent(1);
            string result = null;

            gateway.GetAsync(url: "/foo", callback: new StringCallback()
            {
                OnSuccess = (string s) => {
                    Console.WriteLine("result: " + s);

                    result = s;
                    CountDown.Signal();
                }
            });

            CountDown.Wait();

            Assert.AreEqual("{'name' : 'foobar2'}", result);

            mockHttp.Flush();
        }