github.com/blueinnovationsgroup/can-go@v0.0.0-20230518195432-d0567cda0028/pkg/candebug/http_test.go (about)

     1  package candebug
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/blueinnovationsgroup/can-go"
    13  	"github.com/blueinnovationsgroup/can-go/pkg/descriptor"
    14  	"github.com/blueinnovationsgroup/can-go/pkg/generated"
    15  	"gotest.tools/v3/assert"
    16  )
    17  
    18  func TestServeMessagesHTTP_Single(t *testing.T) {
    19  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    20  		ServeMessagesHTTP(w, r, []generated.Message{
    21  			&testMessage{
    22  				frame:      can.Frame{ID: 100, Length: 1},
    23  				descriptor: newDriverHeartbeatDescriptor(),
    24  			},
    25  		})
    26  	}))
    27  	c := http.DefaultClient
    28  	ctx := context.Background()
    29  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL, nil)
    30  	assert.NilError(t, err)
    31  	res, err := c.Do(req)
    32  	assert.NilError(t, err)
    33  	response, err := io.ReadAll(res.Body)
    34  	assert.NilError(t, err)
    35  	assert.NilError(t, res.Body.Close())
    36  	const expected = `
    37  DriverHeartbeat
    38  ===============
    39  ID: 100 (0x64)
    40  Sender: DRIVER
    41  SendType: Cyclic
    42  CycleTime: 100ms
    43  DelayTime: 2s
    44  ===============
    45  Command: 0 (0x0) None
    46  `
    47  	assert.Equal(t, strings.TrimSpace(expected), string(response))
    48  }
    49  
    50  func TestServeMessagesHTTP_Multi(t *testing.T) {
    51  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    52  		ServeMessagesHTTP(w, r, []generated.Message{
    53  			&testMessage{
    54  				frame:      can.Frame{ID: 100, Length: 1},
    55  				descriptor: newDriverHeartbeatDescriptor(),
    56  			},
    57  			&testMessage{
    58  				frame:      can.Frame{ID: 100, Length: 1, Data: can.Data{0x01}},
    59  				descriptor: newDriverHeartbeatDescriptor(),
    60  			},
    61  		})
    62  	}))
    63  	c := http.DefaultClient
    64  	ctx := context.Background()
    65  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL, nil)
    66  	assert.NilError(t, err)
    67  	res, err := c.Do(req)
    68  	assert.NilError(t, err)
    69  	response, err := io.ReadAll(res.Body)
    70  	assert.NilError(t, err)
    71  	assert.NilError(t, res.Body.Close())
    72  	const expected = `
    73  DriverHeartbeat
    74  ===============
    75  ID: 100 (0x64)
    76  Sender: DRIVER
    77  SendType: Cyclic
    78  CycleTime: 100ms
    79  DelayTime: 2s
    80  ===============
    81  Command: 0 (0x0) None
    82  
    83  
    84  DriverHeartbeat
    85  ===============
    86  ID: 100 (0x64)
    87  Sender: DRIVER
    88  SendType: Cyclic
    89  CycleTime: 100ms
    90  DelayTime: 2s
    91  ===============
    92  Command: 1 (0x1) Sync
    93  `
    94  	assert.Equal(t, strings.TrimSpace(expected), string(response))
    95  }
    96  
    97  type testMessage struct {
    98  	frame      can.Frame
    99  	descriptor *descriptor.Message
   100  }
   101  
   102  func (m *testMessage) Frame() can.Frame {
   103  	return m.frame
   104  }
   105  
   106  func (m *testMessage) Descriptor() *descriptor.Message {
   107  	return m.descriptor
   108  }
   109  
   110  func (m *testMessage) MarshalFrame() (can.Frame, error) {
   111  	panic("should not be called")
   112  }
   113  
   114  func (testMessage) Reset() {
   115  	panic("should not be called")
   116  }
   117  
   118  func (testMessage) String() string {
   119  	panic("should not be called")
   120  }
   121  
   122  func (testMessage) UnmarshalFrame(can.Frame) error {
   123  	panic("should not be called")
   124  }
   125  
   126  func newDriverHeartbeatDescriptor() *descriptor.Message {
   127  	return &descriptor.Message{
   128  		Name:        "DriverHeartbeat",
   129  		SenderNode:  "DRIVER",
   130  		ID:          100,
   131  		Length:      1,
   132  		Description: "Sync message used to synchronize the controllers",
   133  		SendType:    descriptor.SendTypeCyclic,
   134  		CycleTime:   100 * time.Millisecond,
   135  		DelayTime:   2 * time.Second,
   136  		Signals: []*descriptor.Signal{
   137  			{
   138  				Name:   "Command",
   139  				Start:  0,
   140  				Length: 8,
   141  				Scale:  1,
   142  				ValueDescriptions: []*descriptor.ValueDescription{
   143  					{Value: 0, Description: "None"},
   144  					{Value: 1, Description: "Sync"},
   145  					{Value: 2, Description: "Reboot"},
   146  				},
   147  				ReceiverNodes: []string{"SENSOR", "MOTOR"},
   148  			},
   149  		},
   150  	}
   151  }