github.com/vmware/govmomi@v0.51.0/vim25/methods/fault_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 methods
     6  
     7  import (
     8  	"bytes"
     9  	"testing"
    10  
    11  	"github.com/vmware/govmomi/vim25/soap"
    12  	"github.com/vmware/govmomi/vim25/types"
    13  	"github.com/vmware/govmomi/vim25/xml"
    14  )
    15  
    16  var invalidLoginFault = `
    17  <?xml version="1.0" encoding="UTF-8"?>
    18  <soapenv:Envelope xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    19   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    20   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    21   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    22  <soapenv:Body>
    23  <soapenv:Fault><faultcode>ServerFaultCode</faultcode><faultstring>Cannot complete login due to an incorrect user name or password.</faultstring><detail><InvalidLoginFault xmlns="urn:vim25" xsi:type="InvalidLogin"></InvalidLoginFault></detail></soapenv:Fault>
    24  </soapenv:Body>
    25  </soapenv:Envelope>`
    26  
    27  type TestBody struct {
    28  	Fault *soap.Fault `xml:"http://schemas.xmlsoap.org/soap/envelope/ Fault,omitempty"`
    29  }
    30  
    31  func TestFaultDetail(t *testing.T) {
    32  	body := TestBody{}
    33  	env := soap.Envelope{Body: &body}
    34  
    35  	dec := xml.NewDecoder(bytes.NewReader([]byte(invalidLoginFault)))
    36  	dec.TypeFunc = types.TypeFunc()
    37  
    38  	err := dec.Decode(&env)
    39  	if err != nil {
    40  		t.Fatalf("Decode: %s", err)
    41  	}
    42  
    43  	if body.Fault == nil {
    44  		t.Fatal("Expected fault")
    45  	}
    46  
    47  	if _, ok := body.Fault.Detail.Fault.(types.InvalidLogin); !ok {
    48  		t.Fatalf("Expected InvalidLogin, got: %#v", body.Fault.Detail.Fault)
    49  	}
    50  }