github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/destinationfetchersvc/destinationservice_test.go (about)

     1  package destinationfetchersvc_test
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  const (
    16  	exampleDestination1 = `{
    17          "Name": "mys4_1",
    18          "Type": "HTTP",
    19          "URL": "https://my54321-api.s4.com:443",
    20          "Authentication": "BasicAuthentication",
    21          "ProxyType": "Internet",
    22          "XFSystemName": "Test S4HANA system",
    23          "HTML5.DynamicDestination": "true",
    24          "User": "SOME_USER",
    25          "product.name": "SAP S/4HANA Cloud",
    26          "WebIDEEnabled": "true",
    27          "communicationScenarioId": "SAP_COM_0108",
    28          "Password": "SecretPassword",
    29          "WebIDEUsage": "odata_gen"
    30      }`
    31  	exampleDestination2 = `{
    32          "Name": "mysystem_2",
    33          "Type": "HTTP",
    34          "URL": "https://mysystem.com",
    35          "Authentication": "BasicAuthentication",
    36          "ProxyType": "Internet",
    37          "HTML5.DynamicDestination": "true",
    38          "User": "SOME_USER",
    39          "Password": "SecretPassword",
    40  		"x-system-id": "system-id",
    41  		"x-system-type": "mysystem"
    42      }`
    43  )
    44  
    45  type destinationHandler struct {
    46  	customTenantDestinationHandler func(w http.ResponseWriter, req *http.Request)
    47  	t                              *testing.T
    48  }
    49  
    50  func (dh *destinationHandler) mux() *http.ServeMux {
    51  	mux := http.NewServeMux()
    52  	mux.HandleFunc("/subaccountDestinations", dh.tenantDestinationHandler)
    53  	mux.HandleFunc("/destinations/", dh.fetchDestinationHandler)
    54  	mux.HandleFunc("/oauth/token", dh.tokenHandler)
    55  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    56  		dh.t.Logf("Unhandled request to mocked destination service %s", r.URL.String())
    57  		w.WriteHeader(http.StatusInternalServerError)
    58  	})
    59  	return mux
    60  }
    61  
    62  func (dh *destinationHandler) tenantDestinationHandler(w http.ResponseWriter, req *http.Request) {
    63  	if dh.customTenantDestinationHandler != nil {
    64  		dh.customTenantDestinationHandler(w, req)
    65  		return
    66  	}
    67  	query := req.URL.Query()
    68  	page := query.Get("$page")
    69  
    70  	w.Header().Set("Content-Type", "application/json")
    71  	// assuming pageSize is always 1
    72  	var response []byte
    73  	switch page {
    74  	case "1":
    75  		response = []byte(fmt.Sprintf("[%s]", exampleDestination1))
    76  	case "2":
    77  		response = []byte(fmt.Sprintf("[%s]", exampleDestination2))
    78  	default:
    79  		dh.t.Logf("Expected page size to be 1 or 2, got '%s'", page)
    80  		w.WriteHeader(http.StatusBadRequest)
    81  		return
    82  	}
    83  	w.Header().Set("Page-Count", "2")
    84  	_, err := w.Write(response)
    85  	assert.NoError(dh.t, err)
    86  }
    87  
    88  var defaultDestinations = map[string]string{
    89  	"dest1": `{"name": "dest1", "destinationConfiguration": {}}`,
    90  	"dest2": `{"name": "dest2", "destinationConfiguration": {}}`,
    91  }
    92  
    93  func (dh *destinationHandler) fetchDestinationHandler(w http.ResponseWriter, req *http.Request) {
    94  	path := req.URL.Path
    95  	w.Header().Set("Content-Type", "application/json")
    96  	for destinationName, destination := range defaultDestinations {
    97  		if strings.HasSuffix(path, "/"+destinationName) {
    98  			_, err := w.Write([]byte(destination))
    99  			assert.NoError(dh.t, err)
   100  			return
   101  		}
   102  	}
   103  	w.WriteHeader(http.StatusNotFound)
   104  }
   105  
   106  func (dh *destinationHandler) tokenHandler(w http.ResponseWriter, _ *http.Request) {
   107  	w.Header().Set("Content-Type", "application/json")
   108  	_, err := w.Write([]byte(`{
   109  			"access_token": "accesstoken",
   110  			"token_type": "tokentype",
   111  			"refresh_token": "refreshtoken",
   112  			"expires_in": 100
   113  		}`))
   114  	assert.NoError(dh.t, err)
   115  }
   116  
   117  type destinationServer struct {
   118  	server  *httptest.Server
   119  	handler *destinationHandler
   120  }
   121  
   122  func newDestinationServer(t *testing.T) destinationServer {
   123  	destinationHandler := &destinationHandler{t: t}
   124  	httpServer := httptest.NewUnstartedServer(destinationHandler.mux())
   125  	var err error
   126  	httpServer.Listener, err = net.Listen("tcp", "127.0.0.1:0")
   127  	require.NoError(t, err)
   128  	return destinationServer{
   129  		server:  httpServer,
   130  		handler: destinationHandler,
   131  	}
   132  }