...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/curl/render_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/curl

     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 curl
    17  
    18  import (
    19  	"fmt"
    20  	"net/http"
    21  	"strings"
    22  )
    23  
    24  // some example usage of renderCommands
    25  // notice that the output could not be used in shell directly, you need quotes and escape
    26  func Example_renderCommands() {
    27  	commands, _ := renderCommands(CommandFlags{
    28  		Method:         http.MethodGet,
    29  		URL:            "https://github.com/chaos-mesh/chaos-mesh",
    30  		Header:         nil,
    31  		Body:           "",
    32  		FollowLocation: true,
    33  		JsonContent:    false,
    34  	})
    35  
    36  	fmt.Println(strings.Join(commands, " "))
    37  	// Output: curl -i -s -L https://github.com/chaos-mesh/chaos-mesh
    38  }
    39  
    40  func Example_renderCommands_withCustomHeader() {
    41  	commands, _ := renderCommands(CommandFlags{
    42  		Method: http.MethodGet,
    43  		URL:    "https://github.com/chaos-mesh/chaos-mesh",
    44  		Header: Header{
    45  			"User-Agent": []string{"Go-http-client/1.1"},
    46  		},
    47  		Body:           "",
    48  		FollowLocation: true,
    49  		JsonContent:    false,
    50  	})
    51  
    52  	fmt.Println(strings.Join(commands, " "))
    53  	// Output: curl -i -s -L -H User-Agent: Go-http-client/1.1 https://github.com/chaos-mesh/chaos-mesh
    54  }
    55  
    56  func Example_renderCommands_postJson() {
    57  	commands, _ := renderCommands(CommandFlags{
    58  		Method:         http.MethodPost,
    59  		URL:            "https://jsonplaceholder.typicode.com/posts",
    60  		Header:         nil,
    61  		Body:           "{\"foo\": \"bar\"}",
    62  		FollowLocation: false,
    63  		JsonContent:    true,
    64  	})
    65  
    66  	fmt.Println(strings.Join(commands, " "))
    67  	// Output: curl -i -s -X POST -d {"foo": "bar"} -H Content-Type: application/json https://jsonplaceholder.typicode.com/posts
    68  }
    69