package fake

import "github.com/Azure/azure-sdk-for-go/sdk/azcore/fake"

Package fake provides the building blocks for fake servers. This includes fakes for authentication, API responses, and more.

Index

Examples

Types

type AddNonTerminalResponseOptions

type AddNonTerminalResponseOptions = exported.AddNonTerminalResponseOptions

AddNonTerminalResponseOptions contains the optional values for PollerResponder[T].AddNonTerminalResponse.

type AddPageOptions

type AddPageOptions = exported.AddPageOptions

AddPageOptions contains the optional values for PagerResponder[T].AddPage.

type ErrorResponder

type ErrorResponder exported.ErrorResponder

ErrorResponder represents a scalar error response.

Example

Code:play 

package main

import (
	"errors"
	"net/http"

	"github.com/Azure/azure-sdk-for-go/sdk/azcore/fake"
)

func main() {
	// an ErrorResponder is used to build an error response
	errResp := fake.ErrorResponder{}

	// use SetError to return a generic error
	errResp.SetError(errors.New("the system is down"))

	// to return an *azcore.ResponseError, use SetResponseError
	errResp.SetResponseError(http.StatusConflict, "ErrorCodeConflict")

	// ErrorResponder returns a singular error, so calling Set* APIs overwrites any previous value
}

func (*ErrorResponder) SetError

func (e *ErrorResponder) SetError(err error)

SetError sets the specified error to be returned. Use SetResponseError for returning an *azcore.ResponseError.

func (*ErrorResponder) SetResponseError

func (e *ErrorResponder) SetResponseError(httpStatus int, errorCode string)

SetResponseError sets an *azcore.ResponseError with the specified values to be returned.

type PagerResponder

type PagerResponder[T any] exported.PagerResponder[T]

PagerResponder represents a sequence of paged responses. Responses are replayed in the order in which they were added.

Example

Code:play 

package main

import (
	"errors"
	"net/http"

	"github.com/Azure/azure-sdk-for-go/sdk/azcore/fake"
)

// Widget is a hypothetical type used in the following examples.
type Widget struct {
	ID    int
	Shape string
}

// WidgetListResponse is a hypothetical type used in the following examples.
type WidgetListResponse struct {
	Widgets []Widget
}

func main() {
	// for a hypothetical API NewListWidgetsPager() *runtime.Pager[WidgetListResponse]

	// a PagerResponder is used to build a sequence of responses for a paged operation
	pagerResp := fake.PagerResponder[WidgetListResponse]{}

	// use AddPage to add one or more pages to the response.
	// responses are returned in the order in which they were added.
	pagerResp.AddPage(http.StatusOK, WidgetListResponse{
		Widgets: []Widget{
			{ID: 1, Shape: "circle"},
			{ID: 2, Shape: "square"},
			{ID: 3, Shape: "triangle"},
		},
	}, nil)
	pagerResp.AddPage(http.StatusOK, WidgetListResponse{
		Widgets: []Widget{
			{ID: 4, Shape: "rectangle"},
			{ID: 5, Shape: "rhombus"},
		},
	}, nil)

	// errors can also be included in the sequence of responses.
	// this can be used to simulate an error during paging.
	pagerResp.AddError(errors.New("network too slow"))

	pagerResp.AddPage(http.StatusOK, WidgetListResponse{
		Widgets: []Widget{
			{ID: 6, Shape: "trapezoid"},
		},
	}, nil)
}

func (*PagerResponder[T]) AddError

func (p *PagerResponder[T]) AddError(err error)

AddError adds an error to the sequence of responses. The error is returned from the call to runtime.Pager[T].NextPage().

func (*PagerResponder[T]) AddPage

func (p *PagerResponder[T]) AddPage(httpStatus int, page T, o *AddPageOptions)

AddPage adds a page to the sequence of respones.

func (*PagerResponder[T]) AddResponseError

func (p *PagerResponder[T]) AddResponseError(httpStatus int, errorCode string)

AddResponseError adds an *azcore.ResponseError to the sequence of responses. The error is returned from the call to runtime.Pager[T].NextPage().

type PollerResponder

type PollerResponder[T any] exported.PollerResponder[T]

PollerResponder represents a sequence of responses for a long-running operation. Any non-terminal responses are replayed in the order in which they were added. The terminal response, success or error, is always the final response.

Example

Code:play 

package main

import (
	"errors"
	"net/http"

	"github.com/Azure/azure-sdk-for-go/sdk/azcore/fake"
)

// Widget is a hypothetical type used in the following examples.
type Widget struct {
	ID    int
	Shape string
}

// WidgetResponse is a hypothetical type used in the following examples.
type WidgetResponse struct {
	Widget
}

func main() {
	// for a hypothetical API BeginCreateWidget(context.Context) (*runtime.Poller[WidgetResponse], error)

	// a PollerResponder is used to build a sequence of responses for a long-running operation
	pollerResp := fake.PollerResponder[WidgetResponse]{}

	// use AddNonTerminalResponse to add one or more non-terminal responses
	// to the sequence of responses. this is to simulate polling on a LRO.
	// non-terminal responses are optional. exclude them to simulate a LRO
	// that synchronously completes.
	pollerResp.AddNonTerminalResponse(http.StatusOK, nil)

	// non-terminal errors can also be included in the sequence of responses.
	// use this to simulate an error during polling.
	pollerResp.AddPollingError(errors.New("flaky network"))

	// use SetTerminalResponse to successfully terminate the long-running operation.
	// the provided value will be returned as the terminal response.
	pollerResp.SetTerminalResponse(http.StatusOK, WidgetResponse{
		Widget: Widget{
			ID:    987,
			Shape: "dodecahedron",
		},
	}, nil)
}

