// example 1 (figure 11) public static Request Basic_get_request_causing_a_piggy_backed_response(int id = 0x7d34) { var request = new Request(CodeRegistry.Get, true) { Id = id, Uri = new Uri("coap://server/temperature") }; ResourceMock.Setup(r => r.CanGet(It.IsAny<Uri>())).Returns(true); ResourceMock.Setup(r => r.Get(It.IsAny<Uri>())).Returns(ByteConverter.GetBytes("22.3 C")); return request; }
public void SendRequest() { var uri = new Uri("coap://127.0.0.1/temperature"); var request = new Request(CodeRegistry.Get, true) { Uri = uri }; request.AddOption(new Option(OptionNumber.Token, ByteConverter.GetBytes(0xcafe))); _messageLayer.Send(request); }
public void Should_get_uri() { var request = new Request(CodeRegistry.Get, false) { RemoteAddress = "server" }; request.AddOption(new Option(OptionNumber.UriPath, ByteConverter.GetBytes("temperature"))); Assert.AreEqual("coap://server/temperature", request.Uri.ToString()); Assert.AreEqual(1, request.OptionCount); }
private void Respond(Request request, bool isPiggyBacked = false) { var type = isPiggyBacked ? MessageType.Acknowledgement : request.Type; var code = CodeRegistry.Content; var payload = new byte[0]; try { payload = _resource.Get(request.Uri); } catch (ResponseError error) { code = error.Code; } var response = new Response(type, code) { Payload = payload, RemoteAddress = request.RemoteAddress, Port = request.Port, Token = request.Token }; if (isPiggyBacked) { response.Id = request.Id; } _messageLayer.Send(response); }
// example 2 (figure 3): Blockwise GET with early negotiation public static Request Blockwise_get_with_early_negotiation(int num) { var id = 1234 + num; var uri = new Uri("coap://server/status"); var request = new Request(CodeRegistry.Get, true) { Id = id, Uri = uri }; request.AddOption(new BlockOption(OptionNumber.Block2, num, 0, BlockOption.EncodeSzx(64))); return request; }
public void Should_set_uri() { var request = new Request(CodeRegistry.Get, true) { Uri = new Uri("coap://[2001:db8::2:1]") }; Assert.AreEqual("[2001:0DB8:0000:0000:0000:0000:0002:0001]", request.RemoteAddress); Assert.AreEqual(5683, request.Port); Assert.AreEqual(0, GetCount(request, OptionNumber.UriPath)); Assert.AreEqual(0, GetCount(request, OptionNumber.UriQuery)); }
public void Should_serialize_long_option() { var request = new Request(CodeRegistry.Get, true) { Uri = new Uri("coap://server/_seventeen_chars_") }; var bytes = _serializer.Serialize(request); var bits = Util.GetBits(bytes, 4, 2); Assert.AreEqual(23, bytes.Length); Assert.AreEqual("1001111100000010", bits); }
public void Should_cancel_retransmission_if_reply_received() { var request = new Request(CodeRegistry.Get, true); _messageLayer.Send(request); var ack = new Message(MessageType.Acknowledgement) { Id = request.Id }; _messageLayer.OnReceive(ack); Assert.AreEqual(0, _messageLayer.Retransmissions.Count()); }
public void Should_set_uri_with_query() { var request = new Request(CodeRegistry.Get, true) { Uri = new Uri("coap://198.51.100.1:61616//%2F//?%2F%2F&?%26") }; Assert.AreEqual("198.51.100.1", request.RemoteAddress); Assert.AreEqual(61616, request.Port); Assert.AreEqual(4, GetCount(request, OptionNumber.UriPath)); Assert.AreEqual(2, GetCount(request, OptionNumber.UriQuery)); }
public void Should_reply_duplicated_requests_from_cache_if_confirmable() { var request = new Request(CodeRegistry.Get, true) { Id = 1 }; var ack = new Message(MessageType.Acknowledgement) { Id = 1 }; _messageLayer.OnReceive(request); _messageLayer.Send(ack); _messageLayer.OnReceive(request); _observer.Verify(h => h.OnReceive(request), Times.Once()); _lowerLayerMock.Verify(l => l.Send(ack), Times.Exactly(2)); }
public void Non_confirmable_message_should_carry_request() { var request = new Request(CodeRegistry.Get, false); Assert.IsNotNull(request); }
private bool CanBePiggyBacked(Request request) { return _resource.CanGet(request.Uri); }
private void Accept(Request request) { var ack = new Message(MessageType.Acknowledgement) { Id = request.Id }; _messageLayer.Send(ack); }
// example 7 (figure 9): Simple atomic blockwise PUT with negotiation public static Request Simple_atomic_blockwise_put_with_negotiation() { var uri = new Uri("coap://server/options"); var request = new Request(CodeRegistry.Put, true) { Id = 1234, Uri = uri, Token = ByteConverter.GetBytes(0x17), Payload = new byte[224] }; return request; }
// example 7 (figure 17) public static Request Basic_get_request_where_the_request_and_response_are_non_confirmable() { var request = new Request(CodeRegistry.Get, false) { Id = 0x7d40, Uri = new Uri("coap://server/temperature"), Token = ByteConverter.GetBytes(0x75) }; ResourceMock.Setup(r => r.Get(It.IsAny<Uri>())).Returns(ByteConverter.GetBytes("22.3 C")); return request; }
// example 5 (figure 15) public static Request Basic_get_request_causing_a_separate_response() { var request = new Request(CodeRegistry.Get, true) { Id = 0x7d38, Uri = new Uri("coap://server/temperature"), Token = ByteConverter.GetBytes(0x53) }; ResourceMock.Setup(r => r.Get(It.IsAny<Uri>())).Returns(ByteConverter.GetBytes("22.3 C")); return request; }
// example 1 (figure 2): Simple blockwise GET public static Request Simple_blockwise_get() { var uri = new Uri("coap://server/status"); var request = new Request(CodeRegistry.Get, true) { Id = 1234, Uri = uri }; return request; }
public void Should_schedule_retransmission_if_confirmable() { var request = new Request(CodeRegistry.Get, true); _messageLayer.Send(request); Assert.AreEqual(1, _messageLayer.Retransmissions.Count()); }
public void SetUp() { _request = new Request(CodeRegistry.Get, true); }
private static int GetCount(Request request, OptionNumber number) { return request.Options.Where(o => o.Number == number).Count(); }
public void Should_set_uri_with_special_characters() { var request = new Request(CodeRegistry.Get, true) { Uri = new Uri("coap://xn--18j4d.example/%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF") }; var uriPath = ByteConverter.GetString(request.GetFirstOption(OptionNumber.UriPath).Value); Assert.AreEqual("%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF", uriPath); }
public void Should_set_uri_with_path() { var request = new Request(CodeRegistry.Get, true) { Uri = new Uri("coap://example.net/.well-known/core") }; Assert.AreEqual(2, GetCount(request, OptionNumber.UriPath)); }