...

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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package httpchaos
    15  
    16  import (
    17  	"fmt"
    18  	"net/http"
    19  	"strings"
    20  	"time"
    21  )
    22  
    23  const SECRET = "Secret"
    24  
    25  func getPodHttp(c http.Client, port uint16, secret, body string) (*http.Response, error) {
    26  	request, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://localhost:%d/http", port), strings.NewReader(body))
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	request.Header.Set(SECRET, secret)
    31  	client := &http.Client{
    32  		Transport: &http.Transport{},
    33  	}
    34  	resp, err := client.Do(request)
    35  	return resp, err
    36  }
    37  
    38  func getPodHttpNoSecret(c http.Client, port uint16) (*http.Response, error) {
    39  	return getPodHttp(c, port, "", "")
    40  }
    41  
    42  func getPodHttpDefaultSecret(c http.Client, port uint16, body string) (*http.Response, error) {
    43  	return getPodHttp(c, port, "foo", body)
    44  }
    45  
    46  func getPodHttpNoBody(c http.Client, port uint16) (*http.Response, error) {
    47  	return getPodHttpDefaultSecret(c, port, "")
    48  }
    49  
    50  // get pod http delay
    51  func getPodHttpDelay(c http.Client, port uint16) (*http.Response, time.Duration, error) {
    52  	start := time.Now()
    53  	resp, err := getPodHttpNoBody(c, port)
    54  	if err != nil {
    55  		return nil, 0, err
    56  	}
    57  
    58  	return resp, time.Now().Sub(start), nil
    59  }
    60