github.com/vmware/govmomi@v0.51.0/vapi/rest/resource_test.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package rest_test
     6  
     7  import (
     8  	"context"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/vmware/govmomi/simulator"
    13  	"github.com/vmware/govmomi/vapi/rest"
    14  	"github.com/vmware/govmomi/vim25"
    15  )
    16  
    17  func TestResource_WithParam(t *testing.T) {
    18  	simulator.Test(func(ctx context.Context, vc *vim25.Client) {
    19  		c := rest.NewClient(vc)
    20  
    21  		url := c.Resource("api/some/resource").
    22  			WithParam("key1", "value1")
    23  		expectedPath := "api/some/resource?key1=value1"
    24  		if !strings.Contains(url.String(), expectedPath) {
    25  			t.Errorf("Param incorrectly added to resource, URL %q, expected path %q", url.String(), expectedPath)
    26  		}
    27  
    28  		url = url.WithParam("key2", "value2")
    29  		expectedPath = "api/some/resource?key1=value1&key2=value2"
    30  		if !strings.Contains(url.String(), expectedPath) {
    31  			t.Errorf("Param incorrectly updated on resource, URL %q, expected path %q", url.String(), expectedPath)
    32  		}
    33  
    34  		url = c.Resource("api/some/resource")
    35  		url = url.WithParam("names", "foo").WithParam("names", "bar")
    36  		expectedPath = "api/some/resource?names=foo&names=bar"
    37  		if !strings.Contains(url.String(), expectedPath) {
    38  			t.Errorf("Param incorrectly updated on resource, URL %q, expected path %q", url.String(), expectedPath)
    39  		}
    40  	})
    41  }
    42  
    43  func TestResource_WithPathEncodedParam(t *testing.T) {
    44  	simulator.Test(func(ctx context.Context, vc *vim25.Client) {
    45  		c := rest.NewClient(vc)
    46  
    47  		// path is correctly formatted when Path-Encoded param is first
    48  		url := c.Resource("api/some/resource").
    49  			WithPathEncodedParam("key1", "value 1")
    50  		expectedPath := "api/some/resource?key1=value%201"
    51  		if !strings.Contains(url.String(), expectedPath) {
    52  			t.Errorf("First path-encoded param incorrectly added to resource, URL %q, expected path %q", url.String(), expectedPath)
    53  		}
    54  
    55  		// path is correctly formatted when Path-Encoded param is last
    56  		url = c.Resource("api/some/resource").
    57  			WithParam("key1", "value 1").
    58  			WithPathEncodedParam("key2", "value 2")
    59  		expectedPath = "api/some/resource?key1=value+1&key2=value%202"
    60  		if !strings.Contains(url.String(), expectedPath) {
    61  			t.Errorf("Last path-encoded param incorrectly added to resource, URL %q, expected path %q", url.String(), expectedPath)
    62  		}
    63  
    64  		// if WithParam is used again, it will re-encode the Path-Encoded value
    65  		url = url.WithParam("key3", "value 3")
    66  		expectedPath = "api/some/resource?key1=value+1&key2=value+2&key3=value+3"
    67  		if !strings.Contains(url.String(), expectedPath) {
    68  			t.Errorf("Middle path-encoded param not endcoded as expected, URL %q, expected path %q", url.String(), expectedPath)
    69  		}
    70  
    71  	})
    72  }