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

     1  /*
     2  Copyright (c) 2020 VMware, Inc. All Rights Reserved.
     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 rest_test
    18  
    19  import (
    20  	"context"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/vmware/govmomi/simulator"
    25  	"github.com/vmware/govmomi/vapi/rest"
    26  	"github.com/vmware/govmomi/vim25"
    27  )
    28  
    29  func TestResource_WithParam(t *testing.T) {
    30  	simulator.Test(func(ctx context.Context, vc *vim25.Client) {
    31  		c := rest.NewClient(vc)
    32  
    33  		url := c.Resource("api/some/resource").
    34  			WithParam("key1", "value1")
    35  		expectedPath := "api/some/resource?key1=value1"
    36  		if !strings.Contains(url.String(), expectedPath) {
    37  			t.Errorf("Param incorrectly added to resource, URL %q, expected path %q", url.String(), expectedPath)
    38  		}
    39  
    40  		url = url.WithParam("key2", "value2")
    41  		expectedPath = "api/some/resource?key1=value1&key2=value2"
    42  		if !strings.Contains(url.String(), expectedPath) {
    43  			t.Errorf("Param incorrectly updated on resource, URL %q, expected path %q", url.String(), expectedPath)
    44  		}
    45  
    46  		url = c.Resource("api/some/resource")
    47  		url = url.WithParam("names", "foo").WithParam("names", "bar")
    48  		expectedPath = "api/some/resource?names=foo&names=bar"
    49  		if !strings.Contains(url.String(), expectedPath) {
    50  			t.Errorf("Param incorrectly updated on resource, URL %q, expected path %q", url.String(), expectedPath)
    51  		}
    52  	})
    53  }
    54  
    55  func TestResource_WithPathEncodedParam(t *testing.T) {
    56  	simulator.Test(func(ctx context.Context, vc *vim25.Client) {
    57  		c := rest.NewClient(vc)
    58  
    59  		// path is correctly formatted when Path-Encoded param is first
    60  		url := c.Resource("api/some/resource").
    61  			WithPathEncodedParam("key1", "value 1")
    62  		expectedPath := "api/some/resource?key1=value%201"
    63  		if !strings.Contains(url.String(), expectedPath) {
    64  			t.Errorf("First path-encoded param incorrectly added to resource, URL %q, expected path %q", url.String(), expectedPath)
    65  		}
    66  
    67  		// path is correctly formatted when Path-Encoded param is last
    68  		url = c.Resource("api/some/resource").
    69  			WithParam("key1", "value 1").
    70  			WithPathEncodedParam("key2", "value 2")
    71  		expectedPath = "api/some/resource?key1=value+1&key2=value%202"
    72  		if !strings.Contains(url.String(), expectedPath) {
    73  			t.Errorf("Last path-encoded param incorrectly added to resource, URL %q, expected path %q", url.String(), expectedPath)
    74  		}
    75  
    76  		// if WithParam is used again, it will re-encode the Path-Encoded value
    77  		url = url.WithParam("key3", "value 3")
    78  		expectedPath = "api/some/resource?key1=value+1&key2=value+2&key3=value+3"
    79  		if !strings.Contains(url.String(), expectedPath) {
    80  			t.Errorf("Middle path-encoded param not endcoded as expected, URL %q, expected path %q", url.String(), expectedPath)
    81  		}
    82  
    83  	})
    84  }