message_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package coap
  2. import (
  3. "bytes"
  4. "fmt"
  5. "testing"
  6. )
  7. func TestMediaTypes(t *testing.T) {
  8. types := []interface{}{TextPlain, AppXML, AppOctets, AppExi, AppJSON}
  9. exp := "coap.MediaType"
  10. for _, typ := range types {
  11. if got := fmt.Sprintf("%T", typ); got != exp {
  12. t.Errorf("Error on %#v, expected %q, was %q", typ, exp, got)
  13. }
  14. }
  15. }
  16. func TestOptionToBytes(t *testing.T) {
  17. tests := []struct {
  18. in interface{}
  19. exp []byte
  20. }{
  21. {"", []byte{}},
  22. {[]byte{}, []byte{}},
  23. {"x", []byte{'x'}},
  24. {[]byte{'x'}, []byte{'x'}},
  25. {MediaType(3), []byte{0x3}},
  26. {3, []byte{0x3}},
  27. {838, []byte{0x3, 0x46}},
  28. {int32(838), []byte{0x3, 0x46}},
  29. {uint(838), []byte{0x3, 0x46}},
  30. {uint32(838), []byte{0x3, 0x46}},
  31. }
  32. for _, test := range tests {
  33. op := option{Value: test.in}
  34. got := op.toBytes()
  35. if !bytes.Equal(test.exp, got) {
  36. t.Errorf("Error on %T(%v), got %#v, wanted %#v",
  37. test.in, test.in, got, test.exp)
  38. }
  39. }
  40. }
  41. func TestMessageConfirmable(t *testing.T) {
  42. tests := []struct {
  43. m Message
  44. exp bool
  45. }{
  46. {&BaseMessage{mtype: CON}, true},
  47. {&BaseMessage{mtype: NON}, false},
  48. }
  49. for _, test := range tests {
  50. got := test.m.IsConfirmable()
  51. if got != test.exp {
  52. t.Errorf("Expected %v for %v", test.exp, test.m)
  53. }
  54. }
  55. }