package twilio import ( "context" "io" "net/http" "net/http/httptest" "testing" "github.com/stretchr/testify/assert" "github.com/target/goalert/config" ) func TestTwiMLResponse(t *testing.T) { t.Run("hangup", func(t *testing.T) { var mockConfig config.Config ctx := mockConfig.Context(context.Background()) rec := httptest.NewRecorder() r := newTwiMLResponse(ctx, rec) r.Say("Hello") r.Hangup() resp := rec.Result() assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Contains(t, resp.Header.Get("Content-Type"), "application/xml") data, err := io.ReadAll(resp.Body) assert.NoError(t, err) assert.Equal(t, ` Hello Goodbye. `, string(data)) }) t.Run("redirect", func(t *testing.T) { var mockConfig config.Config mockConfig.Twilio.VoiceLanguage = "en-US" ctx := mockConfig.Context(context.Background()) rec := httptest.NewRecorder() r := newTwiMLResponse(ctx, rec) r.Say("Hello") r.Redirect("http://example.com") resp := rec.Result() assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Contains(t, resp.Header.Get("Content-Type"), "application/xml") data, err := io.ReadAll(resp.Body) assert.NoError(t, err) assert.Equal(t, ` Hello http://example.com `, string(data)) }) t.Run("redirect-pause", func(t *testing.T) { var mockConfig config.Config mockConfig.Twilio.VoiceName = "Polly.Joanna-Neural" mockConfig.Twilio.VoiceLanguage = "en-US" ctx := mockConfig.Context(context.Background()) rec := httptest.NewRecorder() r := newTwiMLResponse(ctx, rec) r.Say("Hello! This is GoAlert.") r.RedirectPauseSec("http://example.com", 3) resp := rec.Result() assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Contains(t, resp.Header.Get("Content-Type"), "application/xml") data, err := io.ReadAll(resp.Body) assert.NoError(t, err) assert.Equal(t, ` Hello! This is GoAlert. http://example.com `, string(data)) }) t.Run("unknown-gather", func(t *testing.T) { var mockConfig config.Config ctx := mockConfig.Context(context.Background()) rec := httptest.NewRecorder() r := newTwiMLResponse(ctx, rec) r.SayUnknownDigit() r.Say("Hello") r.Gather("http://example.com") resp := rec.Result() assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Contains(t, resp.Header.Get("Content-Type"), "application/xml") data, err := io.ReadAll(resp.Body) assert.NoError(t, err) assert.Equal(t, ` Sorry, I didn't understand that. Hello If you are done, you may simply hang up. To repeat this message, press star. `, string(data)) }) t.Run("ack test", func(t *testing.T) { var mockConfig config.Config ctx := mockConfig.Context(context.Background()) rec := httptest.NewRecorder() r := newTwiMLResponse(ctx, rec) r.Say("Hello") r.AddOptions(optionAck) r.Gather("http://example.com") resp := rec.Result() assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Contains(t, resp.Header.Get("Content-Type"), "application/xml") data, err := io.ReadAll(resp.Body) assert.NoError(t, err) assert.Equal(t, ` Hello To acknowledge, press 4. To repeat this message, press star. `, string(data)) }) t.Run("esc test", func(t *testing.T) { var mockConfig config.Config ctx := mockConfig.Context(context.Background()) rec := httptest.NewRecorder() r := newTwiMLResponse(ctx, rec) r.Say("Hello") r.AddOptions(optionEscalate) r.Gather("http://example.com") resp := rec.Result() assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Contains(t, resp.Header.Get("Content-Type"), "application/xml") data, err := io.ReadAll(resp.Body) assert.NoError(t, err) assert.Equal(t, ` Hello To escalate, press 5. To repeat this message, press star. `, string(data)) }) }