github.com/argoproj/argo-cd/v2@v2.10.5/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/v2/common"
    12  )
    13  
    14  // DoHttpRequest executes a http request against the Argo CD API server
    15  func DoHttpRequest(method string, path string, data ...byte) (*http.Response, error) {
    16  	reqUrl, err := url.Parse(path)
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  	reqUrl.Scheme = "http"
    21  	reqUrl.Host = apiServerAddress
    22  	var body io.Reader
    23  	if data != nil {
    24  		body = bytes.NewReader(data)
    25  	}
    26  	req, err := http.NewRequest(method, reqUrl.String(), body)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	req.AddCookie(&http.Cookie{Name: common.AuthCookieName, Value: token})
    31  	req.Header.Set("Content-Type", "application/json")
    32  
    33  	httpClient := &http.Client{
    34  		Transport: &http.Transport{
    35  			TLSClientConfig: &tls.Config{InsecureSkipVerify: IsRemote()},
    36  		},
    37  	}
    38  
    39  	return httpClient.Do(req)
    40  }
    41  
    42  // DoHttpJsonRequest executes a http request against the Argo CD API server and unmarshals the response body as JSON
    43  func DoHttpJsonRequest(method string, path string, result interface{}, data ...byte) error {
    44  	resp, err := DoHttpRequest(method, path, data...)
    45  	if err != nil {
    46  		return err
    47  	}
    48  	responseData, err := io.ReadAll(resp.Body)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	return json.Unmarshal(responseData, result)
    53  }