Documentation ¶
Overview ¶
Package fake provides the building blocks for fake servers. This includes fakes for authentication, API responses, and more.
Index ¶
- type AddNonTerminalResponseOptions
- type AddPageOptions
- type ErrorResponder
- type PagerResponder
- type PollerResponder
- func (p *PollerResponder[T]) AddNonTerminalResponse(httpStatus int, o *AddNonTerminalResponseOptions)
- func (p *PollerResponder[T]) AddPollingError(err error)
- func (p *PollerResponder[T]) SetTerminalError(httpStatus int, errorCode string)
- func (p *PollerResponder[T]) SetTerminalResponse(httpStatus int, result T, o *SetTerminalResponseOptions)
- type Responder
- type SetResponseOptions
- type SetTerminalResponseOptions
- type TokenCredential
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
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 ¶
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 }
Output:
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.
- errorCode is the value to be used in the ResponseError.Code field
- httpStatus is the HTTP status code
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 ¶
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) }
Output:
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.
- page is the response page to be added
- o contains optional values, pass nil to accept the defaults
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 ¶
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) }
Output:
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 ¶
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. }
Output:
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 ¶
Responder represents a scalar response.
Example ¶
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, }) }
Output:
func (*Responder[T]) SetResponse ¶
func (r *Responder[T]) SetResponse(httpStatus int, resp T, o *SetResponseOptions)
SetResponse sets the specified value to be returned.
- httpStatus is the HTTP status code to be returned
- resp is the response to be returned
- o contains optional values, pass nil to accept the defaults
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 ¶
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() }
Output:
func (*TokenCredential) GetToken ¶
func (t *TokenCredential) GetToken(ctx context.Context, opts policy.TokenRequestOptions) (azcore.AccessToken, error)
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 ¶
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")) }
Output: