...

Source file src/github.com/chaos-mesh/chaos-mesh/e2e-test/e2e/chaos/httpchaos/misc.go

Documentation: github.com/chaos-mesh/chaos-mesh/e2e-test/e2e/chaos/httpchaos

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package httpchaos
    17  
    18  import (
    19  	"fmt"
    20  	"net/http"
    21  	"strings"
    22  	"time"
    23  )
    24  
    25  const SECRET = "Secret"
    26  
    27  type HTTPE2EClient struct {
    28  	IP string
    29  	C  *http.Client
    30  }
    31  
    32  func getPodHttp(c HTTPE2EClient, port uint16, secret, body string) (*http.Response, error) {
    33  	request, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s:%d/http", c.IP, port), strings.NewReader(body))
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	request.Header.Set(SECRET, secret)
    38  	client := &http.Client{
    39  		Transport: &http.Transport{},
    40  	}
    41  	resp, err := client.Do(request)
    42  	return resp, err
    43  }
    44  
    45  func getPodHttpNoSecret(c HTTPE2EClient, port uint16) (*http.Response, error) {
    46  	return getPodHttp(c, port, "", "")
    47  }
    48  
    49  func getPodHttpDefaultSecret(c HTTPE2EClient, port uint16, body string) (*http.Response, error) {
    50  	return getPodHttp(c, port, "foo", body)
    51  }
    52  
    53  func getPodHttpNoBody(c HTTPE2EClient, port uint16) (*http.Response, error) {
    54  	return getPodHttpDefaultSecret(c, port, "")
    55  }
    56  
    57  // get pod http delay
    58  func getPodHttpDelay(c HTTPE2EClient, port uint16) (*http.Response, time.Duration, error) {
    59  	start := time.Now()
    60  	resp, err := getPodHttpNoBody(c, port)
    61  	if err != nil {
    62  		return nil, 0, err
    63  	}
    64  
    65  	return resp, time.Now().Sub(start), nil
    66  }
    67