...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/curl/roundtrip_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  	"net/http"
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  )
    24  
    25  // Maybe we need some fuzzing test
    26  
    27  func TestRoundTrip(t *testing.T) {
    28  	RegisterTestingT(t)
    29  	tests := []struct {
    30  		name  string
    31  		flags CommandFlags
    32  	}{
    33  		{
    34  			name: "simple get",
    35  			flags: CommandFlags{
    36  				Method:         http.MethodGet,
    37  				URL:            "github.com/chaos-mesh/chaos-mesh",
    38  				Header:         nil,
    39  				Body:           "",
    40  				FollowLocation: false,
    41  				JsonContent:    false,
    42  			},
    43  		}, {
    44  			name: "get with header",
    45  			flags: CommandFlags{
    46  				Method: http.MethodGet,
    47  				URL:    "https://github.com/chaos-mesh/chaos-mesh",
    48  				Header: Header{
    49  					"User-Agent": []string{"Go-http-client/1.1"},
    50  				},
    51  				Body:           "",
    52  				FollowLocation: false,
    53  				JsonContent:    false,
    54  			},
    55  		}, {
    56  			name: "post json",
    57  			flags: 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  			name: "post json with custom header",
    67  			flags: CommandFlags{
    68  				Method: http.MethodPost,
    69  				URL:    "https://jsonplaceholder.typicode.com/posts",
    70  				Header: Header{
    71  					"User-Agent": []string{"Go-http-client/1.1"},
    72  				},
    73  				Body:           "{\"foo\": \"bar\"}",
    74  				FollowLocation: false,
    75  				JsonContent:    true,
    76  			},
    77  		}, {
    78  			name: "get with following location",
    79  			flags: CommandFlags{
    80  				Method:         http.MethodGet,
    81  				URL:            "www.google.com",
    82  				Header:         nil,
    83  				Body:           "",
    84  				FollowLocation: true,
    85  				JsonContent:    false,
    86  			},
    87  		},
    88  	}
    89  	for _, test := range tests {
    90  		t.Run(test.name, func(t *testing.T) {
    91  			commands, err := renderCommands(test.flags)
    92  			Expect(err).ShouldNot(HaveOccurred())
    93  			parsedFlags, err := parseCommands(commands)
    94  			Expect(err).ShouldNot(HaveOccurred())
    95  			Expect(parsedFlags).To(Equal(&test.flags), "rendered commands %+v", commands)
    96  		})
    97  	}
    98  }
    99