github.com/argoproj/argo-events@v1.9.1/eventsources/sources/webhook/start_test.go (about)

     1  package webhook
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"net/http"
     7  	"net/url"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/smartystreets/goconvey/convey"
    12  
    13  	"github.com/argoproj/argo-events/eventsources/common/webhook"
    14  )
    15  
    16  func TestHandleRoute(t *testing.T) {
    17  	convey.Convey("Given a route that receives HTTP access", t, func() {
    18  		router := &Router{
    19  			route: webhook.GetFakeRoute(),
    20  		}
    21  		writer := &webhook.FakeHttpWriter{}
    22  
    23  		convey.Convey("Test Get method with query parameters", func() {
    24  			url, _ := url.Parse("http://example.com/fake?aaa=b%20b&ccc=d%20d")
    25  			out := make(chan []byte)
    26  			router.route.Active = true
    27  			router.route.Context.Method = http.MethodGet
    28  
    29  			go func() {
    30  				out <- <-router.route.DataCh
    31  			}()
    32  
    33  			router.HandleRoute(writer, &http.Request{
    34  				Method: http.MethodGet,
    35  				URL:    url,
    36  			})
    37  			result := <-out
    38  			convey.So(writer.HeaderStatus, convey.ShouldEqual, http.StatusOK)
    39  			convey.So(string(result), convey.ShouldContainSubstring, `"body":{"aaa":["b b"],"ccc":["d d"]}`)
    40  		})
    41  		convey.Convey("Test Get method without query parameter", func() {
    42  			url, _ := url.Parse("http://example.com/fake")
    43  			out := make(chan []byte)
    44  			router.route.Active = true
    45  			router.route.Context.Method = http.MethodGet
    46  
    47  			go func() {
    48  				out <- <-router.route.DataCh
    49  			}()
    50  
    51  			router.HandleRoute(writer, &http.Request{
    52  				Method: http.MethodGet,
    53  				URL:    url,
    54  			})
    55  			result := <-out
    56  			convey.So(writer.HeaderStatus, convey.ShouldEqual, http.StatusOK)
    57  			convey.So(string(result), convey.ShouldContainSubstring, `"body":{}`)
    58  		})
    59  		convey.Convey("Test POST method with form-urlencoded", func() {
    60  			payload := []byte(`aaa=b%20b&ccc=d%20d`)
    61  
    62  			out := make(chan []byte)
    63  			router.route.Active = true
    64  			router.route.Context.Method = http.MethodPost
    65  
    66  			go func() {
    67  				out <- <-router.route.DataCh
    68  			}()
    69  
    70  			var buf bytes.Buffer
    71  			buf.Write(payload)
    72  
    73  			headers := make(map[string][]string)
    74  			headers["Content-Type"] = []string{"application/x-www-form-urlencoded"}
    75  
    76  			router.HandleRoute(writer, &http.Request{
    77  				Method: http.MethodPost,
    78  				Header: headers,
    79  				Body:   io.NopCloser(strings.NewReader(buf.String())),
    80  			})
    81  			result := <-out
    82  			convey.So(writer.HeaderStatus, convey.ShouldEqual, http.StatusOK)
    83  			convey.So(string(result), convey.ShouldContainSubstring, `"body":{"aaa":["b b"],"ccc":["d d"]}`)
    84  		})
    85  		convey.Convey("Test POST method with json", func() {
    86  			payload := []byte(`{"aaa":["b b"],"ccc":["d d"]}`)
    87  
    88  			out := make(chan []byte)
    89  			router.route.Active = true
    90  			router.route.Context.Method = http.MethodPost
    91  
    92  			go func() {
    93  				out <- <-router.route.DataCh
    94  			}()
    95  
    96  			var buf bytes.Buffer
    97  			buf.Write(payload)
    98  
    99  			headers := make(map[string][]string)
   100  			headers["Content-Type"] = []string{"application/json"}
   101  
   102  			router.HandleRoute(writer, &http.Request{
   103  				Method: http.MethodPost,
   104  				Header: headers,
   105  				Body:   io.NopCloser(strings.NewReader(buf.String())),
   106  			})
   107  			result := <-out
   108  			convey.So(writer.HeaderStatus, convey.ShouldEqual, http.StatusOK)
   109  			convey.So(string(result), convey.ShouldContainSubstring, `"body":{"aaa":["b b"],"ccc":["d d"]}`)
   110  		})
   111  	})
   112  }