func TestDoPollForAsynchronous_StopsPollingIfItReceivesAnInvalidOperationResource(t *testing.T) { r1 := newAsynchronousResponse() r2 := newOperationResourceResponse("busy") r3 := newOperationResourceResponse("busy") r3.Body = mocks.NewBody(operationResourceIllegal) r4 := newOperationResourceResponse(operationSucceeded) client := mocks.NewSender() client.AppendResponse(r1) client.AppendAndRepeatResponse(r2, 2) client.AppendAndRepeatResponse(r3, 1) client.AppendAndRepeatResponse(r4, 1) r, err := autorest.SendWithSender(client, mocks.NewRequest(), DoPollForAsynchronous(time.Millisecond)) if client.Attempts() > 4 { t.Fatalf("azure: DoPollForAsynchronous failed to stop polling after receiving an invalid OperationResource") } if err == nil { t.Fatalf("azure: DoPollForAsynchronous failed to return an error after receving an invalid OperationResource") } autorest.Respond(r, autorest.ByClosing()) }
func TestWithErrorUnlessStatusCode_NotAnAzureError(t *testing.T) { body := `<html> <head> <title>IIS Error page</title> </head> <body>Some non-JSON error page</body> </html>` r := mocks.NewResponseWithContent(body) r.Request = mocks.NewRequest() r.StatusCode = http.StatusBadRequest r.Status = http.StatusText(r.StatusCode) err := autorest.Respond(r, WithErrorUnlessStatusCode(http.StatusOK), autorest.ByClosing()) ok, _ := err.(*RequestError) if ok != nil { t.Fatalf("azure: azure.RequestError returned from malformed response: %v", err) } // the error body should still be there defer r.Body.Close() b, err := ioutil.ReadAll(r.Body) if err != nil { t.Fatal(err) } if string(b) != body { t.Fatalf("response body is wrong. got=%q exptected=%q", string(b), body) } }
func TestDoPollForAsynchronous_ReturnsErrorForLastErrorResponse(t *testing.T) { // Return error code and message if error present in last response r1 := newAsynchronousResponse() r1.Header.Del(http.CanonicalHeaderKey(headerAsyncOperation)) r2 := newProvisioningStatusResponse("busy") r2.Header.Del(http.CanonicalHeaderKey(headerAsyncOperation)) r3 := newAsynchronousResponseWithError() r3.Header.Del(http.CanonicalHeaderKey(headerAsyncOperation)) client := mocks.NewSender() client.AppendResponse(r1) client.AppendAndRepeatResponse(r2, 2) client.AppendAndRepeatResponse(r3, 1) r, err := autorest.SendWithSender(client, mocks.NewRequest(), DoPollForAsynchronous(time.Millisecond)) expected := makeLongRunningOperationErrorString("InvalidParameter", "tom-service-DISCOVERY-server-base-v1.core.local' is not a valid captured VHD blob name prefix.") if err.Error() != expected { t.Fatalf("azure: DoPollForAsynchronous failed to return an appropriate error message for an unknown error. \n expected=%q \n got=%q", expected, err.Error()) } autorest.Respond(r, autorest.ByClosing()) }
// InitiateDeviceAuth initiates a device auth flow. It returns a DeviceCode // that can be used with CheckForUserCompletion or WaitForUserCompletion. func InitiateDeviceAuth(client *autorest.Client, oauthConfig OAuthConfig, clientID, resource string) (*DeviceCode, error) { req, _ := autorest.Prepare( &http.Request{}, autorest.AsPost(), autorest.AsFormURLEncoded(), autorest.WithBaseURL(oauthConfig.DeviceCodeEndpoint.String()), autorest.WithFormData(url.Values{ "client_id": []string{clientID}, "resource": []string{resource}, }), ) resp, err := client.Send(req) if err != nil { return nil, fmt.Errorf("%s %s: %s", logPrefix, errCodeSendingFails, err) } var code DeviceCode err = autorest.Respond( resp, autorest.WithErrorUnlessStatusCode(http.StatusOK), autorest.ByUnmarshallingJSON(&code), autorest.ByClosing()) if err != nil { return nil, fmt.Errorf("%s %s: %s", logPrefix, errCodeHandlingFails, err) } code.ClientID = clientID code.Resource = resource code.OAuthConfig = oauthConfig return &code, nil }
func TestDoPollForAsynchronous_CanBeCanceled(t *testing.T) { cancel := make(chan struct{}) delay := 5 * time.Second r1 := newAsynchronousResponse() client := mocks.NewSender() client.AppendResponse(r1) client.AppendAndRepeatResponse(newOperationResourceResponse("Busy"), -1) var wg sync.WaitGroup wg.Add(1) start := time.Now() go func() { req := mocks.NewRequest() req.Cancel = cancel wg.Done() r, _ := autorest.SendWithSender(client, req, DoPollForAsynchronous(10*time.Second)) autorest.Respond(r, autorest.ByClosing()) }() wg.Wait() close(cancel) time.Sleep(5 * time.Millisecond) if time.Since(start) >= delay { t.Fatalf("azure: DoPollForAsynchronous failed to cancel") } }
// StartResponder handles the response to the Start request. The method always // closes the http.Response Body. func (client VirtualMachineScaleSetVMsClient) StartResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond(resp, client.ByInspecting(), autorest.WithErrorUnlessStatusCode(http.StatusAccepted), autorest.ByClosing()) result.Response = resp return }
func TestDoPollForAsynchronous_ReturnsAnUnknownErrorForFailedOperations(t *testing.T) { // Return unknown error if error not present in last response r1 := newAsynchronousResponse() r1.Header.Del(http.CanonicalHeaderKey(headerAsyncOperation)) r2 := newProvisioningStatusResponse("busy") r2.Header.Del(http.CanonicalHeaderKey(headerAsyncOperation)) r3 := newProvisioningStatusResponse(operationFailed) r3.Header.Del(http.CanonicalHeaderKey(headerAsyncOperation)) client := mocks.NewSender() client.AppendResponse(r1) client.AppendAndRepeatResponse(r2, 2) client.AppendAndRepeatResponse(r3, 1) r, err := autorest.SendWithSender(client, mocks.NewRequest(), DoPollForAsynchronous(time.Millisecond)) expected := makeLongRunningOperationErrorString("Unknown", "None") if err.Error() != expected { t.Fatalf("azure: DoPollForAsynchronous failed to return an appropriate error message for an unknown error. \n expected=%q \n got=%q", expected, err.Error()) } autorest.Respond(r, autorest.ByClosing()) }
// GeneralizeResponder handles the response to the Generalize request. The method always // closes the http.Response Body. func (client VirtualMachinesClient) GeneralizeResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond(resp, client.ByInspecting(), autorest.WithErrorUnlessStatusCode(http.StatusOK), autorest.ByClosing()) result.Response = resp return }
// DeleteResponder handles the response to the Delete request. The method always // closes the http.Response Body. func (client AvailabilitySetsClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond(resp, client.ByInspecting(), autorest.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent), autorest.ByClosing()) result.Response = resp return }
func TestRequestErrorString_WithError(t *testing.T) { j := `{ "error": { "code": "InternalError", "message": "Conflict", "details": [{"code": "conflict1", "message":"error message1"}] } }` uuid := "71FDB9F4-5E49-4C12-B266-DE7B4FD999A6" r := mocks.NewResponseWithContent(j) mocks.SetResponseHeader(r, HeaderRequestID, uuid) r.Request = mocks.NewRequest() r.StatusCode = http.StatusInternalServerError r.Status = http.StatusText(r.StatusCode) err := autorest.Respond(r, WithErrorUnlessStatusCode(http.StatusOK), autorest.ByClosing()) if err == nil { t.Fatalf("azure: returned nil error for proper error response") } azErr, _ := err.(*RequestError) expected := "autorest/azure: Service returned an error. Status=500 Code=\"InternalError\" Message=\"Conflict\" Details=[{\"code\":\"conflict1\",\"message\":\"error message1\"}]" if expected != azErr.Error() { t.Fatalf("azure: send wrong RequestError.\nexpected=%v\ngot=%v", expected, azErr.Error()) } }
func TestWithErrorUnlessStatusCode_FoundAzureErrorWithDetails(t *testing.T) { j := `{ "error": { "code": "InternalError", "message": "Azure is having trouble right now.", "details": [{"code": "conflict1", "message":"error message1"}, {"code": "conflict2", "message":"error message2"}] } }` uuid := "71FDB9F4-5E49-4C12-B266-DE7B4FD999A6" r := mocks.NewResponseWithContent(j) mocks.SetResponseHeader(r, HeaderRequestID, uuid) r.Request = mocks.NewRequest() r.StatusCode = http.StatusInternalServerError r.Status = http.StatusText(r.StatusCode) err := autorest.Respond(r, WithErrorUnlessStatusCode(http.StatusOK), autorest.ByClosing()) if err == nil { t.Fatalf("azure: returned nil error for proper error response") } azErr, ok := err.(*RequestError) if !ok { t.Fatalf("azure: returned error is not azure.RequestError: %T", err) } if expected := "InternalError"; azErr.ServiceError.Code != expected { t.Fatalf("azure: wrong error code. expected=%q; got=%q", expected, azErr.ServiceError.Code) } if azErr.ServiceError.Message == "" { t.Fatalf("azure: error message is not unmarshaled properly") } b, _ := json.Marshal(*azErr.ServiceError.Details) if string(b) != `[{"code":"conflict1","message":"error message1"},{"code":"conflict2","message":"error message2"}]` { t.Fatalf("azure: error details is not unmarshaled properly") } if expected := http.StatusInternalServerError; azErr.StatusCode != expected { t.Fatalf("azure: got wrong StatusCode=%v Expected=%d", azErr.StatusCode, expected) } if expected := uuid; azErr.RequestID != expected { t.Fatalf("azure: wrong request ID in error. expected=%q; got=%q", expected, azErr.RequestID) } _ = azErr.Error() // the error body should still be there defer r.Body.Close() b, err = ioutil.ReadAll(r.Body) if err != nil { t.Fatal(err) } if string(b) != j { t.Fatalf("response body is wrong. got=%q expected=%q", string(b), j) } }
// DeleteResponder handles the response to the Delete request. The method always // closes the http.Response Body. func (client ManagementClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond( resp, client.ByInspecting(), azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted, http.StatusNotFound), autorest.ByClosing()) result.Response = resp return }
// PowerOffResponder handles the response to the PowerOff request. The method always // closes the http.Response Body. func (client VirtualMachinesClient) PowerOffResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond( resp, client.ByInspecting(), azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted), autorest.ByClosing()) result.Response = resp return }
// StopResponder handles the response to the Stop request. The method always // closes the http.Response Body. func (client ApplicationGatewaysClient) StopResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond( resp, client.ByInspecting(), azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted), autorest.ByClosing()) result.Response = resp return }
// SynchronizeAutoStorageKeysResponder handles the response to the SynchronizeAutoStorageKeys request. The method always // closes the http.Response Body. func (client AccountClient) SynchronizeAutoStorageKeysResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond( resp, client.ByInspecting(), azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent), autorest.ByClosing()) result.Response = resp return }
// MoveResourcesResponder handles the response to the MoveResources request. The method always // closes the http.Response Body. func (client GlobalResourceGroupsClient) MoveResourcesResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond( resp, client.ByInspecting(), azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent), autorest.ByClosing()) result.Response = resp return }
// DeleteResponder handles the response to the Delete request. The method always // closes the http.Response Body. func (client RouteTablesClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond( resp, client.ByInspecting(), azure.WithErrorUnlessStatusCode(http.StatusNoContent, http.StatusOK, http.StatusAccepted), autorest.ByClosing()) result.Response = resp return }
// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always // closes the http.Response Body. func (client VirtualMachineExtensionsClient) CreateOrUpdateResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond( resp, client.ByInspecting(), azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated), autorest.ByClosing()) result.Response = resp return }
// ListEffectiveNetworkSecurityGroupsResponder handles the response to the ListEffectiveNetworkSecurityGroups request. The method always // closes the http.Response Body. func (client InterfacesClient) ListEffectiveNetworkSecurityGroupsResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond( resp, client.ByInspecting(), azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted), autorest.ByClosing()) result.Response = resp return }
// DeleteAuthorizationRuleResponder handles the response to the DeleteAuthorizationRule request. The method always // closes the http.Response Body. func (client Client) DeleteAuthorizationRuleResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond( resp, client.ByInspecting(), azure.WithErrorUnlessStatusCode(http.StatusNoContent, http.StatusOK), autorest.ByClosing()) result.Response = resp return }
// CreateEnvironmentResponder handles the response to the CreateEnvironment request. The method always // closes the http.Response Body. func (client LabOperationsClient) CreateEnvironmentResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond( resp, client.ByInspecting(), azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted), autorest.ByClosing()) result.Response = resp return }
// ForceRebootResponder handles the response to the ForceReboot request. The method always // closes the http.Response Body. func (client ManagementClient) ForceRebootResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond( resp, client.ByInspecting(), azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent), autorest.ByClosing()) result.Response = resp return }
// CreateOrUpdateResponder handles the response to the CreateOrUpdate request. The method always // closes the http.Response Body. func (client ExpressRouteCircuitsClient) CreateOrUpdateResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond( resp, client.ByInspecting(), azure.WithErrorUnlessStatusCode(http.StatusCreated, http.StatusOK), autorest.ByClosing()) result.Response = resp return }
// CheckExistenceByIDResponder handles the response to the CheckExistenceByID request. The method always // closes the http.Response Body. func (client Client) CheckExistenceByIDResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond( resp, client.ByInspecting(), azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent, http.StatusNotFound), autorest.ByClosing()) result.Response = resp return }
// DeleteResponder handles the response to the Delete request. The method always // closes the http.Response Body. func (client IntegrationAccountsClient) DeleteResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond( resp, client.ByInspecting(), azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent), autorest.ByClosing()) result.Response = resp return }
// CancelResponder handles the response to the Cancel request. The method always // closes the http.Response Body. func (client DeploymentsClient) CancelResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond( resp, client.ByInspecting(), azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusNoContent), autorest.ByClosing()) result.Response = resp return }
// UpdateResponder handles the response to the Update request. The method always // closes the http.Response Body. func (client OriginsClient) UpdateResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond( resp, client.ByInspecting(), azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusAccepted), autorest.ByClosing()) result.Response = resp return }
// RunResponder handles the response to the Run request. The method always // closes the http.Response Body. func (client WorkflowTriggersClient) RunResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond( resp, client.ByInspecting(), azure.WithErrorUnlessStatusCode(http.StatusOK), autorest.ByClosing()) result.Response = resp return }
// ResetResponder handles the response to the Reset request. The method always // closes the http.Response Body. func (client VirtualNetworkGatewaysClient) ResetResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond( resp, client.ByInspecting(), azure.WithErrorUnlessStatusCode(http.StatusAccepted, http.StatusOK), autorest.ByClosing()) result.Response = resp return }
// MigrateResponder handles the response to the Migrate request. The method always // closes the http.Response Body. func (client WorkspaceCollectionsClient) MigrateResponder(resp *http.Response) (result autorest.Response, err error) { err = autorest.Respond( resp, client.ByInspecting(), azure.WithErrorUnlessStatusCode(http.StatusOK), autorest.ByClosing()) result.Response = resp return }