77 lines
2.1 KiB
Go
77 lines
2.1 KiB
Go
package rpc
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestEncodeNewParams(t *testing.T) {
|
|
msg := &Msg{}
|
|
msg.Time = 1 // set time to non-zero for a static time in the marshalled result
|
|
msg.Params = NewParams(map[string]interface{}{
|
|
"a dirty mind": "is a joy forever",
|
|
"this is dirty": "but possible!"})
|
|
require.False(t, msg.Params.IsEmpty())
|
|
|
|
// marshall the message with the deferredDecoder with a raw message
|
|
ret, err := json.Marshal(msg)
|
|
require.Nil(t, err)
|
|
|
|
outcome := `{"dinetrpc":1,"time":1,"params":{"a dirty mind":"is a joy forever",
|
|
"this is dirty":"but possible!"}}`
|
|
require.JSONEq(t, outcome, string(ret))
|
|
}
|
|
|
|
func TestEncodeCustomParams(t *testing.T) {
|
|
msg := &Msg{
|
|
Time: 10,
|
|
Params: &ConfigParam{UID: 10},
|
|
}
|
|
require.False(t, msg.Params.IsEmpty())
|
|
|
|
var bla int
|
|
require.Equal(t, ErrMsgNotSupported, msg.Params.Unmarshal(&bla))
|
|
|
|
ret, err := json.Marshal(msg)
|
|
require.Nil(t, err)
|
|
|
|
outcome := `{"dinetrpc":1,"time":10,"params":{"uid": 10}}`
|
|
require.JSONEq(t, outcome, string(ret))
|
|
}
|
|
|
|
func TestEncodeNewResult(t *testing.T) {
|
|
msg := &Msg{}
|
|
msg.Result = NewResult(map[string]interface{}{
|
|
"a dirty mind": "is a joy forever",
|
|
"this is dirty": "but possible!"})
|
|
msg.Time = 1 // set time to non-zero for a static time in the marshalled result
|
|
require.False(t, msg.Result.IsEmpty())
|
|
|
|
ret, err := json.Marshal(msg)
|
|
require.Nil(t, err)
|
|
|
|
outcome := `{"dinetrpc":1,"time":1,"result":{"a dirty mind":"is a joy forever",
|
|
"this is dirty":"but possible!"}}`
|
|
require.JSONEq(t, outcome, string(ret))
|
|
}
|
|
|
|
func TestEncodeCustomResult(t *testing.T) {
|
|
msg := &Msg{}
|
|
msg.Time = 100 // set time to non-zero for a static time in the marshalled result
|
|
msg.Result = &ConfigParam{UID: 10}
|
|
require.False(t, msg.Result.IsEmpty())
|
|
|
|
var bla int
|
|
err := msg.Result.Unmarshal(&bla)
|
|
require.Equal(t, ErrMsgNotSupported, err)
|
|
|
|
// marshall the message with the deferredDecoder with a raw message
|
|
ret, err := json.Marshal(msg)
|
|
require.Nil(t, err)
|
|
|
|
outcome := `{"dinetrpc":1,"time":100,"result":{"uid": 10}}`
|
|
require.JSONEq(t, outcome, string(ret))
|
|
}
|