github.com/cloudwego/hertz@v0.9.3/pkg/common/utils/network_test.go (about)

     1  /*
     2   * Copyright 2022 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package utils
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/cloudwego/hertz/pkg/common/test/assert"
    23  )
    24  
    25  func TestTLSRecordHeaderLooksLikeHTTP(t *testing.T) {
    26  	HeaderValueAndExpectedResult := [][]interface{}{
    27  		{[5]byte{'G', 'E', 'T', ' ', '/'}, true},
    28  		{[5]byte{'H', 'E', 'A', 'D', ' '}, true},
    29  		{[5]byte{'P', 'O', 'S', 'T', ' '}, true},
    30  		{[5]byte{'P', 'U', 'T', ' ', '/'}, true},
    31  		{[5]byte{'O', 'P', 'T', 'I', 'O'}, true},
    32  		{[5]byte{'G', 'E', 'T', '/', ' '}, false},
    33  		{[5]byte{' ', 'H', 'E', 'A', 'D'}, false},
    34  		{[5]byte{' ', 'P', 'O', 'S', 'T'}, false},
    35  		{[5]byte{'P', 'U', 'T', '/', ' '}, false},
    36  		{[5]byte{'H', 'E', 'R', 'T', 'Z'}, false},
    37  	}
    38  
    39  	for _, testCase := range HeaderValueAndExpectedResult {
    40  		value, expectedResult := testCase[0].([5]byte), testCase[1].(bool)
    41  		assert.DeepEqual(t, expectedResult, TLSRecordHeaderLooksLikeHTTP(value))
    42  	}
    43  }
    44  
    45  func TestLocalIP(t *testing.T) {
    46  	// Mock the localIP variable for testing purposes.
    47  	localIP = "192.168.0.1"
    48  
    49  	// Ensure that LocalIP() returns the expected local IP.
    50  	expectedIP := "192.168.0.1"
    51  	if got := LocalIP(); got != expectedIP {
    52  		assert.DeepEqual(t, got, expectedIP)
    53  	}
    54  }