Skip to content

Commit

Permalink
Pass the requesting CoapMessage into Get/Put/Post/Delete methods in C…
Browse files Browse the repository at this point in the history
…oapResource
  • Loading branch information
NZSmartie committed Aug 23, 2017
1 parent f568528 commit 98e879f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
17 changes: 11 additions & 6 deletions CoAPNet.Tests/CoapResourceHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void TestResponse()
.Verifiable();

var mockResource = new Mock<CoapResource>("/test");
mockResource.Setup(r => r.Get()).Returns(new CoapMessage());
mockResource.Setup(r => r.Get(It.IsAny<CoapMessage>())).Returns(new CoapMessage());

var request = new CoapMessage { Code = CoapMessageCode.Get };
request.FromUri(new Uri(_baseUri, "/test"));
Expand All @@ -61,7 +61,7 @@ public void TestGetRequest()
// Arrange
var mockResource = new Mock<CoapResource>("/test");
mockResource
.Setup(r => r.Get())
.Setup(r => r.Get(It.IsAny<CoapMessage>()))
.Returns(new CoapMessage())
.Verifiable();

Expand All @@ -84,7 +84,7 @@ public void TestPostRequest()
// Arrange
var mockResource = new Mock<CoapResource>("/test");
mockResource
.Setup(r => r.Post())
.Setup(r => r.Post(It.IsAny<CoapMessage>()))
.Returns(new CoapMessage())
.Verifiable();

Expand All @@ -107,7 +107,7 @@ public void TestPutRequest()
// Arrange
var mockResource = new Mock<CoapResource>("/test");
mockResource
.Setup(r => r.Put())
.Setup(r => r.Put(It.IsAny<CoapMessage>()))
.Returns(new CoapMessage())
.Verifiable();

Expand All @@ -130,7 +130,7 @@ public void TestDeleteRequest()
// Arrange
var mockResource = new Mock<CoapResource>("/test");
mockResource
.Setup(r => r.Delete())
.Setup(r => r.Delete(It.IsAny<CoapMessage>()))
.Returns(new CoapMessage())
.Verifiable();

Expand Down Expand Up @@ -158,13 +158,18 @@ public void TestResourceMethodNotImplemented()
.Returns(Task.FromResult(0))
.Verifiable();

var mockResource = new Mock<CoapResource>("/test");
mockResource
.Setup(r => r.Get(It.IsAny<CoapMessage>()))
.Throws(new NotImplementedException());

var request = new CoapMessage { Code = CoapMessageCode.Get };
request.FromUri(new Uri(_baseUri, "/test"));

// Act
var service = new CoapResourceHandler();

service.Resources.Add(new CoapResource("/test"));
service.Resources.Add(mockResource.Object);
service.ProcessRequestAsync(new MockConnectionInformation(_endpoint.Object), request.Serialise()).Wait();

// Assert
Expand Down
6 changes: 4 additions & 2 deletions CoAPNet/CoAPNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks Condition="'$(LibraryFrameworks)'!=''">$(LibraryFrameworks)</TargetFrameworks>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>NZSmartie.CoAPNet</PackageId>
<Version>0.2.1</Version>
<Version>0.2.2</Version>
<Authors>Roman Vaughan</Authors>
<Company>NZSmartie</Company>
<Product>CoAPNet</Product>
Expand All @@ -15,7 +15,9 @@
<PackageTags>CoAP IoT sensors devices hardware network protocol</PackageTags>
<IncludeSource>True</IncludeSource>
<IncludeSymbols>True</IncludeSymbols>
<PackageReleaseNotes>v0.2.1
<PackageReleaseNotes>v0.2.2
- Pass the requesting CoapMessage into Get/Put/Post/Delete methods in CoapResource
v0.2.1
- Support message retransmission in CoapClient
v0.2.0
- Added (still Work in Progress) CoapHandler and CoapService with tests for handling incomming requests.
Expand Down
8 changes: 4 additions & 4 deletions CoAPNet/CoapHander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,16 @@ public async Task ProcessRequestAsync(ICoapConnectionInformation connection, byt
switch (message.Code)
{
case CoapMessageCode.Get:
result = resource.Get();
result = resource.Get(message);
break;
case CoapMessageCode.Post:
result = resource.Post();
result = resource.Post(message);
break;
case CoapMessageCode.Put:
result = resource.Put();
result = resource.Put(message);
break;
case CoapMessageCode.Delete:
result = resource.Delete();
result = resource.Delete(message);
break;
default:
throw new InvalidOperationException();
Expand Down
33 changes: 25 additions & 8 deletions CoAPNet/CoapResource.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace CoAPNet
{
Expand All @@ -21,24 +22,40 @@ public CoapResource(CoapResourceMetadata metadata)
Metadata = metadata;
}

public virtual CoapMessage Get()
public virtual CoapMessage Get(CoapMessage request)
{
throw new NotImplementedException();
return new CoapMessage
{
Code = CoapMessageCode.MethodNotAllowed,
Token = request.Token
};
}

public virtual CoapMessage Put()
public virtual CoapMessage Put(CoapMessage request)
{
throw new NotImplementedException();
return new CoapMessage
{
Code = CoapMessageCode.MethodNotAllowed,
Token = request.Token
};
}

public virtual CoapMessage Post()
public virtual CoapMessage Post(CoapMessage request)
{
throw new NotImplementedException();
return new CoapMessage
{
Code = CoapMessageCode.MethodNotAllowed,
Token = request.Token
};
}

public virtual CoapMessage Delete()
public virtual CoapMessage Delete(CoapMessage request)
{
throw new NotImplementedException();
return new CoapMessage
{
Code = CoapMessageCode.MethodNotAllowed,
Token = request.Token
};
}
}
}

0 comments on commit 98e879f

Please sign in to comment.