github.com/argoproj/argo-events@v1.9.1/test/stress/generator/cmd/webhook.go (about)

     1  package cmd
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/tls"
     6  	"fmt"
     7  	"net/http"
     8  	"os"
     9  	"time"
    10  
    11  	"github.com/spf13/cobra"
    12  	"sigs.k8s.io/controller-runtime/pkg/manager/signals"
    13  )
    14  
    15  func NewWebhookCommand() *cobra.Command {
    16  	var (
    17  		url  string
    18  		data string
    19  
    20  		httpClient = http.Client{
    21  			Timeout:   5 * time.Second,
    22  			Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
    23  			CheckRedirect: func(req *http.Request, via []*http.Request) error {
    24  				return http.ErrUseLastResponse
    25  			},
    26  		}
    27  	)
    28  
    29  	command := &cobra.Command{
    30  		Use:   "webhook",
    31  		Short: "Generate webhook event source messages",
    32  		Long:  ``,
    33  		Run: func(cmd *cobra.Command, args []string) {
    34  			validateGlobalParameters(cmd, args)
    35  			if url == "" {
    36  				fmt.Printf("\nUrl is required\n\n")
    37  				cmd.HelpFunc()(cmd, args)
    38  				os.Exit(1)
    39  			}
    40  			stressRun(signals.SetupSignalHandler(), func() error {
    41  				resp, err := httpClient.Post(url, "application/json", bytes.NewBuffer([]byte(data)))
    42  				if err != nil {
    43  					return fmt.Errorf("failed to post an event: %w", err)
    44  				}
    45  				if resp.StatusCode == 200 {
    46  					return nil
    47  				}
    48  				return fmt.Errorf("failed to post an message, response code: %d", resp.StatusCode)
    49  			})
    50  		},
    51  	}
    52  	command.Flags().StringVar(&url, "url", "", "The url to post messages")
    53  	command.Flags().StringVar(&data, "data", "{}", "The data to post")
    54  	return command
    55  }