package asteriskws import "testing" func TestParsePlainTextControlEvents(t *testing.T) { ev := ParseControlEvent("MEDIA_START connection_id:abc channel:WebSocket/INCOMING-1 channel_id:123 format:slin16 optimal_frame_size:640 ptime:20") ms := ev.MediaStart() if ev.Type != ControlMediaStart || ms.ConnectionID != "abc" || ms.OptimalFrameSize != 640 || ms.PTimeMS != 20 { t.Fatalf("bad media start %+v %+v", ev, ms) } cases := map[string]ControlEventType{"DTMF_END channel_id:123 digit:5": ControlDTMFEnd, "MEDIA_XOFF channel_id:123": ControlMediaXOFF, "MEDIA_XON channel_id:123": ControlMediaXON, "STATUS channel_id:123 queue_length:0": ControlStatus, "MEDIA_BUFFERING_COMPLETED test-123": ControlMediaBufferingCompleted, "MEDIA_MARK_PROCESSED mark-123": ControlMediaMarkProcessed, "QUEUE_DRAINED channel_id:123": ControlQueueDrained, "SOMETHING value:1": ControlUnknown, "": ControlUnknown} for input, want := range cases { if got := ParseControlEvent(input); got.Type != want { t.Fatalf("%q got %s want %s", input, got.Type, want) } } } func TestParseJSONControlEvents(t *testing.T) { for _, input := range []string{`{"event":"MEDIA_START","connection_id":"abc","channel_id":"123","format":"slin16","optimal_frame_size":640,"ptime":20}`, `{"type":"MEDIA_XOFF","channel_id":"123"}`, `{"event":"NEW_EVENT"}`} { if ev := ParseControlEvent(input); ev.Type == "" { t.Fatalf("empty type") } } }