github.com/go-playground/webhooks/v6@v6.3.0/azuredevops/azuredevops_test.go (about)

     1  package azuredevops
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"os"
     8  	"testing"
     9  
    10  	"reflect"
    11  
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  // NOTES:
    16  // - Run "go test" to run tests
    17  // - Run "gocov test | gocov report" to report on test converage by file
    18  // - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called
    19  //
    20  // or
    21  //
    22  // -- may be a good idea to change to output path to somewherelike /tmp
    23  // go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html
    24  //
    25  
    26  const (
    27  	virtualDir = "/webhooks"
    28  )
    29  
    30  var hook *Webhook
    31  
    32  func TestMain(m *testing.M) {
    33  
    34  	// setup
    35  	var err error
    36  	hook, err = New()
    37  	if err != nil {
    38  		log.Fatal(err)
    39  	}
    40  	os.Exit(m.Run())
    41  	// teardown
    42  }
    43  
    44  func newServer(handler http.HandlerFunc) *httptest.Server {
    45  	mux := http.NewServeMux()
    46  	mux.HandleFunc(virtualDir, handler)
    47  	return httptest.NewServer(mux)
    48  }
    49  
    50  func TestWebhooks(t *testing.T) {
    51  	assert := require.New(t)
    52  	tests := []struct {
    53  		name     string
    54  		event    Event
    55  		typ      interface{}
    56  		filename string
    57  		headers  http.Header
    58  	}{
    59  		{
    60  			name:     "build.complete",
    61  			event:    BuildCompleteEventType,
    62  			typ:      BuildCompleteEvent{},
    63  			filename: "../testdata/azuredevops/build.complete.json",
    64  		},
    65  		{
    66  			name:     "git.pullrequest.created",
    67  			event:    GitPullRequestCreatedEventType,
    68  			typ:      GitPullRequestEvent{},
    69  			filename: "../testdata/azuredevops/git.pullrequest.created.json",
    70  		},
    71  		{
    72  			name:     "git.pullrequest.merged",
    73  			event:    GitPullRequestMergedEventType,
    74  			typ:      GitPullRequestEvent{},
    75  			filename: "../testdata/azuredevops/git.pullrequest.merged.json",
    76  		},
    77  		{
    78  			name:     "git.pullrequest.updated",
    79  			event:    GitPullRequestUpdatedEventType,
    80  			typ:      GitPullRequestEvent{},
    81  			filename: "../testdata/azuredevops/git.pullrequest.updated.json",
    82  		},
    83  		{
    84  			name:     "git.push",
    85  			event:    GitPushEventType,
    86  			typ:      GitPushEvent{},
    87  			filename: "../testdata/azuredevops/git.push.json",
    88  		},
    89  	}
    90  
    91  	for _, tt := range tests {
    92  		tc := tt
    93  		client := &http.Client{}
    94  		t.Run(tt.name, func(t *testing.T) {
    95  			t.Parallel()
    96  			payload, err := os.Open(tc.filename)
    97  			assert.NoError(err)
    98  			defer func() {
    99  				_ = payload.Close()
   100  			}()
   101  
   102  			var parseError error
   103  			var results interface{}
   104  			server := newServer(func(w http.ResponseWriter, r *http.Request) {
   105  				results, parseError = hook.Parse(r, tc.event)
   106  			})
   107  			defer server.Close()
   108  			req, err := http.NewRequest(http.MethodPost, server.URL+virtualDir, payload)
   109  			assert.NoError(err)
   110  			req.Header.Set("Content-Type", "application/json")
   111  
   112  			resp, err := client.Do(req)
   113  			assert.NoError(err)
   114  			assert.Equal(http.StatusOK, resp.StatusCode)
   115  			assert.NoError(parseError)
   116  			assert.Equal(reflect.TypeOf(tc.typ), reflect.TypeOf(results))
   117  		})
   118  	}
   119  }