github.com/argoproj/argo-cd/v3@v3.2.1/test/e2e/fixture/http.go (about)

     1  package fixture
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/tls"
     6  	"encoding/json"
     7  	"io"
     8  	"net/http"
     9  	"net/url"
    10  
    11  	"github.com/argoproj/argo-cd/v3/common"
    12  )
    13  
    14  // DoHttpRequest executes a http request against the Argo CD API server
    15  func DoHttpRequest(method string, path string, host string, data ...byte) (*http.Response, error) { //nolint:revive //FIXME(var-naming)
    16  	reqURL, err := url.Parse(path)
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  	reqURL.Scheme = "http"
    21  	if host != "" {
    22  		reqURL.Host = host
    23  	} else {
    24  		reqURL.Host = apiServerAddress
    25  	}
    26  	var body io.Reader
    27  	if data != nil {
    28  		body = bytes.NewReader(data)
    29  	}
    30  	req, err := http.NewRequest(method, reqURL.String(), body)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	req.AddCookie(&http.Cookie{Name: common.AuthCookieName, Value: token})
    35  	req.Header.Set("Content-Type", "application/json")
    36  
    37  	httpClient := &http.Client{
    38  		Transport: &http.Transport{
    39  			TLSClientConfig: &tls.Config{InsecureSkipVerify: IsRemote()},
    40  		},
    41  	}
    42  
    43  	return httpClient.Do(req)
    44  }
    45  
    46  // DoHttpJsonRequest executes a http request against the Argo CD API server and unmarshals the response body as JSON
    47  func DoHttpJsonRequest(method string, path string, result any, data ...byte) error { //nolint:revive //FIXME(var-naming)
    48  	resp, err := DoHttpRequest(method, path, "", data...)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	responseData, err := io.ReadAll(resp.Body)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	return json.Unmarshal(responseData, result)
    57  }