Exemple #1
0
func newAsynchronousResponseWithError() *http.Response {
	r := mocks.NewResponseWithStatus("400 Bad Request", http.StatusBadRequest)
	mocks.SetRetryHeader(r, retryDelay)
	r.Request = mocks.NewRequestForURL(mocks.TestURL)
	r.Body = mocks.NewBody(errorResponse)
	return r
}
Exemple #2
0
func newAsynchronousResponse() *http.Response {
	r := mocks.NewResponseWithStatus("201 Created", http.StatusCreated)
	r.Body = mocks.NewBody(fmt.Sprintf(pollingStateFormat, operationInProgress))
	mocks.SetResponseHeader(r, http.CanonicalHeaderKey(headerAsyncOperation), mocks.TestAzureAsyncURL)
	mocks.SetResponseHeader(r, http.CanonicalHeaderKey(autorest.HeaderLocation), mocks.TestLocationURL)
	mocks.SetRetryHeader(r, retryDelay)
	r.Request = mocks.NewRequestForURL(mocks.TestURL)
	return r
}
func TestResponseGetPollingDelay(t *testing.T) {
	d1 := 10 * time.Second

	r := mocks.NewResponse()
	mocks.SetRetryHeader(r, d1)
	ar := Response{Response: r}

	d2 := ar.GetPollingDelay(time.Duration(0))
	if d1 != d2 {
		t.Errorf("autorest: Response#GetPollingDelay failed to return the correct delay -- expected %v, received %v",
			d1, d2)
	}
}
func TestPollForDurationsStopsWithinReason(t *testing.T) {
	client := mocks.NewSender()
	client.EmitErrors(-1)

	d := 10 * time.Millisecond
	resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted)
	mocks.SetAcceptedHeaders(resp)
	mocks.SetRetryHeader(resp, d)
	client.SetResponse(resp)

	req, _ := NewPollingRequest(resp, NullAuthorizer{})

	start := time.Now()
	PollForDuration(client, req, time.Duration(0), d)
	if time.Now().Sub(start) > (time.Duration(5.0) * d) {
		t.Error("autorest: PollForDuration took too long to stop -- exceeded 5 times expected duration")
	}
}
func TestPollingHonorsDelay(t *testing.T) {
	client := mocks.NewSender()
	client.EmitErrors(-1)

	d1 := 10 * time.Millisecond
	resp := mocks.NewResponseWithStatus("202 Accepted", http.StatusAccepted)
	mocks.SetAcceptedHeaders(resp)
	mocks.SetRetryHeader(resp, d1)
	client.SetResponse(resp)

	req, _ := NewPollingRequest(resp, NullAuthorizer{})

	start := time.Now()
	PollForAttempts(client, req, time.Duration(0), 2)
	d2 := time.Now().Sub(start)
	if d2 < d1 {
		t.Errorf("autorest: Polling failed to honor delay -- expected %v, actual %v", d1.Seconds(), d2.Seconds())
	}
}