github.com/hahmadia/mattermost-server@v5.11.1+incompatible/utils/utils_test.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package utils
     5  
     6  import (
     7  	"net/http"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestStringArrayIntersection(t *testing.T) {
    14  	a := []string{
    15  		"abc",
    16  		"def",
    17  		"ghi",
    18  	}
    19  	b := []string{
    20  		"jkl",
    21  	}
    22  	c := []string{
    23  		"def",
    24  	}
    25  
    26  	if len(StringArrayIntersection(a, b)) != 0 {
    27  		t.Fatal("should be 0")
    28  	}
    29  
    30  	if len(StringArrayIntersection(a, c)) != 1 {
    31  		t.Fatal("should be 1")
    32  	}
    33  }
    34  
    35  func TestRemoveDuplicatesFromStringArray(t *testing.T) {
    36  	a := []string{
    37  		"a",
    38  		"b",
    39  		"a",
    40  		"a",
    41  		"b",
    42  		"c",
    43  		"a",
    44  	}
    45  
    46  	if len(RemoveDuplicatesFromStringArray(a)) != 3 {
    47  		t.Fatal("should be 3")
    48  	}
    49  }
    50  
    51  func TestStringSliceDiff(t *testing.T) {
    52  	a := []string{"one", "two", "three", "four", "five", "six"}
    53  	b := []string{"two", "seven", "four", "six"}
    54  	expected := []string{"one", "three", "five"}
    55  
    56  	assert.Equal(t, StringSliceDiff(a, b), expected)
    57  }
    58  
    59  func TestGetIpAddress(t *testing.T) {
    60  	// Test with a single IP in the X-Forwarded-For
    61  	httpRequest1 := http.Request{
    62  		Header: http.Header{
    63  			"X-Forwarded-For": []string{"10.0.0.1"},
    64  			"X-Real-Ip":       []string{"10.1.0.1"},
    65  		},
    66  		RemoteAddr: "10.2.0.1:12345",
    67  	}
    68  
    69  	assert.Equal(t, "10.0.0.1", GetIpAddress(&httpRequest1))
    70  
    71  	// Test with multiple IPs in the X-Forwarded-For
    72  	httpRequest2 := http.Request{
    73  		Header: http.Header{
    74  			"X-Forwarded-For": []string{"10.0.0.1,  10.0.0.2, 10.0.0.3"},
    75  			"X-Real-Ip":       []string{"10.1.0.1"},
    76  		},
    77  		RemoteAddr: "10.2.0.1:12345",
    78  	}
    79  
    80  	assert.Equal(t, "10.0.0.1", GetIpAddress(&httpRequest2))
    81  
    82  	// Test with an empty X-Forwarded-For
    83  	httpRequest3 := http.Request{
    84  		Header: http.Header{
    85  			"X-Forwarded-For": []string{""},
    86  			"X-Real-Ip":       []string{"10.1.0.1"},
    87  		},
    88  		RemoteAddr: "10.2.0.1:12345",
    89  	}
    90  
    91  	assert.Equal(t, "10.1.0.1", GetIpAddress(&httpRequest3))
    92  
    93  	// Test without an X-Fowarded-For
    94  	httpRequest4 := http.Request{
    95  		Header: http.Header{
    96  			"X-Real-Ip": []string{"10.1.0.1"},
    97  		},
    98  		RemoteAddr: "10.2.0.1:12345",
    99  	}
   100  
   101  	assert.Equal(t, "10.1.0.1", GetIpAddress(&httpRequest4))
   102  
   103  	// Test without any headers
   104  	httpRequest5 := http.Request{
   105  		RemoteAddr: "10.2.0.1:12345",
   106  	}
   107  
   108  	assert.Equal(t, "10.2.0.1", GetIpAddress(&httpRequest5))
   109  }