github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6@v6.2.0/fake_example_test.go (about)

     1  //go:build go1.18
     2  // +build go1.18
     3  
     4  // Copyright (c) Microsoft Corporation. All rights reserved.
     5  // Licensed under the MIT License. See License.txt in the project root for license information.
     6  
     7  package armnetwork_test
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  	"log"
    13  	"net/http"
    14  
    15  	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
    16  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
    17  	azfake "github.com/Azure/azure-sdk-for-go/sdk/azcore/fake"
    18  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
    19  	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6"
    20  	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6/fake"
    21  )
    22  
    23  func ExampleInterfacesServer() {
    24  	// first, create an instance of the fake server for the client you wish to test.
    25  	// the type name of the server will be similar to the corresponding client, with
    26  	// the suffix "Server" instead of "Client".
    27  	fakeInterfacesServer := fake.InterfacesServer{
    28  
    29  		// next, provide implementations for the APIs you wish to fake.
    30  		// this fake corresponds to the InterfacesClient.Get() API.
    31  		Get: func(ctx context.Context, resourceGroupName string, networkInterfaceName string, options *armnetwork.InterfacesClientGetOptions) (resp azfake.Responder[armnetwork.InterfacesClientGetResponse], errResp azfake.ErrorResponder) {
    32  			// the values of ctx, resourceGroupName, vmName, and options come from the API call.
    33  
    34  			// the named return values resp and errResp are used to construct the response
    35  			// and are meant to be mutually exclusive. if both responses have been constructed,
    36  			// the error response is selected.
    37  
    38  			// construct the response type, populating fields as required
    39  			interfaceResp := armnetwork.InterfacesClientGetResponse{}
    40  			interfaceResp.ID = to.Ptr("/fake/resource/id")
    41  
    42  			// use resp to set the desired response
    43  			resp.SetResponse(http.StatusOK, interfaceResp, nil)
    44  
    45  			// to simulate the failure case, use errResp
    46  			// errResp.SetResponseError(http.StatusBadRequest, "ThisIsASimulatedError")
    47  
    48  			return
    49  		},
    50  	}
    51  
    52  	// now create the corresponding client, connecting the fake server via the client options
    53  	client, err := armnetwork.NewInterfacesClient("subscriptionID", &azfake.TokenCredential{}, &arm.ClientOptions{
    54  		ClientOptions: azcore.ClientOptions{
    55  			Transport: fake.NewInterfacesServerTransport(&fakeInterfacesServer),
    56  		},
    57  	})
    58  	if err != nil {
    59  		log.Fatal(err)
    60  	}
    61  
    62  	// call the API. the provided values will be passed to the fake's implementation.
    63  	// the response or error values returned by the API call are from the fake.
    64  	resp, err := client.Get(context.TODO(), "fakeResourceGroup", "fakeInterface", nil)
    65  	if err != nil {
    66  		log.Fatal(err)
    67  	}
    68  
    69  	fmt.Println(*resp.ID)
    70  
    71  	// APIs that haven't been faked will return an error
    72  	_, err = client.BeginDelete(context.TODO(), "fakeResourceGroup", "fakeInterface", nil)
    73  
    74  	fmt.Println(err.Error())
    75  
    76  	// Output:
    77  	// /fake/resource/id
    78  	// fake for method BeginDelete not implemented
    79  }