func TestWithUserAgent(t *testing.T) { r, err := Prepare(mocks.NewRequest(), WithUserAgent("User Agent Go")) if err != nil { fmt.Printf("ERROR: %v", err) } if r.Header.Get(headerUserAgent) != "User Agent Go" { t.Errorf("autorest: WithUserAgent failed to add header (%s=%s)", headerUserAgent, r.Header.Get(headerUserAgent)) } }
func TestAsContentType(t *testing.T) { r, err := Prepare(mocks.NewRequest(), AsContentType("application/text")) if err != nil { fmt.Printf("ERROR: %v", err) } if r.Header.Get(headerContentType) != "application/text" { t.Errorf("autorest: AsContentType failed to add header (%s=%s)", headerContentType, r.Header.Get(headerContentType)) } }
func TestWithBearerAuthorization(t *testing.T) { r, err := Prepare(mocks.NewRequest(), WithBearerAuthorization("SOME-TOKEN")) if err != nil { fmt.Printf("ERROR: %v", err) } if r.Header.Get(headerAuthorization) != "Bearer SOME-TOKEN" { t.Errorf("autorest: WithBearerAuthorization failed to add header (%s=%s)", headerAuthorization, r.Header.Get(headerAuthorization)) } }
func TestAsJSON(t *testing.T) { r, err := Prepare(mocks.NewRequest(), AsJSON()) if err != nil { fmt.Printf("ERROR: %v", err) } if r.Header.Get(headerContentType) != mimeTypeJSON { t.Errorf("autorest: AsJSON failed to add header (%s=%s)", headerContentType, r.Header.Get(headerContentType)) } }
func TestWithHeaderAllocatesHeaders(t *testing.T) { r, err := Prepare(mocks.NewRequest(), WithHeader("x-foo", "bar")) if err != nil { t.Errorf("autorest: WithHeader failed (%v)", err) } if r.Header.Get("x-foo") != "bar" { t.Errorf("autorest: WithHeader failed to add header (%s=%s)", "x-foo", r.Header.Get("x-foo")) } }
func TestClientSendReturnsPrepareError(t *testing.T) { r := mocks.NewRequest() s := mocks.NewSender() c := Client{Authorizer: mockFailingAuthorizer{}, Sender: s} _, err := c.Send(r) if err == nil { t.Error("autorest: Client#Send failed to return an error the Prepare error") } }
func TestClientSendSends(t *testing.T) { r := mocks.NewRequest() s := mocks.NewSender() c := Client{Sender: s} c.Send(r) if s.Attempts() <= 0 { t.Error("autorest: Client#Send failed to invoke the Sender") } }
func TestWithNothing(t *testing.T) { r1 := mocks.NewRequest() r2, err := Prepare(r1, WithNothing()) if err != nil { t.Errorf("autorest: WithNothing returned an unexpected error (%v)", err) } if !reflect.DeepEqual(r1, r2) { t.Error("azure: WithNothing modified the passed HTTP Request") } }
func TestClientSendInvokesInspector(t *testing.T) { r := mocks.NewRequest() s := mocks.NewSender() i := &mockInspector{} c := Client{RequestInspector: i.WithInspection(), Sender: s} c.Send(r) if !i.wasInvoked { t.Error("autorest: Client#Send failed to invoke the RequestInspector") } }
func TestClientSendDefaultsToUsingStatusCodeOK(t *testing.T) { r := mocks.NewRequest() s := mocks.NewSender() c := Client{Authorizer: mockAuthorizer{}, Sender: s} _, err := c.Send(r) if err != nil { t.Errorf("autorest: Client#Send returned an error for Status Code OK -- %v", err) } }
func TestClientSendClosesReponseBodyWhenReturningError(t *testing.T) { s := mocks.NewSender() r := mocks.NewResponseWithStatus("500 InternalServerError", http.StatusInternalServerError) s.SetResponse(r) c := Client{Sender: s} c.Send(mocks.NewRequest()) if r.Body.(*mocks.Body).IsOpen() { t.Error("autorest: Client#Send failed to close the response body when returning an error") } }
func TestClientSendReturnsErrorWithUnexpectedStatusCode(t *testing.T) { r := mocks.NewRequest() s := mocks.NewSender() s.EmitStatus("500 InternalServerError", http.StatusInternalServerError) c := Client{Sender: s} _, err := c.Send(r) if err == nil { t.Error("autorest: Client#Send failed to return an error for an unexpected Status Code") } }
func TestClientSendDoesNotPollIfUnnecessary(t *testing.T) { r := mocks.NewRequest() s := mocks.NewSender() c := Client{Sender: s, PollingMode: PollUntilAttempts, PollingAttempts: 10} c.Send(r, http.StatusOK, http.StatusAccepted) if s.Attempts() != 1 { t.Errorf("autorest: Client#Send unexpectedly polled -- attempts %d", s.Attempts()) } }
func TestWithAuthorizer(t *testing.T) { r1 := mocks.NewRequest() na := &NullAuthorizer{} r2, err := Prepare(r1, na.WithAuthorization()) if err != nil { t.Errorf("autorest: NullAuthorizer#WithAuthorization returned an unexpected error (%v)", err) } else if !reflect.DeepEqual(r1, r2) { t.Errorf("autorest: NullAuthorizer#WithAuthorization modified the request -- received %v, expected %v", r2, r1) } }
func TestClientDoSetsUserAgent(t *testing.T) { c := Client{UserAgent: "UserAgent"} r := mocks.NewRequest() c.Do(r) if r.Header.Get(http.CanonicalHeaderKey(headerUserAgent)) != "UserAgent" { t.Errorf("autorest: Client#Do failed to correctly set User-Agent header: %s=%s", http.CanonicalHeaderKey(headerUserAgent), r.Header.Get(http.CanonicalHeaderKey(headerUserAgent))) } }
func TestClientSendDoesNotReturnErrorForExpectedStatusCode(t *testing.T) { r := mocks.NewRequest() s := mocks.NewSender() s.EmitStatus("500 InternalServerError", http.StatusInternalServerError) c := Client{Sender: s} _, err := c.Send(r, http.StatusInternalServerError) if err != nil { t.Errorf("autorest: Client#Send returned an error for an expected Status Code -- %v", err) } }
func TestClientSendPollsIfNeeded(t *testing.T) { r := mocks.NewRequest() s := mocks.NewSender() s.SetPollAttempts(5) c := Client{Sender: s, PollingMode: PollUntilAttempts, PollingAttempts: 10} c.Send(r, http.StatusOK, http.StatusAccepted) if s.Attempts() != (5 + 1) { t.Errorf("autorest: Client#Send failed to poll the expected number of times -- attempts %d", s.Attempts()) } }
func TestClientSendSetsAuthorization(t *testing.T) { r := mocks.NewRequest() s := mocks.NewSender() c := Client{Authorizer: mockAuthorizer{}, Sender: s} c.Send(r) if len(r.Header.Get(http.CanonicalHeaderKey(headerAuthorization))) <= 0 { t.Errorf("autorest: Client#Send failed to set Authorization header -- %s=%s", http.CanonicalHeaderKey(headerAuthorization), r.Header.Get(http.CanonicalHeaderKey(headerAuthorization))) } }
func TestWithErrorUnlessOK(t *testing.T) { r := mocks.NewResponse() r.Request = mocks.NewRequest() err := Respond(r, WithErrorUnlessOK(), ByClosingIfError()) if err != nil { t.Errorf("autorest: WithErrorUnlessOK returned an error for OK status code (%v)", err) } }
func TestDoErrorIfStatusCodeIgnoresStatusCodes(t *testing.T) { client := mocks.NewSender() client.EmitStatus("202 Accepted", http.StatusAccepted) r, err := SendWithSender(client, mocks.NewRequest(), DoErrorIfStatusCode(http.StatusBadRequest), DoCloseIfError()) if err != nil { t.Error("autorest: DoErrorIfStatusCode failed to ignore a status code") } Respond(r, ByClosing()) }
func TestDoErrorUnlessStatusCode(t *testing.T) { client := mocks.NewSender() client.EmitStatus("400 BadRequest", http.StatusBadRequest) r, err := SendWithSender(client, mocks.NewRequest(), DoErrorUnlessStatusCode(http.StatusAccepted), DoCloseIfError()) if err == nil { t.Error("autorest: DoErrorUnlessStatusCode failed to emit an error for an unknown status code") } Respond(r, ByClosing()) }
func TestDoErrorUnlessStatusCodeIgnoresStatusCodes(t *testing.T) { client := mocks.NewSender() client.EmitStatus("202 Accepted", http.StatusAccepted) r, err := SendWithSender(client, mocks.NewRequest(), DoErrorUnlessStatusCode(http.StatusAccepted), DoCloseIfError()) if err != nil { t.Error("autorest: DoErrorUnlessStatusCode emitted an error for a knonwn status code") } Respond(r, ByClosing()) }
func TestWithErrorUnlessOKEmitsErrorIfNotOK(t *testing.T) { r := mocks.NewResponse() r.Request = mocks.NewRequest() r.Status = "400 BadRequest" r.StatusCode = http.StatusBadRequest err := Respond(r, WithErrorUnlessOK(), ByClosingIfError()) if err == nil { t.Errorf("autorest: WithErrorUnlessOK failed to return an error for a non-OK status code (%v)", err) } }
func TestWithErrorUnlessStatusCodeEmitsErrorForUnacceptableStatusCode(t *testing.T) { r := mocks.NewResponse() r.Request = mocks.NewRequest() r.Status = "400 BadRequest" r.StatusCode = http.StatusBadRequest err := Respond(r, WithErrorUnlessStatusCode(http.StatusOK, http.StatusUnauthorized, http.StatusInternalServerError), ByClosingIfError()) if err == nil { t.Errorf("autorest: WithErrorUnlessStatusCode failed to return an error for an unacceptable status code (%s)", r.Status) } }
func ExampleDoRetryForAttempts() { client := mocks.NewSender() client.EmitErrors(10) // Retry with backoff -- ensure returned Bodies are closed r, _ := SendWithSender(client, mocks.NewRequest(), DoCloseIfError(), DoRetryForAttempts(5, time.Duration(0))) Respond(r, ByClosing()) fmt.Printf("Retry stopped after %d attempts", client.Attempts()) // Output: Retry stopped after 5 attempts }
func TestDoCloseIfError(t *testing.T) { client := mocks.NewSender() client.EmitStatus("400 BadRequest", http.StatusBadRequest) r, _ := SendWithSender(client, mocks.NewRequest(), DoErrorIfStatusCode(http.StatusBadRequest), DoCloseIfError()) if r.Body.(*mocks.Body).IsOpen() { t.Error("autorest: Expected DoCloseIfError to close response body -- it was left open") } Respond(r, ByClosing()) }
func TestAfterDelayWaits(t *testing.T) { client := mocks.NewSender() d := 10 * time.Millisecond tt := time.Now() r, _ := SendWithSender(client, mocks.NewRequest(), AfterDelay(d)) s := time.Since(tt) if s < d { t.Error("autorest: AfterDelay failed to wait for at least the specified duration") } Respond(r, ByClosing()) }
func TestDoRetryForDurationStopsAfterSuccess(t *testing.T) { client := mocks.NewSender() r, err := SendWithSender(client, mocks.NewRequest(), DoRetryForDuration(10*time.Millisecond, time.Duration(0))) if client.Attempts() != 1 { t.Errorf("autorest: DoRetryForDuration failed to stop after success -- expected attempts %v, actual %v", 1, client.Attempts()) } if err != nil { t.Errorf("autorest: DoRetryForDuration returned an unexpected error (%v)", err) } Respond(r, ByClosing()) }
func ExampleDoErrorIfStatusCode() { client := mocks.NewSender() client.EmitStatus("204 NoContent", http.StatusNoContent) // Chain decorators to retry the request, up to five times, if the status code is 204 r, _ := SendWithSender(client, mocks.NewRequest(), DoErrorIfStatusCode(http.StatusNoContent), DoCloseIfError(), DoRetryForAttempts(5, time.Duration(0))) Respond(r, ByClosing()) fmt.Printf("Retry stopped after %d attempts with code %s", client.Attempts(), r.Status) // Output: Retry stopped after 5 attempts with code 204 NoContent }
func TestDoRetryForAttemptsReturnsResponse(t *testing.T) { client := mocks.NewSender() client.EmitErrors(1) r, err := SendWithSender(client, mocks.NewRequest(), DoRetryForAttempts(1, time.Duration(0))) if err == nil { t.Error("autorest: Mock client failed to emit errors") } if r == nil { t.Error("autorest: DoRetryForAttempts failed to return the underlying response") } Respond(r, ByClosing()) }