github.com/openshift-online/ocm-sdk-go@v0.1.473/internal/server_address_test.go (about)

     1  /*
     2  Copyright (c) 2021 Red Hat, 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 internal
    18  
    19  import (
    20  	"context"
    21  	"net/url"
    22  
    23  	. "github.com/onsi/ginkgo/v2/dsl/table" // nolint
    24  	. "github.com/onsi/gomega"              // nolint
    25  )
    26  
    27  var _ = DescribeTable(
    28  	"Parsing success",
    29  	func(input string, expected *ServerAddress) {
    30  		actual, err := ParseServerAddress(context.Background(), input)
    31  		Expect(err).ToNot(HaveOccurred())
    32  		Expect(actual.Text).To(Equal(input))
    33  		Expect(actual.Network).To(Equal(expected.Network))
    34  		Expect(actual.Protocol).To(Equal(expected.Protocol))
    35  		Expect(actual.Host).To(Equal(expected.Host))
    36  		Expect(actual.Port).To(Equal(expected.Port))
    37  		Expect(actual.Socket).To(Equal(expected.Socket))
    38  		Expect(actual.URL.String()).To(Equal(expected.URL.String()))
    39  	},
    40  	Entry(
    41  		"tcp",
    42  		"tcp://my.server.com",
    43  		&ServerAddress{
    44  			Network:  TCPNetwork,
    45  			Protocol: HTTPProtocol,
    46  			Host:     "my.server.com",
    47  			Port:     "80",
    48  			URL: &url.URL{
    49  				Scheme: "http",
    50  				Host:   "my.server.com",
    51  			},
    52  		},
    53  	),
    54  	Entry(
    55  		"tcp+http",
    56  		"tcp+http://my.server.com",
    57  		&ServerAddress{
    58  			Network:  TCPNetwork,
    59  			Protocol: HTTPProtocol,
    60  			Host:     "my.server.com",
    61  			Port:     "80",
    62  			URL: &url.URL{
    63  				Scheme: "http",
    64  				Host:   "my.server.com",
    65  			},
    66  		},
    67  	),
    68  	Entry(
    69  		"tcp+https",
    70  		"tcp+https://my.server.com",
    71  		&ServerAddress{
    72  			Network:  TCPNetwork,
    73  			Protocol: HTTPSProtocol,
    74  			Host:     "my.server.com",
    75  			Port:     "443",
    76  			URL: &url.URL{
    77  				Scheme: "https",
    78  				Host:   "my.server.com",
    79  			},
    80  		},
    81  	),
    82  	Entry(
    83  		"tcp+h2c",
    84  		"tcp+h2c://my.server.com",
    85  		&ServerAddress{
    86  			Network:  TCPNetwork,
    87  			Protocol: H2CProtocol,
    88  			Host:     "my.server.com",
    89  			Port:     "80",
    90  			URL: &url.URL{
    91  				Scheme: "http",
    92  				Host:   "my.server.com",
    93  			},
    94  		},
    95  	),
    96  	Entry(
    97  		"unix",
    98  		"unix://my.server.com/my.socket",
    99  		&ServerAddress{
   100  			Network:  UnixNetwork,
   101  			Protocol: HTTPProtocol,
   102  			Host:     "my.server.com",
   103  			Port:     "80",
   104  			URL: &url.URL{
   105  				Scheme: "http",
   106  				Host:   "my.server.com",
   107  			},
   108  			Socket: "/my.socket",
   109  		},
   110  	),
   111  	Entry(
   112  		"unix+http",
   113  		"unix+http://my.server.com/my.socket",
   114  		&ServerAddress{
   115  			Network:  UnixNetwork,
   116  			Protocol: HTTPProtocol,
   117  			Host:     "my.server.com",
   118  			Port:     "80",
   119  			Socket:   "/my.socket",
   120  			URL: &url.URL{
   121  				Scheme: "http",
   122  				Host:   "my.server.com",
   123  			},
   124  		},
   125  	),
   126  	Entry(
   127  		"unix+https",
   128  		"unix+https://my.server.com/my.socket",
   129  		&ServerAddress{
   130  			Network:  UnixNetwork,
   131  			Protocol: HTTPSProtocol,
   132  			Host:     "my.server.com",
   133  			Port:     "443",
   134  			Socket:   "/my.socket",
   135  			URL: &url.URL{
   136  				Scheme: "https",
   137  				Host:   "my.server.com",
   138  			},
   139  		},
   140  	),
   141  	Entry(
   142  		"unix+h2c",
   143  		"unix+h2c://my.server.com/my.socket",
   144  		&ServerAddress{
   145  			Network:  UnixNetwork,
   146  			Protocol: H2CProtocol,
   147  			Host:     "my.server.com",
   148  			Port:     "80",
   149  			Socket:   "/my.socket",
   150  			URL: &url.URL{
   151  				Scheme: "http",
   152  				Host:   "my.server.com",
   153  			},
   154  		},
   155  	),
   156  	Entry(
   157  		"http",
   158  		"http://my.server.com",
   159  		&ServerAddress{
   160  			Network:  TCPNetwork,
   161  			Protocol: HTTPProtocol,
   162  			Host:     "my.server.com",
   163  			Port:     "80",
   164  			URL: &url.URL{
   165  				Scheme: "http",
   166  				Host:   "my.server.com",
   167  			},
   168  		},
   169  	),
   170  	Entry(
   171  		"http+tcp",
   172  		"http+tcp://my.server.com",
   173  		&ServerAddress{
   174  			Network:  TCPNetwork,
   175  			Protocol: HTTPProtocol,
   176  			Host:     "my.server.com",
   177  			Port:     "80",
   178  			URL: &url.URL{
   179  				Scheme: "http",
   180  				Host:   "my.server.com",
   181  			},
   182  		},
   183  	),
   184  	Entry(
   185  		"http+unix",
   186  		"http+unix://my.server.com/my.socket",
   187  		&ServerAddress{
   188  			Network:  UnixNetwork,
   189  			Protocol: HTTPProtocol,
   190  			Host:     "my.server.com",
   191  			Port:     "80",
   192  			Socket:   "/my.socket",
   193  			URL: &url.URL{
   194  				Scheme: "http",
   195  				Host:   "my.server.com",
   196  				Path:   "",
   197  			},
   198  		},
   199  	),
   200  	Entry(
   201  		"https",
   202  		"https://my.server.com",
   203  		&ServerAddress{
   204  			Network:  TCPNetwork,
   205  			Protocol: HTTPSProtocol,
   206  			Host:     "my.server.com",
   207  			Port:     "443",
   208  			URL: &url.URL{
   209  				Scheme: "https",
   210  				Host:   "my.server.com",
   211  			},
   212  		},
   213  	),
   214  	Entry(
   215  		"https+tcp",
   216  		"https+tcp://my.server.com",
   217  		&ServerAddress{
   218  			Network:  TCPNetwork,
   219  			Protocol: HTTPSProtocol,
   220  			Host:     "my.server.com",
   221  			Port:     "443",
   222  			URL: &url.URL{
   223  				Scheme: "https",
   224  				Host:   "my.server.com",
   225  			},
   226  		},
   227  	),
   228  	Entry(
   229  		"https+unix",
   230  		"https+unix://my.server.com/my.socket",
   231  		&ServerAddress{
   232  			Network:  UnixNetwork,
   233  			Protocol: HTTPSProtocol,
   234  			Host:     "my.server.com",
   235  			Port:     "443",
   236  			Socket:   "/my.socket",
   237  			URL: &url.URL{
   238  				Scheme: "https",
   239  				Host:   "my.server.com",
   240  			},
   241  		},
   242  	),
   243  	Entry(
   244  		"h2c",
   245  		"h2c://my.server.com",
   246  		&ServerAddress{
   247  			Network:  TCPNetwork,
   248  			Protocol: H2CProtocol,
   249  			Host:     "my.server.com",
   250  			Port:     "80",
   251  			URL: &url.URL{
   252  				Scheme: "http",
   253  				Host:   "my.server.com",
   254  			},
   255  		},
   256  	),
   257  	Entry(
   258  		"h2c+tcp",
   259  		"h2c+tcp://my.server.com",
   260  		&ServerAddress{
   261  			Network:  TCPNetwork,
   262  			Protocol: H2CProtocol,
   263  			Host:     "my.server.com",
   264  			Port:     "80",
   265  			URL: &url.URL{
   266  				Scheme: "http",
   267  				Host:   "my.server.com",
   268  			},
   269  		},
   270  	),
   271  	Entry(
   272  		"h2c+unix",
   273  		"h2c+unix://my.server.com/my.socket",
   274  		&ServerAddress{
   275  			Network:  UnixNetwork,
   276  			Protocol: H2CProtocol,
   277  			Host:     "my.server.com",
   278  			Port:     "80",
   279  			Socket:   "/my.socket",
   280  			URL: &url.URL{
   281  				Scheme: "http",
   282  				Host:   "my.server.com",
   283  			},
   284  		},
   285  	),
   286  	Entry(
   287  		"Non default HTTP port",
   288  		"http://my.server.com:1080",
   289  		&ServerAddress{
   290  			Network:  TCPNetwork,
   291  			Protocol: HTTPProtocol,
   292  			Host:     "my.server.com",
   293  			Port:     "1080",
   294  			URL: &url.URL{
   295  				Scheme: "http",
   296  				Host:   "my.server.com:1080",
   297  			},
   298  		},
   299  	),
   300  	Entry(
   301  		"Non default HTTPS port",
   302  		"http://my.server.com:1443",
   303  		&ServerAddress{
   304  			Network:  TCPNetwork,
   305  			Protocol: HTTPProtocol,
   306  			Host:     "my.server.com",
   307  			Port:     "1443",
   308  			URL: &url.URL{
   309  				Scheme: "http",
   310  				Host:   "my.server.com:1443",
   311  			},
   312  		},
   313  	),
   314  	Entry(
   315  		"Non default H2C port",
   316  		"h2c://my.server.com:1080",
   317  		&ServerAddress{
   318  			Network:  TCPNetwork,
   319  			Protocol: H2CProtocol,
   320  			Host:     "my.server.com",
   321  			Port:     "1080",
   322  			URL: &url.URL{
   323  				Scheme: "http",
   324  				Host:   "my.server.com:1080",
   325  			},
   326  		},
   327  	),
   328  	Entry(
   329  		"Unix socket in query parameter",
   330  		"unix://my.server.com/my/path?socket=/my.socket",
   331  		&ServerAddress{
   332  			Network:  UnixNetwork,
   333  			Protocol: HTTPProtocol,
   334  			Host:     "my.server.com",
   335  			Port:     "80",
   336  			Socket:   "/my.socket",
   337  			URL: &url.URL{
   338  				Scheme: "http",
   339  				Host:   "my.server.com",
   340  			},
   341  		},
   342  	),
   343  	Entry(
   344  		"TCP network from query parameter",
   345  		"http://my.server.com?network=tcp",
   346  		&ServerAddress{
   347  			Network:  TCPNetwork,
   348  			Protocol: HTTPProtocol,
   349  			Host:     "my.server.com",
   350  			Port:     "80",
   351  			URL: &url.URL{
   352  				Scheme: "http",
   353  				Host:   "my.server.com",
   354  			},
   355  		},
   356  	),
   357  	Entry(
   358  		"Unix network from query parameter",
   359  		"http://my.server.com/my/path?network=unix&socket=/my.socket",
   360  		&ServerAddress{
   361  			Network:  UnixNetwork,
   362  			Protocol: HTTPProtocol,
   363  			Host:     "my.server.com",
   364  			Port:     "80",
   365  			Socket:   "/my.socket",
   366  			URL: &url.URL{
   367  				Scheme: "http",
   368  				Host:   "my.server.com",
   369  			},
   370  		},
   371  	),
   372  	Entry(
   373  		"H2C protocol from query parameter",
   374  		"tcp://my.server.com?protocol=h2c",
   375  		&ServerAddress{
   376  			Network:  TCPNetwork,
   377  			Protocol: H2CProtocol,
   378  			Host:     "my.server.com",
   379  			Port:     "80",
   380  			URL: &url.URL{
   381  				Scheme: "http",
   382  				Host:   "my.server.com",
   383  			},
   384  		},
   385  	),
   386  )
   387  
   388  var _ = DescribeTable(
   389  	"Parsing error",
   390  	func(input string, expected ...string) {
   391  		actual, err := ParseServerAddress(context.Background(), input)
   392  		Expect(err).To(HaveOccurred())
   393  		Expect(actual).To(BeNil())
   394  		message := err.Error()
   395  		for _, substring := range expected {
   396  			Expect(message).To(ContainSubstring(substring))
   397  		}
   398  	},
   399  	Entry(
   400  		"Unknonwn network",
   401  		"mynet+http://my.server.com",
   402  		"component 'mynet' of scheme 'mynet+http' doesn't correspond to any supported "+
   403  			"network or protocol",
   404  		"supported networks are 'tcp' and 'unix'",
   405  	),
   406  	Entry(
   407  		"Unknonwn protocol",
   408  		"tcp+myprotocol://my.server.com",
   409  		"component 'myprotocol' of scheme 'tcp+myprotocol' doesn't correspond to any "+
   410  			"supported network or protocol",
   411  		"supported protocols are 'http', 'https' and 'h2c'",
   412  	),
   413  	Entry(
   414  		"Missing Unix socket",
   415  		"unix://my.server.com",
   416  		"expected socket name in the 'socket' query parameter or in the path but both "+
   417  			"are empty",
   418  	),
   419  	Entry(
   420  		"Incompatible network from query parameter",
   421  		"unix://my.server.com/my.socket?network=tcp",
   422  		"network 'tcp' from query parameter isn't compatible with network 'unix' "+
   423  			"from scheme",
   424  	),
   425  	Entry(
   426  		"Invalid network from query parameter",
   427  		"http://my.server.com/my.socket?network=unox",
   428  		"network 'unox' isn't valid, valid values are 'unix' and 'tcp'",
   429  	),
   430  	Entry(
   431  		"Invalid protocol from query parameter",
   432  		"tcp://my.server.com/my.socket?protocol=h2d",
   433  		"protocol 'h2d' isn't valid, valid values are 'http', 'https' and 'h2c'",
   434  	),
   435  )