diff options
| -rw-r--r-- | cmd/fwdsms/twilio_test.go | 68 | ||||
| -rw-r--r-- | go.mod | 1 | ||||
| -rw-r--r-- | go.sum | 2 |
3 files changed, 71 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 + }) +} @@ -5,6 +5,7 @@ go 1.15 require ( github.com/gorilla/handlers v1.5.1 github.com/stretchr/testify v1.7.0 + go.awhk.org/pipeln v0.1.0 golang.org/x/sys v0.0.0-20210218155724-8ebf48af031b gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) @@ -10,6 +10,8 @@ github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +go.awhk.org/pipeln v0.1.0 h1:lqH2sV2g1mGg207mo5EvKtEFDg30mQ0JoECrhRl5lp8= +go.awhk.org/pipeln v0.1.0/go.mod h1:+yf4v5PiMs/Zr9sQlUUPkPA4jULrBADr+hDuAPjtXZU= golang.org/x/sys v0.0.0-20210218155724-8ebf48af031b h1:lAZ0/chPUDWwjqosYR0X4M490zQhMsiJ4K3DbA7o+3g= golang.org/x/sys v0.0.0-20210218155724-8ebf48af031b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= |
