diff options
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/fwdsms/twilio_test.go | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/cmd/fwdsms/twilio_test.go b/cmd/fwdsms/twilio_test.go index 6418965..b17bf06 100644 --- a/cmd/fwdsms/twilio_test.go +++ b/cmd/fwdsms/twilio_test.go @@ -4,12 +4,18 @@ package main import ( + "io/ioutil" "net/http" "net/url" "strings" "testing" + "time" + "github.com/gorilla/handlers" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "go.awhk.org/pipeln" ) var handler = newSMSHandler(&Config{ @@ -62,3 +68,65 @@ func TestSMSHandler_checkRequestSignature_SignatureGood(t *testing.T) { assert.NoError(t, req.ParseForm()) assert.NoError(t, handler.checkRequestSignature(req)) } + +func TestSMSHandler_EndToEnd(t *testing.T) { + mux := http.NewServeMux() + mux.Handle("/endpoint", handlers.ProxyHeaders(handler)) + srv := http.Server{Handler: mux} + ln := pipeln.New("localhost.test:80") + go srv.Serve(ln) + defer srv.Close() + + client := http.Client{Transport: &http.Transport{Dial: ln.Dial}} + form := url.Values{} + form.Set("From", "Foo") + form.Set("To", "Bar") + form.Set("Body", "Test") + req, err := http.NewRequest(http.MethodPost, "http://localhost.test:80/endpoint", strings.NewReader(form.Encode())) + require.NoError(t, err) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + req.Header.Set("X-Forwarded-Scheme", "http") + + t.Run("Bad HTTP Method", func(t *testing.T) { + resp, err := client.Head("http://localhost.test:80/endpoint") + require.NoError(t, err) + assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) + }) + + t.Run("Bad Signature", func(t *testing.T) { + req.Header.Set("X-Twilio-Signature", "DYIRnXpKIjrgAMxc0FD01B55+ag=") + resp, err := client.Do(req) + require.NoError(t, err) + assert.Equal(t, http.StatusBadRequest, resp.StatusCode) + }) + + t.Run("Good Signature", func(t *testing.T) { + done := make(chan struct{}) + + go func() { + defer close(done) + select { + case sms := <-handler.sms: + assert.Equal(t, "Foo", sms.From) + assert.Equal(t, "Bar", sms.To) + assert.Equal(t, "Test", sms.Message) + case <-time.After(time.Second): + t.Error("Timed out while waiting on handler.sms.") + } + }() + + // Signature generated with: + // % echo -n "http://localhost.test:80/endpointBodyTestFromFooToBar" | openssl dgst -binary -hmac "token" -sha1 | base64 + req.Header.Set("X-Twilio-Signature", "iiifXqv3dP5j8Oj5eB4RAOm/3tI=") + resp, err := client.Do(req) + require.NoError(t, err) + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + require.NoError(t, err) + + assert.Equal(t, http.StatusOK, resp.StatusCode) + assert.Equal(t, "text/xml", resp.Header.Get("Content-Type")) + assert.Equal(t, "<Response/>", string(body)) + <-done + }) +} |
