converter_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package binary
  15. import (
  16. "os"
  17. "path"
  18. "reflect"
  19. "testing"
  20. )
  21. func TestMessageDecode(t *testing.T) {
  22. image, err := os.ReadFile(path.Join("../../../docs", "cover.jpg"))
  23. if err != nil {
  24. t.Errorf("Cannot read image: %v", err)
  25. }
  26. tests := []struct {
  27. payload []byte
  28. result map[string]interface{}
  29. }{
  30. {
  31. payload: image,
  32. result: map[string]interface{}{
  33. "self": image,
  34. },
  35. },
  36. }
  37. conv, _ := GetConverter()
  38. for i, tt := range tests {
  39. result, err := conv.Decode(tt.payload)
  40. if err != nil {
  41. t.Errorf("%d decode error: %v", i, err)
  42. }
  43. if !reflect.DeepEqual(tt.result, result) {
  44. t.Errorf("%d result mismatch:\n\nexp=%s\n\ngot=%s\n\n", i, tt.result, result)
  45. }
  46. }
  47. }