func TestClientPollAsNeededPollsForDuration(t *testing.T) { c := Client{} c.PollingMode = PollUntilDuration c.PollingDuration = 10 * time.Millisecond s := mocks.NewSender() s.EmitStatus("202 Accepted", http.StatusAccepted) c.Sender = s r := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(r) s.SetResponse(r) d1 := 10 * time.Millisecond start := time.Now() resp, _ := c.PollAsNeeded(r) d2 := time.Now().Sub(start) if d2 < d1 { t.Errorf("autorest: Client#PollAsNeeded did not poll for the expected duration -- expected %v, actual %v", d1.Seconds(), d2.Seconds()) } Respond(resp, ByClosing()) }
func TestResponseRequiresPollingReturnsFalseForUnexpectedStatusCodes(t *testing.T) { resp := mocks.NewResponseWithStatus("500 InternalServerError", http.StatusInternalServerError) mocks.SetAcceptedHeaders(resp) if ResponseRequiresPolling(resp) { t.Error("autorest: ResponseRequiresPolling did not return false when ignoring a status code") } }
func TestNewPollingRequestLeavesBodyOpenWhenLocationHeaderIsMissing(t *testing.T) { resp := mocks.NewResponseWithStatus("500 InternalServerError", http.StatusInternalServerError) NewPollingRequest(resp, NullAuthorizer{}) if !resp.Body.(*mocks.Body).IsOpen() { t.Error("autorest: NewPollingRequest closed the http.Request Body when the Location header was missing") } }
func TestGetPollingLocationReturnsEmptyStringForMissingLocation(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) l := GetPollingLocation(resp) if len(l) != 0 { t.Errorf("autorest: GetPollingLocation return a value without a Location header -- received %v", l) } }
func TestNewPollingRequestDoesNotReturnARequestWhenLocationHeaderIsMissing(t *testing.T) { resp := mocks.NewResponseWithStatus("500 InternalServerError", http.StatusInternalServerError) req, _ := NewPollingRequest(resp, NullAuthorizer{}) if req != nil { t.Error("autorest: NewPollingRequest returned an http.Request when the Location header was missing") } }
func TestResponseRequiresPollingDefaultsToAcceptedStatusCode(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) if !ResponseRequiresPolling(resp) { t.Error("autorest: ResponseRequiresPolling failed to create a request for default 202 Accepted status code") } }
func TestNewPollingRequestClosesTheResponseBody(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) NewPollingRequest(resp, NullAuthorizer{}) if resp.Body.(*mocks.Body).IsOpen() { t.Error("autorest: NewPollingRequest failed to close the response body when creating a new request") } }
func (s *Senders) Do(req *http.Request) (*http.Response, error) { if len(*s) == 0 { response := mocks.NewResponseWithStatus("", http.StatusInternalServerError) return response, fmt.Errorf("no sender for %q", req.URL) } sender := (*s)[0] *s = (*s)[1:] return sender.Do(req) }
func TestClientIsPollingAllowed(t *testing.T) { c := Client{PollingMode: PollUntilAttempts} r := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) err := c.IsPollingAllowed(r) if err != nil { t.Errorf("autorest: Client#IsPollingAllowed returned an error for an http.Response that requires polling (%v)", err) } }
func TestClientIsPollingAllowedIgnoresDisabledForIgnoredStatusCode(t *testing.T) { c := Client{PollingMode: PollUntilAttempts} r := mocks.NewResponseWithStatus("400 BadRequest", http.StatusBadRequest) err := c.IsPollingAllowed(r) if err != nil { t.Errorf("autorest: Client#IsPollingAllowed returned an error for an http.Response that requires polling (%v)", err) } }
func TestClientIsPollingAllowedIgnoredPollingMode(t *testing.T) { c := Client{PollingMode: DoNotPoll} r := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) err := c.IsPollingAllowed(r) if err == nil { t.Error("autorest: Client#IsPollingAllowed failed to return an error when polling is disabled") } }
func TestGetPollingDelayReturnsDefaultDelayIfRetryHeaderIsMissing(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) d := GetPollingDelay(resp, DefaultPollingDelay) if d != DefaultPollingDelay { t.Errorf("autorest: GetPollingDelay failed to returned the default delay for a missing Retry-After header -- expected %v, received %v", DefaultPollingDelay, d) } }
func TestGetPollingDelay(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) d := GetPollingDelay(resp, DefaultPollingDelay) if d != mocks.TestDelay { t.Errorf("autorest: GetPollingDelay failed to returned the expected delay -- expected %v, received %v", mocks.TestDelay, d) } }
func TestGetPollingLocation(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) l := GetPollingLocation(resp) if len(l) == 0 { t.Errorf("autorest: GetPollingLocation failed to return Location header -- expected %v, received %v", mocks.TestURL, l) } }
func TestNewPollingRequestProvidesTheURL(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) req, _ := NewPollingRequest(resp, NullAuthorizer{}) if req.URL.String() != mocks.TestURL { t.Errorf("autorest: NewPollingRequest did not create an HTTP with the expected URL -- received %v, expected %v", req.URL, mocks.TestURL) } }
func TestNewPollingRequestReturnsAGetRequest(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) req, _ := NewPollingRequest(resp, NullAuthorizer{}) if req.Method != "GET" { t.Errorf("autorest: NewPollingRequest did not create an HTTP GET request -- actual method %v", req.Method) } }
func TestNewPollingRequestReturnsAnErrorWhenPrepareFails(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) _, err := NewPollingRequest(resp, mockFailingAuthorizer{}) if err == nil { t.Error("autorest: NewPollingRequest failed to return an error when Prepare fails") } }
func TestNewPollingRequestDoesNotReturnARequestWhenPrepareFails(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) resp.Header.Set(http.CanonicalHeaderKey(headerLocation), testBadURL) req, _ := NewPollingRequest(resp, NullAuthorizer{}) if req != nil { t.Error("autorest: NewPollingRequest returned an http.Request when Prepare failed") } }
func TestNewPollingRequestAppliesAuthorization(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) req, _ := NewPollingRequest(resp, mockAuthorizer{}) if req.Header.Get(http.CanonicalHeaderKey(headerAuthorization)) != testAuthorizationHeader { t.Errorf("autorest: NewPollingRequest did not apply authorization -- received %v, expected %v", req.Header.Get(http.CanonicalHeaderKey(headerAuthorization)), testAuthorizationHeader) } }
func TestNewPollingRequestLeavesBodyOpenWhenPrepareFails(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) resp.Header.Set(http.CanonicalHeaderKey(headerLocation), testBadURL) _, err := NewPollingRequest(resp, NullAuthorizer{}) if !resp.Body.(*mocks.Body).IsOpen() { t.Errorf("autorest: NewPollingRequest closed the http.Request Body when Prepare returned an error (%v)", err) } }
func TestGetPollingDelayReturnsDefaultDelayIfRetryHeaderIsMalformed(t *testing.T) { resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) resp.Header.Set(http.CanonicalHeaderKey(headerRetryAfter), "a very bad non-integer value") d := GetPollingDelay(resp, DefaultPollingDelay) if d != DefaultPollingDelay { t.Errorf("autorest: GetPollingDelay failed to returned the default delay for a malformed Retry-After header -- expected %v, received %v", DefaultPollingDelay, d) } }
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 TestDecorateForPollingCloseBodyOnEachAttempt(t *testing.T) { client := mocks.NewSender() resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) client.SetResponse(resp) req, _ := NewPollingRequest(resp, NullAuthorizer{}) resp, _ = PollForAttempts(client, req, time.Duration(0), 5) if resp.Body.(*mocks.Body).CloseAttempts() < 5 { t.Errorf("autorest: decorateForPolling failed to close the response Body between requests -- expected %v, received %v", 5, resp.Body.(*mocks.Body).CloseAttempts()) } }
func TestPollingReturnsNoErrorForUnexpectedStatusCode(t *testing.T) { client := mocks.NewSender() client.EmitStatus("500 InternalServerError", http.StatusInternalServerError) resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) req, _ := NewPollingRequest(resp, NullAuthorizer{}) resp, err := PollForAttempts(client, req, time.Duration(0), 1, http.StatusAccepted) if err != nil { t.Error("autorest: Polling emitted error for unknown status code") } }
func TestPollForAttemptsStops(t *testing.T) { client := mocks.NewSender() client.EmitErrors(-1) resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) req, _ := NewPollingRequest(resp, NullAuthorizer{}) PollForAttempts(client, req, time.Duration(0), 5) if client.Attempts() < 5 || client.Attempts() > 5 { t.Errorf("autorest: PollForAttempts stopped incorrectly -- expected %v attempts, actual attempts were %v", 5, client.Attempts()) } }
func TestPollingReturnsDefaultsToAcceptedStatusCode(t *testing.T) { client := mocks.NewSender() resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) client.SetResponse(resp) req, _ := NewPollingRequest(resp, NullAuthorizer{}) resp, err := PollForAttempts(client, req, time.Duration(0), 1) if err == nil { t.Error("autorest: Polling failed to default to HTTP 202") } }
func TestPollingLeavesFinalBodyOpen(t *testing.T) { client := mocks.NewSender() client.EmitStatus("500 InternalServerError", http.StatusInternalServerError) resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) req, _ := NewPollingRequest(resp, NullAuthorizer{}) resp, _ = PollForAttempts(client, req, time.Duration(0), 1) if !resp.Body.(*mocks.Body).IsOpen() { t.Error("autorest: Polling unexpectedly closed the response body") } }
func TestClientPollAsNeededReturnsErrorWhenPollingDisabled(t *testing.T) { c := Client{} c.Sender = mocks.NewSender() c.PollingMode = DoNotPoll r := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(r) _, err := c.PollAsNeeded(r) if err == nil { t.Error("autorest: Client#PollAsNeeded failed to return an error when polling was disabled but required") } Respond(r, ByClosing()) }
func TestPollForDurationsStops(t *testing.T) { client := mocks.NewSender() client.EmitErrors(-1) d := 10 * time.Millisecond resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(resp) req, _ := NewPollingRequest(resp, NullAuthorizer{}) start := time.Now() PollForDuration(client, req, time.Duration(0), d) if time.Now().Sub(start) < d { t.Error("autorest: PollForDuration stopped too soon") } }
func TestClientPollAsNeededReturnsErrorIfUnableToCreateRequest(t *testing.T) { c := Client{} c.Authorizer = mockFailingAuthorizer{} c.Sender = mocks.NewSender() c.PollingMode = PollUntilAttempts c.PollingAttempts = 1 r := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted) mocks.SetAcceptedHeaders(r) _, err := c.PollAsNeeded(r) if err == nil { t.Error("autorest: Client#PollAsNeeded failed to return error when unable to create request") } Respond(r, ByClosing()) }