Example #1
0
        public void Ocoap_Get()
        {
            CoapClient client = new CoapClient($"coap://localhost:{_serverPort}/abc")
            {
                OscoapContext = SecurityContext.DeriveContext(_Secret, _ClientId, _ServerId)
            };
            Response r = client.Get();

            Assert.AreEqual("/abc", r.PayloadString);
        }
Example #2
0
        public void TestCoapClient_Discover()
        {
            Uri        uri    = new Uri($"coap://localhost:{_serverPort}/");
            CoapClient client = new CoapClient(uri);

            IEnumerable <WebLink> resources = client.Discover();

            Assert.AreEqual(4, resources.Count());

            resources = client.Discover(MediaType.ApplicationLinkFormat);
            Assert.AreEqual(4, resources.Count());
        }
        private void TestSimpleGet(Uri uri)
        {
            Console.WriteLine("Test simple GET to " + uri);

            CoapClient client = new CoapClient(uri);

            client.EndPoint = _clientEndpoint;

            Response response = client.Get();

            Console.WriteLine("Client received response " + response.PayloadString);
            Assert.AreEqual(_currentResponseText, response.PayloadString);

            _serverSurveillant.WaitUntilDeduplicatorShouldBeEmpty();
            _serverSurveillant.AssertMapsEmpty();
            _clientSurveillant.AssertMapsEmpty();
        }
Example #4
0
        public void TestCoapClient_UriPath()
        {
            Uri        uri    = new Uri("coap://localhost:" + _serverPort + "/");
            CoapClient client = new CoapClient(uri);

            IEnumerable <WebLink> resources = client.Discover();

            Assert.AreEqual(4, resources.Count());

            client.UriPath = "abc";

            Response r = client.Get();

            Assert.AreEqual("/abc", r.PayloadString);

            client.UriPath = "/abc/def";
            r = client.Get();
            Assert.AreEqual("/abc/def", r.PayloadString);
        }
        private void TestBlockwise(Uri uri)
        {
            Console.WriteLine("Test blockwise POST to " + uri);

            CoapClient client = new CoapClient(uri);

            client.EndPoint = _clientEndpoint;

            String ten = "123456789.";

            _currentRequestText = ten + ten + ten;
            Response response = client.Post(_currentRequestText, MediaType.TextPlain);

            Console.WriteLine("Client received response " + response.PayloadString);
            Assert.AreEqual(_currentResponseText, response.PayloadString);

            _serverSurveillant.WaitUntilDeduplicatorShouldBeEmpty();
            _serverSurveillant.AssertMapsEmpty();
            _clientSurveillant.AssertMapsEmpty();
        }
        private void TestObserve(Uri uri)
        {
            Console.WriteLine("Test observe relation with a reactive cancelation to " + uri);

            ManualResetEvent mre = new ManualResetEvent(false);

            CoapClient client = new CoapClient(uri);

            client.EndPoint = _clientEndpoint;

            Int32 notificationCounter    = 0;
            CoapObserveRelation relation = null;

            relation = client.Observe(response =>
            {
                notificationCounter++;
                Console.WriteLine("Client received notification " + notificationCounter + ": " + response.PayloadString);

                if (notificationCounter == HOW_MANY_NOTIFICATION_WE_WAIT_FOR)
                {
                    Console.WriteLine("Client forgets observe relation to " + uri);
                    SpinWait.SpinUntil(() => relation != null);
                    relation.ProactiveCancel();
                }
                else if (notificationCounter == HOW_MANY_NOTIFICATION_WE_WAIT_FOR + 1)
                {
                    mre.Set();
                }
            }, reason => Assert.Fail(reason.ToString()));

            // Wait until we have received all the notifications and canceled the relation
            Thread.Sleep(HOW_MANY_NOTIFICATION_WE_WAIT_FOR * OBS_NOTIFICATION_INTERVALL + 3000);

            Boolean success = mre.WaitOne(100);

            Assert.IsTrue(success, "Client has not received all expected responses");

            _serverSurveillant.WaitUntilDeduplicatorShouldBeEmpty();
            _serverSurveillant.AssertMapsEmpty();
            _clientSurveillant.AssertMapsEmpty();
        }
