storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/handlers/proxy_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2018 MinIO, Inc.
     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 handlers
    18  
    19  import (
    20  	"net/http"
    21  	"testing"
    22  )
    23  
    24  type headerTest struct {
    25  	key      string // header key
    26  	val      string // header val
    27  	expected string // expected result
    28  }
    29  
    30  func TestGetScheme(t *testing.T) {
    31  	headers := []headerTest{
    32  		{xForwardedProto, "https", "https"},
    33  		{xForwardedProto, "http", "http"},
    34  		{xForwardedProto, "HTTP", "http"},
    35  		{xForwardedScheme, "https", "https"},
    36  		{xForwardedScheme, "http", "http"},
    37  		{xForwardedScheme, "HTTP", "http"},
    38  		{forwarded, `For="[2001:db8:cafe::17]:4711`, ""},                    // No proto
    39  		{forwarded, `for=192.0.2.43, for=198.51.100.17;proto=https`, ""},    // Multiple params, will be empty.
    40  		{forwarded, `for=172.32.10.15; proto=https;by=127.0.0.1;`, "https"}, // Space before proto
    41  		{forwarded, `for=192.0.2.60;proto=http;by=203.0.113.43`, "http"},    // Multiple params
    42  	}
    43  	for _, v := range headers {
    44  		req := &http.Request{
    45  			Header: http.Header{
    46  				v.key: []string{v.val},
    47  			}}
    48  		res := GetSourceScheme(req)
    49  		if res != v.expected {
    50  			t.Errorf("wrong header for %s: got %s want %s", v.key, res,
    51  				v.expected)
    52  		}
    53  	}
    54  }
    55  
    56  // TestGetSourceIP - check the source ip of a request is parsed correctly.
    57  func TestGetSourceIP(t *testing.T) {
    58  	headers := []headerTest{
    59  		{xForwardedFor, "8.8.8.8", "8.8.8.8"},                                         // Single address
    60  		{xForwardedFor, "8.8.8.8, 8.8.4.4", "8.8.8.8"},                                // Multiple
    61  		{xForwardedFor, "", ""},                                                       // None
    62  		{xRealIP, "8.8.8.8", "8.8.8.8"},                                               // Single address
    63  		{xRealIP, "[2001:db8:cafe::17]:4711", "[2001:db8:cafe::17]:4711"},             // IPv6 address
    64  		{xRealIP, "", ""},                                                             // None
    65  		{forwarded, `for="_gazonk"`, "_gazonk"},                                       // Hostname
    66  		{forwarded, `For="[2001:db8:cafe::17]:4711`, `[2001:db8:cafe::17]:4711`},      // IPv6 address
    67  		{forwarded, `for=192.0.2.60;proto=http;by=203.0.113.43`, `192.0.2.60`},        // Multiple params
    68  		{forwarded, `for=192.0.2.43, for=198.51.100.17`, "192.0.2.43"},                // Multiple params
    69  		{forwarded, `for="workstation.local",for=198.51.100.17`, "workstation.local"}, // Hostname
    70  	}
    71  
    72  	for _, v := range headers {
    73  		req := &http.Request{
    74  			Header: http.Header{
    75  				v.key: []string{v.val},
    76  			}}
    77  		res := GetSourceIP(req)
    78  		if res != v.expected {
    79  			t.Errorf("wrong header for %s: got %s want %s", v.key, res,
    80  				v.expected)
    81  		}
    82  	}
    83  }