code.vegaprotocol.io/vega@v0.79.0/libs/http/cors_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package http
    17  
    18  import "testing"
    19  
    20  func TestAllowedOrigin(t *testing.T) {
    21  	t.Parallel()
    22  	tests := []struct {
    23  		name           string
    24  		allowedOrigins []string
    25  		origin         string
    26  		want           bool
    27  	}{
    28  		{
    29  			name:           "no allowed origins",
    30  			allowedOrigins: nil,
    31  			origin:         "http://example.com",
    32  			want:           true,
    33  		}, {
    34  			name:           "empty allowed origins",
    35  			allowedOrigins: []string{},
    36  			origin:         "http://example.com",
    37  			want:           true,
    38  		}, {
    39  			name:           "wildcard",
    40  			allowedOrigins: []string{"*"},
    41  			origin:         "http://example.com",
    42  			want:           true,
    43  		}, {
    44  			name:           "match",
    45  			allowedOrigins: []string{"http://example.com"},
    46  			origin:         "http://example.com",
    47  			want:           true,
    48  		}, {
    49  			name:           "match with scheme",
    50  			allowedOrigins: []string{"http://example.com"},
    51  			origin:         "https://example.com",
    52  			want:           true,
    53  		}, {
    54  			name:           "no match",
    55  			allowedOrigins: []string{"http://example.com"},
    56  			origin:         "http://not-example.com",
    57  			want:           false,
    58  		}, {
    59  			name:           "match with scheme",
    60  			allowedOrigins: []string{"https://example.com"},
    61  			origin:         "http://example.com",
    62  			want:           true,
    63  		}, {
    64  			name:           "no match with scheme",
    65  			allowedOrigins: []string{"https://example.com"},
    66  			origin:         "http://not-example.com",
    67  			want:           false,
    68  		},
    69  	}
    70  	for _, tt := range tests {
    71  		tt := tt
    72  		t.Run(tt.name, func(t *testing.T) {
    73  			t.Parallel()
    74  			if got := AllowedOrigin(tt.allowedOrigins)(tt.origin); got != tt.want {
    75  				t.Errorf("AllowedOrigin() = %v, want %v", got, tt.want)
    76  			}
    77  		})
    78  	}
    79  }