func (*PollerResponder[T]) AddNonTerminalResponse

func (p *PollerResponder[T]) AddNonTerminalResponse(httpStatus int, o *AddNonTerminalResponseOptions)

AddNonTerminalResponse adds a non-terminal response to the sequence of responses.

func (*PollerResponder[T]) AddPollingError

func (p *PollerResponder[T]) AddPollingError(err error)

AddPollingError adds an error to the sequence of responses. Use this to simulate an error durring polling. NOTE: adding this as the first response will cause the Begin* LRO API to return this error.

func (*PollerResponder[T]) SetTerminalError

func (p *PollerResponder[T]) SetTerminalError(httpStatus int, errorCode string)

SetTerminalError sets an *azcore.ResponseError with the specified values as the failed terminal response.

Example

Code:play 

package main

import (
	"net/http"

	"github.com/Azure/azure-sdk-for-go/sdk/azcore/fake"
)

// Widget is a hypothetical type used in the following examples.
type Widget struct {
	ID    int
	Shape string
}

// WidgetResponse is a hypothetical type used in the following examples.
type WidgetResponse struct {
	Widget
}

func main() {
	// for a hypothetical API BeginCreateWidget(context.Context) (*runtime.Poller[WidgetResponse], error)

	// a PollerResponder is used to build a sequence of responses for a long-running operation
	pollerResp := fake.PollerResponder[WidgetResponse]{}

	// use SetTerminalError to terminate the long-running operation with an error.
	// this returns an *azcore.ResponseError as the terminal response.
	pollerResp.SetTerminalError(http.StatusBadRequest, "NoMoreWidgets")

	// note that SetTerminalResponse and SetTerminalError are meant to be mutually exclusive.
	// in the event that both are called, the result from SetTerminalError will be used.
}

func (*PollerResponder[T]) SetTerminalResponse

func (p *PollerResponder[T]) SetTerminalResponse(httpStatus int, result T, o *SetTerminalResponseOptions)

SetTerminalResponse sets the provided value as the successful, terminal response.

type Responder

type Responder[T any] exported.Responder[T]

Responder represents a scalar response.

Example

Code:play 

package main

import (
	"net/http"

	"github.com/Azure/azure-sdk-for-go/sdk/azcore/fake"
)

// Widget is a hypothetical type used in the following examples.
type Widget struct {
	ID    int
	Shape string
}

// WidgetResponse is a hypothetical type used in the following examples.
type WidgetResponse struct {
	Widget
}

func main() {
	// for a hypothetical API GetNextWidget(context.Context) (WidgetResponse, error)

	// a Responder is used to build a scalar response
	resp := fake.Responder[WidgetResponse]{}

	// optional HTTP headers can be included in the raw response
	header := http.Header{}
	header.Set("custom-header1", "value1")
	header.Set("custom-header2", "value2")

	// here we set the instance of Widget the Responder is to return
	resp.SetResponse(http.StatusOK, WidgetResponse{
		Widget{ID: 123, Shape: "triangle"},
	}, &fake.SetResponseOptions{
		Header: header,
	})
}

func (*Responder[T]) SetResponse

func (r *Responder[T]) SetResponse(httpStatus int, resp T, o *SetResponseOptions)

SetResponse sets the specified value to be returned.

type SetResponseOptions

type SetResponseOptions = exported.SetResponseOptions

SetResponseOptions contains the optional values for Responder[T].SetResponse.

type SetTerminalResponseOptions

type SetTerminalResponseOptions = exported.SetTerminalResponseOptions

SetTerminalResponseOptions contains the optional values for PollerResponder[T].SetTerminalResponse.

type TokenCredential

type TokenCredential struct {
	// contains filtered or unexported fields
}

TokenCredential is a fake credential that implements the azcore.TokenCredential interface.

func NewTokenCredential

func NewTokenCredential() *TokenCredential

NewTokenCredential creates an instance of the TokenCredential type.

Example

Code:play 

package main

import (
	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/fake"
)

func main() {
	// create a fake azcore.TokenCredential
	// the fake is used as the client credential during testing with fakes.
	var _ azcore.TokenCredential = fake.NewTokenCredential()
}

func (*TokenCredential) GetToken

GetToken implements the azcore.TokenCredential for the TokenCredential type.

func (*TokenCredential) SetError

func (t *TokenCredential) SetError(err error)

SetError sets the specified error to be returned from GetToken(). Use this to simulate an error during authentication.

Example

Code:play 

package main

import (
	"errors"

	"github.com/Azure/azure-sdk-for-go/sdk/azcore/fake"
)

func main() {
	cred := fake.NewTokenCredential()

	// set an error to be returned during authentication
	cred.SetError(errors.New("failed to authenticate"))
}

Source Files

fake.go

Directories

PathSynopsis
fake/internal
fake/serverPackage server provides runtime functionality for fake servers.
Version
v1.7.0-beta.1
Published
May 24, 2023
Platform
js/wasm
Imports
6 packages
Last checked
1 minute ago

Tools for package owners.