Comments (4)
I'm ok with that. Pls could you provide PR?
from go-coap.
Actually I found a ways to fix my issue without touching the lib. I'm a little bit busy, but for now here is my current solution for testing:
type CoapHandler func(w ResponseWriter, r *coap.Request)
func coapHandlerFunc(h CoapHandler) coap.HandlerFunc {
return func(w coap.ResponseWriter, r *coap.Request) {
h(w, r)
}
}
var mux = coap.DefaultServeMux
func InitCoapEndpoints() {
mux.HandleFunc("/up", coapHandlerFunc(coapUplinkHandler))
}
func coapUplinkHandler(w ResponseWriter, r *coap.Request) {
// ...
}
In Tests:
type ResponseWriter interface {
Write(p []byte) (n int, err error)
// WriteContext response with payload.
// If p is nil it writes response without payload.
// If p is non-nil then SetContentFormat must be called before Write otherwise Write fails.
WriteWithContext(ctx context.Context, p []byte) (n int, err error)
// SetCode for response that is send via Write call.
//
// If SetCode is not called explicitly, the first call to Write
// will trigger an implicit SetCode(Content/Changed/Deleted/Created - depends on request).
// Thus explicit calls to SetCode are mainly used to send error codes.
SetCode(code coap.COAPCode)
// SetContentFormat of payload for response that is send via Write call.
//
// If SetContentFormat is not called and Write is called with non-nil argumet
// If SetContentFormat is set but Write is called with nil argument it fails
SetContentFormat(contentFormat coap.MediaType)
//NewResponse create response with code and token, messageid against request
NewResponse(code coap.COAPCode) coap.Message
WriteMsg(msg coap.Message) error
//WriteContextMsg to client.
//If Option ContentFormat is set and Payload is not set then call will failed.
//If Option ContentFormat is not set and Payload is set then call will failed.
WriteMsgWithContext(ctx context.Context, msg coap.Message) error
}
var _ ResponseWriter = ResponseRecorder{}
// ResponseRecorder is an implementation of http.ResponseWriter that
// records its mutations for later inspection in tests.
type ResponseRecorder struct {
Msg coap.Message
}
// NewRecorder returns an initialized ResponseRecorder.
func NewRecorder() *ResponseRecorder {
return &ResponseRecorder{
Msg: coap.NewDgramMessage(coap.MessageParams{}),
}
}
func (rw ResponseRecorder) Write(buf []byte) (n int, err error) {
return rw.WriteWithContext(context.Background(), buf)
}
func (rw ResponseRecorder) WriteWithContext(ctx context.Context, buf []byte) (n int, err error) {
rw.Msg.SetPayload(buf)
return len(buf), nil
}
func (rw ResponseRecorder) SetCode(code coap.COAPCode) {
rw.Msg.SetCode(code)
}
func (rw ResponseRecorder) SetContentFormat(contentFormat coap.MediaType) {
rw.Msg.AddOption(coap.ContentFormat, contentFormat)
}
func (rw ResponseRecorder) NewResponse(code coap.COAPCode) coap.Message {
panic("implement me")
}
func (rw ResponseRecorder) WriteMsg(msg coap.Message) error {
rw.Msg = msg
return nil
}
func (rw ResponseRecorder) WriteMsgWithContext(ctx context.Context, msg coap.Message) error {
rw.Msg = msg
return nil
}
func NewRequest(code coap.COAPCode, coapType coap.COAPType, payload []byte) *coap.Request {
// TODO: Token, MessageId and Sequence???
r := &coap.Request{
Msg: coap.NewDgramMessage(coap.MessageParams{
Code: code,
Type: coapType,
Payload: payload,
}),
Client: nil,
Ctx: nil,
Sequence: 0,
}
return r
}
Part of my actual test:
rw := coaptest.NewRecorder()
uplink := UplinkPayload{
Data: "here is my payload",
Identification: env.Dev.Addr,
Number: 123,
}
uplinkCbor, err := cbor.Marshal(uplink)
assert.NoError(t, err)
r := coaptest.NewRequest(coap.POST, coap.NonConfirmable, uplinkCbor)
coapUplinkHandler(rw, r)
assert.Equal(t, coap.Created, rw.Msg.Code())
from go-coap.
Thx for share your knowledge. 👍
from go-coap.
We are releasing new version go-coap/v2. Pls look at it if it satisfy you request, If not pls reopen issue.Thx
from go-coap.
Related Issues (20)
- TCP doesn't work HOT 2
- Bidirectional examples missing HOT 2
- mux.Router can't handle root path or no path ("/") HOT 3
- Move from `pion/udp` to `pion/transport` HOT 3
- Support for Middlewares in mux Router HOT 5
- Consume `pion/dtls` connection ID support HOT 1
- Support serializing and deserializing a UDP DTLS connection HOT 2
- Implement Support for Separate Messages in Server HOT 1
- Server response with ACK to Empty CON (CoAP Ping) HOT 1
- /examples/dtls/cid/server crashes if client has wrong PSK HOT 1
- udp/server_test.go fails if first non-loopback interface is not running HOT 2
- Media type strings do not match IANA registry HOT 5
- Mux provides user with internal map HOT 1
- Ping (CON Empty Message) Notifications HOT 1
- Support for sending messages without token
- Blockwise requests should be correlated based on message options HOT 4
- ListenAndServeWithOptions doesn't correctly start UDP server HOT 1
- Pion pkg/net dependency error HOT 3
- No `NSTART` parameter in TCP transport HOT 2
- Cannot send non-confirmable responses over blockwise udp connection HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-coap.