private void ExecuteGETRequest() { String payload = "nothing"; try { Request request = Request.NewGet(); request.Destination = new IPEndPoint(IPAddress.Loopback, _serverPort); request.Send(_clientEndpoint); // receive response and check Response response = request.WaitForResponse(1000); Assert.IsNotNull(response); payload = response.PayloadString; if (respond_short) { Assert.AreEqual(SHORT_GET_RESPONSE, payload); } else { Assert.AreEqual(LONG_GET_RESPONSE, payload); } } finally { Thread.Sleep(100); // Quickly wait until last ACKs arrive } }
public void TestServer() { // We do not test for block 0 because the client is currently unable to // know if the user attempts to just retrieve block 0 or if he wants to // do early block negotiation with a specific size but actually wants to // retrieve all blocks. Int32[] blockOrder = { 2, 1, 3 }; String[] expectations = { RESPONSE_PAYLOAD.Substring(32 /* until the end */), RESPONSE_PAYLOAD.Substring(16, 16), null // block is out of bounds }; for (Int32 i = 0; i < blockOrder.Length; i++) { Int32 num = blockOrder[i]; Console.WriteLine("Request block number " + num); Int32 szx = BlockOption.EncodeSZX(16); Request request = Request.NewGet(); request.URI = new Uri("coap://localhost:" + _serverPort + "/" + TARGET); request.SetBlock2(szx, false, num); request.Send(); Response response = request.WaitForResponse(1000); Assert.IsNotNull(response); Assert.AreEqual(expectations[i], response.PayloadString); Assert.IsTrue(response.HasOption(OptionType.Block2)); Assert.AreEqual(num, response.Block2.NUM); Assert.AreEqual(szx, response.Block2.SZX); } }
private void SendRequestAndExpect(String expected) { Thread.Sleep(100); Request request = Request.NewGet(); request.SetUri("coap://localhost:" + _serverPort + "/ress"); String response = request.Send().WaitForResponse(1000).PayloadString; Assert.AreEqual(expected, response); }
public void TestDiscoveryFiltering() { String expected = "</sensors/light>;if=\"sensor\";rt=\"light-lux\""; Request request = Request.NewGet(); request.SetUri("coap://localhost/.well-known/core?rt=light-lux"); DiscoveryResource discovery = new DiscoveryResource(_root); String serialized = LinkFormat.Serialize(_root, request.UriQueries); Assert.AreEqual(expected, serialized); }
private void TestSimpleNONGet(Uri uri) { Console.WriteLine("Test simple NON GET to " + uri); Request request = Request.NewGet(); request.URI = uri; request.Type = MessageType.NON; Response response = request.Send(_clientEndpoint).WaitForResponse(1000); Console.WriteLine("Client received response " + response.PayloadString + " with msg type " + response.Type); Assert.AreEqual(_currentResponseText, response.PayloadString); Assert.AreEqual(MessageType.NON, response.Type); _serverSurveillant.WaitUntilDeduplicatorShouldBeEmpty(); _serverSurveillant.AssertMapsEmpty(); _clientSurveillant.AssertMapsEmpty(); }
/// <summary> /// Send a message to the resource being observed that we want to cancel /// the observation. /// </summary> public void ProactiveCancel() { Request cancel = Request.NewGet(); // copy options, but set Observe to cancel cancel.SetOptions(Request.GetOptions()); cancel.MarkObserveCancel(); // use same Token cancel.Token = Request.Token; cancel.Destination = Request.Destination; // dispatch final response to the same message observers cancel.CopyEventHandler(Request); cancel.Send(_endpoint); // cancel old ongoing request Request.IsCancelled = true; Canceled = true; }
public void UpdateETags(byte[][] eTags) { Request newRequest = Request.NewGet(); // Copy over the options newRequest.SetOptions(Request.GetOptions()); newRequest.ClearETags(); foreach (byte[] tag in eTags) { newRequest.AddETag(tag); } newRequest.Token = Request.Token; newRequest.Destination = Request.Destination; newRequest.CopyEventHandler(Request); newRequest.Reregistering += OnReregister; newRequest.ObserveRelation = this; newRequest.Send(_endpoint); Request = newRequest; }
public void TestNameChange() { String baseUri = "coap://localhost:" + _serverPort + "/" + RES_A + "/" + RES_AA + "/"; // First check that we reach the resource String resp1 = Request.NewGet().SetUri(baseUri + NAME_1).Send().WaitForResponse(100).PayloadString; Assert.AreEqual(PAYLOAD, resp1); // Check that the child of 'first' is also reachable String resp2 = Request.NewGet().SetUri(baseUri + NAME_1 + "/" + CHILD).Send().WaitForResponse(100).PayloadString; Assert.AreEqual(CHILD_PAYLOAD, resp2); // change the name to 'second' _resource.Name = NAME_2; // Check that the resource reacts String resp3 = Request.NewGet().SetUri(baseUri + NAME_2).Send().WaitForResponse(100).PayloadString; Assert.AreEqual(PAYLOAD, resp3); // Check that the child of (now) 'second' is also reachable String resp4 = Request.NewGet().SetUri(baseUri + NAME_2 + "/" + CHILD).Send().WaitForResponse(100).PayloadString; Assert.AreEqual(CHILD_PAYLOAD, resp4); // Check that the resource is not found at the old URI StatusCode code1 = Request.NewGet().SetUri(baseUri + NAME_1).Send().WaitForResponse(100).StatusCode; Assert.AreEqual(StatusCode.NotFound, code1); // Check that the child of (now) 'second' is not reachable under 'first' StatusCode code2 = Request.NewGet().SetUri(baseUri + NAME_1 + "/" + CHILD).Send().WaitForResponse(100).StatusCode; Assert.AreEqual(StatusCode.NotFound, code2); }
/// <summary> /// Discovers remote resources. /// </summary> /// <param name="uriPath">path to be queried</param> /// <param name="query">the query to filter resources</param> /// <param name="mediaType">format to use - defaults to any</param> /// <returns>the descoverd <see cref="WebLink"/> representing remote resources, or null if no response</returns> public IEnumerable <WebLink> Discover(string uriPath, String query, int mediaType = MediaType.Undefined) { Request discover = Prepare(Request.NewGet()); discover.ClearUriPath().ClearUriQuery().UriPath = uriPath; if (!String.IsNullOrEmpty(query)) { discover.UriQuery = query; } if (mediaType != MediaType.Undefined) { discover.Accept = mediaType; } Response links = discover.Send().WaitForResponse(Timeout); if (links == null) { // if no response, return null (e.g., timeout) return(null); } switch (links.ContentType) { case MediaType.ApplicationLinkFormat: return(LinkFormat.Parse(links.PayloadString)); case MediaType.ApplicationLinkFormatCbor: return(LinkFormat.ParseCbor(links.Payload)); case MediaType.ApplicationLinkFormatJson: return(LinkFormat.ParseJson(links.PayloadString)); default: return(_EmptyLinks); } }
/// <summary> /// Set a GET request with an observer option asynchronously /// </summary> /// <param name="accept">Specify the content format to return</param> /// <param name="notify">Action to take for each notification</param> /// <param name="error">Action to take on internal error</param> /// <returns>New observe relation</returns> public CoapObserveRelation ObserveAsync(Int32 accept, Action <Response> notify = null, Action <FailReason> error = null) { return(ObserveAsync(Accept(Request.NewGet().MarkObserve(), accept), notify, error)); }
/// <summary> /// Set a GET request with an observer option /// </summary> /// <param name="notify">Action to take for each notification</param> /// <param name="error">Action to take on internal error</param> /// <returns>New observe relation</returns> public CoapObserveRelation Observe(Action <Response> notify = null, Action <FailReason> error = null) { return(Observe(Request.NewGet().MarkObserve(), notify, error)); }
/// <summary> /// Send a GET request to see if one of the ETAG values matches /// one of the currently valid contents /// </summary> /// <param name="etags">ETAG values to check as current content</param> /// <returns>CoAP Response</returns> public Response Validate(params Byte[][] etags) { return(Send(ETags(Request.NewGet(), etags))); }
/// <summary> /// Sends a GET request with the specified Accept option asynchronizely. /// </summary> /// <param name="accept">the Accept option</param> /// <param name="done">the callback when a response arrives</param> /// <param name="fail">the callback when an error occurs</param> public void GetAsync(Int32 accept, Action <Response> done = null, Action <FailReason> fail = null) { SendAsync(Accept(Request.NewGet(), accept), done, fail); }
/// <summary> /// Sends a GET request with the specified Accept option and blocks /// until the response is available. /// </summary> /// <param name="accept">the Accept option</param> /// <returns>the CoAP response</returns> public Response Get(Int32 accept) { return(Send(Accept(Request.NewGet(), accept))); }
/// <summary> /// Sends a GET request and blocks until the response is available. /// </summary> /// <returns>the CoAP response</returns> public Response Get() { return(Send(Request.NewGet())); }