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

     1  /*
     2  Copyright (c) 2019 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  	"bytes"
    21  	"context"
    22  	"io"
    23  	"net/http"
    24  	"strings"
    25  	"testing"
    26  
    27  	"github.com/vmware/govmomi/simulator"
    28  	"github.com/vmware/govmomi/vapi/internal"
    29  	"github.com/vmware/govmomi/vapi/rest"
    30  	_ "github.com/vmware/govmomi/vapi/simulator"
    31  	"github.com/vmware/govmomi/vim25"
    32  )
    33  
    34  func TestSession(t *testing.T) {
    35  	simulator.Test(func(ctx context.Context, vc *vim25.Client) {
    36  		c := rest.NewClient(vc)
    37  
    38  		session, err := c.Session(ctx)
    39  		if err != nil {
    40  			t.Fatal(err)
    41  		}
    42  
    43  		if session != nil {
    44  			t.Fatal("expected nil session")
    45  		}
    46  
    47  		err = c.Login(ctx, simulator.DefaultLogin)
    48  		if err != nil {
    49  			t.Fatal(err)
    50  		}
    51  
    52  		session, err = c.Session(ctx)
    53  		if err != nil {
    54  			t.Fatal(err)
    55  		}
    56  
    57  		if session == nil {
    58  			t.Fatal("expected non-nil session")
    59  		}
    60  
    61  		path := c.Resource("/xpto/bla")
    62  		err = c.Do(ctx, path.Request(http.MethodGet), nil)
    63  		if !rest.IsStatusError(err, http.StatusNotFound) {
    64  			t.Fatal("expecting error to be 'StatusNotFound'", err)
    65  		}
    66  	})
    67  }
    68  
    69  func TestWithHeaders(t *testing.T) {
    70  	simulator.Test(func(ctx context.Context, vc *vim25.Client) {
    71  		c := rest.NewClient(vc)
    72  		if err := c.Login(ctx, simulator.DefaultLogin); err != nil {
    73  			t.Fatal(err)
    74  		}
    75  
    76  		// Assign the headers.
    77  		ctx = c.WithHeader(ctx, http.Header{
    78  			"client-token": []string{"unique-token"},
    79  			"pid":          []string{"2", "3", "4"},
    80  		})
    81  
    82  		req, err := http.NewRequest(
    83  			http.MethodPost,
    84  			c.Resource(internal.DebugEcho).String(),
    85  			strings.NewReader("Hello, world."))
    86  		if err != nil {
    87  			t.Fatal(err)
    88  		}
    89  
    90  		// Send a rest.RawResponse into the decoder to capture the raw
    91  		// response data.
    92  		var res rest.RawResponse
    93  		if err := c.Do(ctx, req, &res); err != nil {
    94  			t.Fatal(err)
    95  		}
    96  
    97  		// Read the raw response.
    98  		data, err := io.ReadAll(&res.Buffer)
    99  		if err != nil {
   100  			t.Fatal(err)
   101  		}
   102  
   103  		// Assert all of the request headers were echoed back to the client.
   104  		if !bytes.Contains(data, []byte("Client-Token: unique-token")) {
   105  			t.Fatal("missing Client-Token: unique-token")
   106  		}
   107  		if !bytes.Contains(data, []byte("Pid: 2")) {
   108  			t.Fatal("missing Pid: 2")
   109  		}
   110  		if !bytes.Contains(data, []byte("Pid: 3")) {
   111  			t.Fatal("missing Pid: 3")
   112  		}
   113  		if !bytes.Contains(data, []byte("Pid: 4")) {
   114  			t.Fatal("missing Pid: 4")
   115  		}
   116  	})
   117  }