...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/statuscheck/http/http_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/statuscheck/http

     1  // Copyright 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 http
    17  
    18  import "testing"
    19  
    20  func Test_validateStatusCode(t *testing.T) {
    21  	tcs := []struct {
    22  		name     string
    23  		criteria string
    24  		result   int
    25  		expect   bool
    26  	}{
    27  		{
    28  			name:     "single code, correct result",
    29  			criteria: "200",
    30  			result:   200,
    31  			expect:   true,
    32  		}, {
    33  			name:     "single code, wrong result",
    34  			criteria: "200",
    35  			result:   201,
    36  			expect:   false,
    37  		}, {
    38  			name:     "code range, correct result",
    39  			criteria: "200-200",
    40  			result:   200,
    41  			expect:   true,
    42  		}, {
    43  			name:     "code range, correct result",
    44  			criteria: "200-400",
    45  			result:   400,
    46  			expect:   true,
    47  		}, {
    48  			name:     "code range, wrong result",
    49  			criteria: "200-400",
    50  			result:   500,
    51  			expect:   false,
    52  		}, {
    53  			name:     "illegal criteria",
    54  			criteria: "200.400",
    55  			result:   500,
    56  			expect:   false,
    57  		}, {
    58  			name:     "illegal criteria",
    59  			criteria: "200-x",
    60  			result:   500,
    61  			expect:   false,
    62  		},
    63  	}
    64  
    65  	for _, tc := range tcs {
    66  		t.Run(tc.name, func(t *testing.T) {
    67  			ok := validateStatusCode(tc.criteria, response{statusCode: tc.result})
    68  			if ok != tc.expect {
    69  				t.Errorf("criteria: %s result: %d expect: %t", tc.criteria, tc.result, tc.expect)
    70  			}
    71  		})
    72  	}
    73  }
    74