Giter VIP home page Giter VIP logo

Comments (4)

jkralik avatar jkralik commented on May 27, 2024

I'm ok with that. Pls could you provide PR?

from go-coap.

niondir avatar niondir commented on May 27, 2024

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.

jkralik avatar jkralik commented on May 27, 2024

Thx for share your knowledge. 👍

from go-coap.

jkralik avatar jkralik commented on May 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.