Example #7
0
        public void TestSynchronousCall()
        {
            _notifications = 0;

            Uri        uri    = new Uri("coap://localhost:" + _serverPort + "/" + TARGET);
            CoapClient client = new CoapClient(uri);

            // Check that we get the right content when calling get()
            String resp1 = client.Get().ResponseText;

            Assert.AreEqual(CONTENT_1, resp1);

            String resp2 = client.Get().ResponseText;

            Assert.AreEqual(CONTENT_1, resp2);

            // Change the content to "two" and check
            String resp3 = client.Post(CONTENT_2).ResponseText;

            Assert.AreEqual(CONTENT_1, resp3);

            String resp4 = client.Get().ResponseText;

            Assert.AreEqual(CONTENT_2, resp4);

            // Observe the resource
            _expected = CONTENT_2;
            CoapObserveRelation obs1 = client.Observe(response =>
            {
                Interlocked.Increment(ref _notifications);
                String payload = response.ResponseText;
                Assert.AreEqual(_expected, payload);
                Assert.IsTrue(response.HasOption(OptionType.Observe));
            }, Fail);

            Assert.IsFalse(obs1.Canceled);

            Thread.Sleep(100);
            _resource.Changed();
            Thread.Sleep(100);
            _resource.Changed();
            Thread.Sleep(100);
            _resource.Changed();

            Thread.Sleep(100);
            _expected = CONTENT_3;
            String resp5 = client.Post(CONTENT_3).ResponseText;

            Assert.AreEqual(CONTENT_2, resp5);

            // Try a put and receive a METHOD_NOT_ALLOWED
            StatusCode code6 = client.Put(CONTENT_4).StatusCode;

            Assert.AreEqual(StatusCode.MethodNotAllowed, code6);

            // Cancel observe relation of obs1 and check that it does no longer receive notifications
            Thread.Sleep(100);
            _expected = null; // The next notification would now cause a failure
            obs1.ReactiveCancel();
            Thread.Sleep(100);
            _resource.Changed();

            // Make another post
            Thread.Sleep(100);
            String resp7 = client.Post(CONTENT_4).ResponseText;

            Assert.AreEqual(CONTENT_3, resp7);

            // Try to use the builder and add a query
            UriBuilder ub = new UriBuilder("coap", "localhost", _serverPort, TARGET);

            ub.Query = QUERY_UPPER_CASE;

            String resp8 = new CoapClient(ub.Uri).Get().ResponseText;

            Assert.AreEqual(CONTENT_4.ToUpper(), resp8);

            // Check that we indeed received 5 notifications
            // 1 from origin GET request, 3 x from changed(), 1 from post()
            Thread.Sleep(100);
            Assert.AreEqual(5, _notifications);
            Assert.IsFalse(_failed);
        }
Example #8
0
        public void TestSynchronousCall()
        {
            int            notifications = 0;
            AutoResetEvent syncEvent     = new AutoResetEvent(false);

            Uri        uri    = new Uri("coap://localhost:" + _serverPort + "/" + TARGET);
            CoapClient client = new CoapClient(uri);

            // Check that we get the right content when calling get()
            string resp1 = client.Get().ResponseText;

            Assert.AreEqual(CONTENT_1, resp1);

            string resp2 = client.Get().ResponseText;

            Assert.AreEqual(CONTENT_1, resp2);

            // Change the content to "two" and check
            string resp3 = client.Post(CONTENT_2).ResponseText;

            Assert.AreEqual(CONTENT_1, resp3);

            string resp4 = client.Get().ResponseText;

            Assert.AreEqual(CONTENT_2, resp4);
            Thread.Sleep(100);

            // Observe the resource
            _expected = CONTENT_2;

            object         blockLock    = new object();
            AutoResetEvent trigger      = new AutoResetEvent(false);
            Response       lastResponse = null;

            CoapObserveRelation obs1 = client.Observe(response => {
                lastResponse = response;
                trigger.Set();
            }, Fail);

            Assert.IsFalse(obs1.Canceled);

            Assert.IsTrue(trigger.WaitOne(100));
            Assert.AreEqual(_expected, lastResponse.ResponseText);
            Assert.IsTrue(lastResponse.HasOption(OptionType.Observe));

            for (int i = 0; i < 5; i++)
            {
                _resource.Changed();
                Assert.IsTrue(trigger.WaitOne(100));
                Assert.AreEqual(_expected, lastResponse.ResponseText);
                Assert.IsTrue(lastResponse.HasOption(OptionType.Observe));
            }

            _expected = CONTENT_3;
            string resp5 = client.Post(CONTENT_3).ResponseText;

            Assert.AreEqual(CONTENT_2, resp5);

            Assert.IsTrue(trigger.WaitOne(100));
            Assert.AreEqual(_expected, lastResponse.ResponseText);
            Assert.IsTrue(lastResponse.HasOption(OptionType.Observe));

            // Try a put and receive a METHOD_NOT_ALLOWED
            StatusCode code6 = client.Put(CONTENT_4).StatusCode;

            Assert.AreEqual(StatusCode.MethodNotAllowed, code6);
            Assert.IsFalse(trigger.WaitOne(100));

            // Cancel observe relation of obs1 and check that it does no longer receive notifications

            obs1.ReactiveCancel();
            Thread.Sleep(100);

            _resource.Changed();
            Assert.IsFalse(trigger.WaitOne(100));

            // Make another post
            Thread.Sleep(100);
            string resp7 = client.Post(CONTENT_4).ResponseText;

            Assert.AreEqual(CONTENT_3, resp7);

            // Try to use the builder and add a query
            UriBuilder ub = new UriBuilder("coap", "localhost", _serverPort, TARGET);

            ub.Query = QUERY_UPPER_CASE;

            string resp8 = new CoapClient(ub.Uri).Get().ResponseText;

            Assert.AreEqual(CONTENT_4.ToUpper(), resp8);

            // Check that we indeed received 5 notifications
            // 1 from origin GET request, 3 x from changed(), 1 from post()
            Thread.Sleep(100);
            Assert.IsFalse(_failed);
        }
