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

     1  /*
     2  Copyright (c) 2020 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  // This file contains tests for the alternative URL support.
    18  
    19  package sdk
    20  
    21  import (
    22  	"net/http"
    23  	"os"
    24  	"time"
    25  
    26  	. "github.com/onsi/ginkgo/v2/dsl/core" // nolint
    27  	. "github.com/onsi/gomega"             // nolint
    28  
    29  	"github.com/onsi/gomega/ghttp"
    30  
    31  	. "github.com/openshift-online/ocm-sdk-go/testing" // nolint
    32  )
    33  
    34  var _ = Describe("Alternative URLs", func() {
    35  	// Tokens used during the tests:
    36  	var accessToken string
    37  	var refreshToken string
    38  
    39  	// Servers used during the tests:
    40  	var oidServer *ghttp.Server
    41  	var defaultServer *ghttp.Server
    42  	var alternativeServer *ghttp.Server
    43  
    44  	// Names of the temporary files containing the CAs for the servers:
    45  	var oidCA string
    46  	var defaultCA string
    47  	var alternativeCA string
    48  
    49  	// URLs of the servers:
    50  	var oidURL string
    51  	var defaultURL string
    52  	var alternativeURL string
    53  
    54  	BeforeEach(func() {
    55  		// Create the tokens:
    56  		accessToken = MakeTokenString("Bearer", 5*time.Minute)
    57  		refreshToken = MakeTokenString("Refresh", 10*time.Hour)
    58  
    59  		// Create the OpenID server:
    60  		oidServer, oidCA = MakeTCPTLSServer()
    61  		oidServer.AppendHandlers(
    62  			ghttp.CombineHandlers(
    63  				RespondWithAccessAndRefreshTokens(accessToken, refreshToken),
    64  			),
    65  		)
    66  		oidURL = oidServer.URL()
    67  
    68  		// Create the API servers:
    69  		defaultServer, defaultCA = MakeTCPTLSServer()
    70  		defaultURL = defaultServer.URL()
    71  		alternativeServer, alternativeCA = MakeTCPTLSServer()
    72  		alternativeURL = alternativeServer.URL()
    73  	})
    74  
    75  	AfterEach(func() {
    76  		// Stop the servers:
    77  		oidServer.Close()
    78  		defaultServer.Close()
    79  		alternativeServer.Close()
    80  
    81  		// Remove the temporary CA files:
    82  		err := os.Remove(oidCA)
    83  		Expect(err).ToNot(HaveOccurred())
    84  		err = os.Remove(defaultCA)
    85  		Expect(err).ToNot(HaveOccurred())
    86  		err = os.Remove(alternativeCA)
    87  		Expect(err).ToNot(HaveOccurred())
    88  	})
    89  
    90  	Describe("Untyped get", func() {
    91  		It("Honours alternative URL", func() {
    92  			// Configure the alternative server so that it verifies that the request
    93  			// is sent:
    94  			alternativeServer.AppendHandlers(
    95  				ghttp.CombineHandlers(
    96  					ghttp.VerifyRequest(http.MethodGet, "/api/clusters_mgmt"),
    97  					RespondWithJSON(http.StatusOK, "{}"),
    98  				),
    99  			)
   100  
   101  			// Create the connection:
   102  			connection, err := NewConnectionBuilder().
   103  				Logger(logger).
   104  				TokenURL(oidURL).
   105  				Tokens(accessToken, refreshToken).
   106  				URL(defaultURL).
   107  				TrustedCAFile(oidCA).
   108  				TrustedCAFile(defaultCA).
   109  				TrustedCAFile(alternativeCA).
   110  				AlternativeURL("/api/clusters_mgmt", alternativeURL).
   111  				Build()
   112  			Expect(err).ToNot(HaveOccurred())
   113  			defer func() {
   114  				err = connection.Close()
   115  				Expect(err).ToNot(HaveOccurred())
   116  			}()
   117  
   118  			// Send the request:
   119  			_, err = connection.Get().
   120  				Path("/api/clusters_mgmt").
   121  				Send()
   122  			Expect(err).ToNot(HaveOccurred())
   123  		})
   124  
   125  		It("Uses default URL", func() {
   126  			// Configure the default server so that it verifies that the request
   127  			// is sent:
   128  			defaultServer.AppendHandlers(
   129  				ghttp.CombineHandlers(
   130  					ghttp.VerifyRequest(http.MethodGet, "/api/clusters_mgmt"),
   131  					RespondWithJSON(http.StatusOK, "{}"),
   132  				),
   133  			)
   134  
   135  			// Create the connection:
   136  			connection, err := NewConnectionBuilder().
   137  				Logger(logger).
   138  				TokenURL(oidURL).
   139  				Tokens(accessToken, refreshToken).
   140  				URL(defaultURL).
   141  				TrustedCAFile(oidCA).
   142  				TrustedCAFile(defaultCA).
   143  				TrustedCAFile(alternativeCA).
   144  				AlternativeURL("/api/accounts_mgmt", alternativeURL).
   145  				Build()
   146  			Expect(err).ToNot(HaveOccurred())
   147  			defer func() {
   148  				err = connection.Close()
   149  				Expect(err).ToNot(HaveOccurred())
   150  			}()
   151  
   152  			// Send the request:
   153  			_, err = connection.Get().
   154  				Path("/api/clusters_mgmt").
   155  				Send()
   156  			Expect(err).ToNot(HaveOccurred())
   157  		})
   158  
   159  		It("Uses most specific alternative URL", func() {
   160  			// Configure the default server so that it verifies that the request
   161  			// is sent:
   162  			alternativeServer.AppendHandlers(
   163  				ghttp.CombineHandlers(
   164  					ghttp.VerifyRequest(http.MethodGet, "/api/clusters_mgmt/v1"),
   165  					RespondWithJSON(http.StatusOK, "{}"),
   166  				),
   167  			)
   168  
   169  			// Create the connection:
   170  			connection, err := NewConnectionBuilder().
   171  				Logger(logger).
   172  				TokenURL(oidURL).
   173  				Tokens(accessToken, refreshToken).
   174  				URL(defaultURL).
   175  				TrustedCAFile(oidCA).
   176  				TrustedCAFile(defaultCA).
   177  				TrustedCAFile(alternativeCA).
   178  				AlternativeURL("/api/clusters_mgmt", defaultURL).
   179  				AlternativeURL("/api/clusters_mgmt/v1", alternativeURL).
   180  				Build()
   181  			Expect(err).ToNot(HaveOccurred())
   182  			defer func() {
   183  				err = connection.Close()
   184  				Expect(err).ToNot(HaveOccurred())
   185  			}()
   186  
   187  			// Send the request:
   188  			_, err = connection.Get().
   189  				Path("/api/clusters_mgmt/v1").
   190  				Send()
   191  			Expect(err).ToNot(HaveOccurred())
   192  		})
   193  	})
   194  
   195  	Describe("Typed get", func() {
   196  		It("Honours alternative URL", func() {
   197  			// Configure the alternative server so that it verifies that the request
   198  			// is sent:
   199  			alternativeServer.AppendHandlers(
   200  				ghttp.CombineHandlers(
   201  					ghttp.VerifyRequest(http.MethodGet, "/api/clusters_mgmt/v1"),
   202  					RespondWithJSON(http.StatusOK, "{}"),
   203  				),
   204  			)
   205  
   206  			// Create the connection:
   207  			connection, err := NewConnectionBuilder().
   208  				Logger(logger).
   209  				TokenURL(oidURL).
   210  				Tokens(accessToken, refreshToken).
   211  				URL(defaultURL).
   212  				TrustedCAFile(oidCA).
   213  				TrustedCAFile(defaultCA).
   214  				TrustedCAFile(alternativeCA).
   215  				AlternativeURL("/api/clusters_mgmt", alternativeURL).
   216  				Build()
   217  			Expect(err).ToNot(HaveOccurred())
   218  			defer func() {
   219  				err = connection.Close()
   220  				Expect(err).ToNot(HaveOccurred())
   221  			}()
   222  
   223  			// Send the request:
   224  			_, err = connection.ClustersMgmt().V1().Get().Send()
   225  			Expect(err).ToNot(HaveOccurred())
   226  		})
   227  
   228  		It("Uses default URL", func() {
   229  			// Configure the default server so that it verifies that the request
   230  			// is sent:
   231  			defaultServer.AppendHandlers(
   232  				ghttp.CombineHandlers(
   233  					ghttp.VerifyRequest(http.MethodGet, "/api/clusters_mgmt/v1"),
   234  					RespondWithJSON(http.StatusOK, "{}"),
   235  				),
   236  			)
   237  
   238  			// Create the connection:
   239  			connection, err := NewConnectionBuilder().
   240  				Logger(logger).
   241  				TokenURL(oidURL).
   242  				Tokens(accessToken, refreshToken).
   243  				URL(defaultURL).
   244  				TrustedCAFile(oidCA).
   245  				TrustedCAFile(defaultCA).
   246  				TrustedCAFile(alternativeCA).
   247  				AlternativeURL("/api/accounts_mgmt", alternativeURL).
   248  				Build()
   249  			Expect(err).ToNot(HaveOccurred())
   250  			defer func() {
   251  				err = connection.Close()
   252  				Expect(err).ToNot(HaveOccurred())
   253  			}()
   254  
   255  			// Send the request:
   256  			_, err = connection.ClustersMgmt().V1().Get().Send()
   257  			Expect(err).ToNot(HaveOccurred())
   258  		})
   259  
   260  		It("Uses most specific alternative URL", func() {
   261  			// Configure the default server so that it verifies that the request
   262  			// is sent:
   263  			alternativeServer.AppendHandlers(
   264  				ghttp.CombineHandlers(
   265  					ghttp.VerifyRequest(http.MethodGet, "/api/clusters_mgmt/v1"),
   266  					RespondWithJSON(http.StatusOK, "{}"),
   267  				),
   268  			)
   269  
   270  			// Create the connection:
   271  			connection, err := NewConnectionBuilder().
   272  				Logger(logger).
   273  				TokenURL(oidURL).
   274  				Tokens(accessToken, refreshToken).
   275  				URL(defaultURL).
   276  				TrustedCAFile(oidCA).
   277  				TrustedCAFile(defaultCA).
   278  				TrustedCAFile(alternativeCA).
   279  				AlternativeURL("/api/clusters_mgmt", defaultURL).
   280  				AlternativeURL("/api/clusters_mgmt/v1", alternativeURL).
   281  				Build()
   282  			Expect(err).ToNot(HaveOccurred())
   283  			defer func() {
   284  				err = connection.Close()
   285  				Expect(err).ToNot(HaveOccurred())
   286  			}()
   287  
   288  			// Send the request:
   289  			_, err = connection.ClustersMgmt().V1().Get().Send()
   290  			Expect(err).ToNot(HaveOccurred())
   291  		})
   292  	})
   293  })