github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/handlers/proxy_test.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package handlers
    19  
    20  import (
    21  	"net/http"
    22  	"testing"
    23  )
    24  
    25  type headerTest struct {
    26  	key      string // header key
    27  	val      string // header val
    28  	expected string // expected result
    29  }
    30  
    31  func TestGetScheme(t *testing.T) {
    32  	headers := []headerTest{
    33  		{xForwardedProto, "https", "https"},
    34  		{xForwardedProto, "http", "http"},
    35  		{xForwardedProto, "HTTP", "http"},
    36  		{xForwardedScheme, "https", "https"},
    37  		{xForwardedScheme, "http", "http"},
    38  		{xForwardedScheme, "HTTP", "http"},
    39  		{forwarded, `For="[2001:db8:cafe::17]:4711`, ""},                    // No proto
    40  		{forwarded, `for=192.0.2.43, for=198.51.100.17;proto=https`, ""},    // Multiple params, will be empty.
    41  		{forwarded, `for=172.32.10.15; proto=https;by=127.0.0.1;`, "https"}, // Space before proto
    42  		{forwarded, `for=192.0.2.60;proto=http;by=203.0.113.43`, "http"},    // Multiple params
    43  	}
    44  	for _, v := range headers {
    45  		req := &http.Request{
    46  			Header: http.Header{
    47  				v.key: []string{v.val},
    48  			},
    49  		}
    50  		res := GetSourceScheme(req)
    51  		if res != v.expected {
    52  			t.Errorf("wrong header for %s: got %s want %s", v.key, res,
    53  				v.expected)
    54  		}
    55  	}
    56  }
    57  
    58  // TestGetSourceIP - check the source ip of a request is parsed correctly.
    59  func TestGetSourceIP(t *testing.T) {
    60  	headers := []headerTest{
    61  		{xForwardedFor, "8.8.8.8", "8.8.8.8"},                                         // Single address
    62  		{xForwardedFor, "8.8.8.8, 8.8.4.4", "8.8.8.8"},                                // Multiple
    63  		{xForwardedFor, "", ""},                                                       // None
    64  		{xRealIP, "8.8.8.8", "8.8.8.8"},                                               // Single address
    65  		{xRealIP, "[2001:db8:cafe::17]:4711", "[2001:db8:cafe::17]"},                  // IPv6 address
    66  		{xRealIP, "", ""},                                                             // None
    67  		{forwarded, `for="_gazonk"`, "_gazonk"},                                       // Hostname
    68  		{forwarded, `For="[2001:db8:cafe::17]:4711`, `[2001:db8:cafe::17]`},           // IPv6 address
    69  		{forwarded, `for=192.0.2.60;proto=http;by=203.0.113.43`, `192.0.2.60`},        // Multiple params
    70  		{forwarded, `for=192.0.2.43, for=198.51.100.17`, "192.0.2.43"},                // Multiple params
    71  		{forwarded, `for="workstation.local",for=198.51.100.17`, "workstation.local"}, // Hostname
    72  	}
    73  
    74  	for _, v := range headers {
    75  		req := &http.Request{
    76  			Header: http.Header{
    77  				v.key: []string{v.val},
    78  			},
    79  		}
    80  		res := GetSourceIP(req)
    81  		if res != v.expected {
    82  			t.Errorf("wrong header for %s: got %s want %s", v.key, res,
    83  				v.expected)
    84  		}
    85  	}
    86  }