Example #9
0
        public void TestAsynchronousCall()
        {
            int notifications = 0;

            Uri        uri    = new Uri("coap://localhost:" + _serverPort + "/" + TARGET);
            CoapClient client = new CoapClient(uri);

            client.Error += (o, e) => Fail(e.Reason);

            // Check that we get the right content when calling get()
            client.GetAsync(response => Assert.AreEqual(CONTENT_1, response.ResponseText));
            Thread.Sleep(100);

            client.GetAsync(response => Assert.AreEqual(CONTENT_1, response.ResponseText));
            Thread.Sleep(100);

            // Change the content to "two" and check
            client.PostAsync(CONTENT_2, response => Assert.AreEqual(CONTENT_1, response.ResponseText));
            Thread.Sleep(100);

            client.GetAsync(response => Assert.AreEqual(CONTENT_2, response.ResponseText));
            Thread.Sleep(100);

            // Observe the resource
            string expected          = CONTENT_2;
            int    actualTest        = 0;
            int    notifyTest        = 0;
            CoapObserveRelation obs1 = client.ObserveAsync(response =>
            {
                Interlocked.Increment(ref _notifications);
                string payload = response.ResponseText;
                Assert.AreEqual(_expected, payload);
                Assert.IsTrue(response.HasOption(OptionType.Observe));
            }
                                                           );

            Thread.Sleep(100);
            _resource.Changed();
            Thread.Sleep(100);
            _resource.Changed();
            Thread.Sleep(100);
            _resource.Changed();

            Thread.Sleep(100);
            expected = CONTENT_3;
            client.PostAsync(CONTENT_3, response => Assert.AreEqual(CONTENT_2, response.ResponseText));
            Thread.Sleep(100);

            // Try a put and receive a MethodNotAllowed
            client.PutAsync(CONTENT_4, response => Assert.AreEqual(StatusCode.MethodNotAllowed, response.StatusCode));

            // Cancel observe relation of obs1 and check that it does no longer receive notifications
            Thread.Sleep(100);
            expected = null; // The next notification would now cause a failure
            obs1.ReactiveCancel();
            Thread.Sleep(100);
            _resource.Changed();

            // Make another post
            Thread.Sleep(100);
            client.PostAsync(CONTENT_4, response => Assert.AreEqual(CONTENT_3, response.ResponseText));
            Thread.Sleep(100);

            UriBuilder ub = new UriBuilder("coap", "localhost", _serverPort, TARGET)
            {
                Query = QUERY_UPPER_CASE
            };

            // Try to use the builder and add a query
            new CoapClient(ub.Uri).GetAsync(response => Assert.AreEqual(CONTENT_4.ToUpper(), response.ResponseText));

            // Check that we indeed received 5 notifications
            // 1 from origin GET request, 3 x from changed(), 1 from post()
            Thread.Sleep(100);
            Assert.AreEqual(0, actualTest);
            Assert.AreEqual(0, notifyTest);
            Assert.IsFalse(_failed);
        }