Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ func (d *Server) Run(in io.Reader, out io.Writer) error {

switch cmd {
case "HELLO":
d.hello(fullCmd[6:])
if len(fullCmd) < 7 {
d.hello("")
} else {
d.hello(fullCmd[6:])
}
case "START":
d.start()
case "LIST":
Expand Down
32 changes: 24 additions & 8 deletions discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,29 @@ func TestDisc(t *testing.T) {

require.NoError(t, discovery.Start())

n, err := stdin.Write([]byte("quit\n"))
require.NoError(t, err)
require.Greater(t, n, 0)
output := [1024]byte{}
n, err = stdout.Read(output[:])
require.Greater(t, n, 0)
require.NoError(t, err)
{
// Check that discovery is able to handle an "hello" without parameters gracefully
// https://github.com/arduino/pluggable-discovery-protocol-handler/issues/32
inN, err := stdin.Write([]byte("hello\n"))
require.NoError(t, err)
require.Greater(t, inN, 0)

output := [1024]byte{}
outN, err := stdout.Read(output[:])
require.Greater(t, outN, 0)
require.NoError(t, err)
require.Equal(t, "{\n \"eventType\": \"hello\",\n \"message\": \"Invalid HELLO command\",\n \"error\": true\n}\n", string(output[:outN]))
}

{
inN, err := stdin.Write([]byte("quit\n"))
require.NoError(t, err)
require.Greater(t, inN, 0)

require.Equal(t, "{\n \"eventType\": \"quit\",\n \"message\": \"OK\"\n}\n", string(output[:n]))
output := [1024]byte{}
outN, err := stdout.Read(output[:])
require.Greater(t, outN, 0)
require.NoError(t, err)
require.Equal(t, "{\n \"eventType\": \"quit\",\n \"message\": \"OK\"\n}\n", string(output[:outN]))
}
}