Esempio n. 1
0
func TestCreateResponderDoesNotModify(t *testing.T) {
	r1 := mocks.NewResponse()
	r2 := mocks.NewResponse()
	p := CreateResponder()
	err := p.Respond(r1)
	if err != nil {
		t.Errorf("autorest: CreateResponder failed (%v)", err)
	}
	if !reflect.DeepEqual(r1, r2) {
		t.Errorf("autorest: CreateResponder without decorators modified the response")
	}
}
Esempio n. 2
0
func TestDoPollForStatusCodes_CanBeCanceled(t *testing.T) {
	cancel := make(chan struct{})
	delay := 5 * time.Second

	r := mocks.NewResponse()
	mocks.SetAcceptedHeaders(r)
	client := mocks.NewSender()
	client.AppendAndRepeatResponse(r, 100)

	var wg sync.WaitGroup
	wg.Add(1)
	start := time.Now()
	go func() {
		wg.Done()
		r, _ := SendWithSender(client, mocks.NewRequest(),
			DoPollForStatusCodes(time.Millisecond, time.Millisecond, http.StatusAccepted))
		Respond(r,
			ByClosing())
	}()
	wg.Wait()
	close(cancel)
	time.Sleep(5 * time.Millisecond)
	if time.Since(start) >= delay {
		t.Fatalf("autorest: Sender#DoPollForStatusCodes failed to cancel")
	}
}
Esempio n. 3
0
func TestGetAsyncOperation_ReturnsEmptyStringIfHeaderIsAbsent(t *testing.T) {
	r := mocks.NewResponse()

	if len(getAsyncOperation(r)) != 0 {
		t.Fatalf("azure: getAsyncOperation failed to return empty string when the Azure-AsyncOperation header is absent -- received %v", getAsyncOperation(r))
	}
}
Esempio n. 4
0
func TestResponseRequiresPollingIgnoresSuccess(t *testing.T) {
	resp := mocks.NewResponse()

	if ResponseRequiresPolling(resp) {
		t.Error("autorest: ResponseRequiresPolling did not ignore a successful response")
	}
}
Esempio n. 5
0
func TestResponseRequiresPollingLeavesBodyOpen(t *testing.T) {
	resp := mocks.NewResponse()

	ResponseRequiresPolling(resp)
	if !resp.Body.(*mocks.Body).IsOpen() {
		t.Error("autorest: ResponseRequiresPolling closed the responise body while ignoring a successful response")
	}
}
Esempio n. 6
0
func TestClientShouldPollIgnoredPollingMode(t *testing.T) {
	c := &Client{PollingMode: DoNotPoll}
	r := mocks.NewResponse()

	if c.ShouldPoll(r) {
		t.Error("autorest: Client#ShouldPoll failed to return false when polling is disabled")
	}
}
Esempio n. 7
0
func TestClientShouldPollIgnoresOk(t *testing.T) {
	c := &Client{PollingMode: PollUntilAttempts}
	r := mocks.NewResponse()

	if c.ShouldPoll(r) {
		t.Error("autorest: Client#ShouldPoll failed to return false for an http.Response that does not require polling")
	}
}
Esempio n. 8
0
func TestByIgnoring(t *testing.T) {
	r := mocks.NewResponse()

	Respond(r,
		(func() RespondDecorator {
			return func(r Responder) Responder {
				return ResponderFunc(func(r2 *http.Response) error {
					r1 := mocks.NewResponse()
					if !reflect.DeepEqual(r1, r2) {
						t.Errorf("autorest: ByIgnoring modified the HTTP Response -- received %v, expected %v", r2, r1)
					}
					return nil
				})
			}
		})(),
		ByIgnoring(),
		ByClosing())
}
Esempio n. 9
0
func TestUpdatePollingState_ClosesTheOriginalResponseBody(t *testing.T) {
	resp := mocks.NewResponse()
	b := resp.Body.(*mocks.Body)
	ps := &pollingState{responseFormat: usesProvisioningStatus}
	updatePollingState(resp, ps)
	if b.IsOpen() {
		t.Fatal("azure: updatePollingState failed to close the original http.Response Body")
	}
}
Esempio n. 10
0
func TestExtractHeaderValueHandlesMissingHeader(t *testing.T) {
	r := mocks.NewResponse()
	v := ""

	if ExtractHeaderValue(mocks.TestHeader, r) != v {
		t.Errorf("autorest: ExtractHeader failed to retrieve the expected header -- expected [%s]%v, received [%s]%v",
			mocks.TestHeader, v, mocks.TestHeader, ExtractHeaderValue(mocks.TestHeader, r))
	}
}
Esempio n. 11
0
func TestExtractHeaderHandlesMissingHeader(t *testing.T) {
	var v []string
	r := mocks.NewResponse()

	if !reflect.DeepEqual(ExtractHeader(mocks.TestHeader, r), v) {
		t.Errorf("autorest: ExtractHeader failed to handle a missing header -- expected %v, received %v",
			v, ExtractHeader(mocks.TestHeader, r))
	}
}
Esempio n. 12
0
func TestClientIsPollingAllowedIgnoresOk(t *testing.T) {
	c := Client{PollingMode: PollUntilAttempts}
	r := mocks.NewResponse()

	err := c.IsPollingAllowed(r)
	if err != nil {
		t.Error("autorest: Client#IsPollingAllowed returned an error for an http.Response that does not require polling")
	}
}
Esempio n. 13
0
func TestByClosingIfErrorDoesNotClosesIfNoErrorOccurs(t *testing.T) {
	r := mocks.NewResponse()
	Respond(r,
		ByClosingIfError())

	if !r.Body.(*mocks.Body).IsOpen() {
		t.Errorf("autorest: ByClosingIfError closed the response body even though no error occurred")
	}
}
Esempio n. 14
0
func TestExtractHeader(t *testing.T) {
	r := mocks.NewResponse()
	v := []string{"v1", "v2", "v3"}
	mocks.SetResponseHeaderValues(r, mocks.TestHeader, v)

	if !reflect.DeepEqual(ExtractHeader(mocks.TestHeader, r), v) {
		t.Errorf("autorest: ExtractHeader failed to retrieve the expected header -- expected [%s]%v, received [%s]%v",
			mocks.TestHeader, v, mocks.TestHeader, ExtractHeader(mocks.TestHeader, r))
	}
}
Esempio n. 15
0
func TestResponseGetPollingLocation(t *testing.T) {
	r := mocks.NewResponse()
	mocks.SetLocationHeader(r, mocks.TestURL)
	ar := Response{Response: r}

	if ar.GetPollingLocation() != mocks.TestURL {
		t.Errorf("autorest: Response#GetPollingLocation failed to return correct URL -- expected %v, received %v",
			mocks.TestURL, ar.GetPollingLocation())
	}
}
Esempio n. 16
0
func TestResponseGetPollingDelayReturnsDefault(t *testing.T) {
	ar := Response{Response: mocks.NewResponse()}

	d1 := 10 * time.Second
	d2 := ar.GetPollingDelay(d1)
	if d1 != d2 {
		t.Errorf("autorest: Response#GetPollingDelay failed to return the default delay -- expected %v, received %v",
			d1, d2)
	}
}
Esempio n. 17
0
func TestExtractHeaderValue(t *testing.T) {
	r := mocks.NewResponse()
	v := "v1"
	mocks.SetResponseHeader(r, mocks.TestHeader, v)

	if ExtractHeaderValue(mocks.TestHeader, r) != v {
		t.Fatalf("autorest: ExtractHeader failed to retrieve the expected header -- expected [%s]%v, received [%s]%v",
			mocks.TestHeader, v, mocks.TestHeader, ExtractHeaderValue(mocks.TestHeader, r))
	}
}
Esempio n. 18
0
func TestExtractRequestID(t *testing.T) {
	uuid := "71FDB9F4-5E49-4C12-B266-DE7B4FD999A6"
	resp := mocks.NewResponse()
	mocks.SetResponseHeader(resp, HeaderRequestID, uuid)

	if ExtractRequestID(resp) != uuid {
		t.Errorf("azure: ExtractRequestID failed to extract the %s -- expected %s, received %s",
			HeaderRequestID, uuid, ExtractRequestID(resp))
	}
}
Esempio n. 19
0
func TestExtractHeaderValueRetrievesFirstValue(t *testing.T) {
	r := mocks.NewResponse()
	v := []string{"v1", "v2", "v3"}
	mocks.SetResponseHeaderValues(r, mocks.TestHeader, v)

	if ExtractHeaderValue(mocks.TestHeader, r) != v[0] {
		t.Errorf("autorest: ExtractHeader failed to retrieve the expected header -- expected [%s]%v, received [%s]%v",
			mocks.TestHeader, v[0], mocks.TestHeader, ExtractHeaderValue(mocks.TestHeader, r))
	}
}
Esempio n. 20
0
func TestByClosing(t *testing.T) {
	r := mocks.NewResponse()
	err := Respond(r, ByClosing())
	if err != nil {
		t.Errorf("autorest: ByClosing failed (%v)", err)
	}
	if r.Body.(*mocks.Body).IsOpen() {
		t.Errorf("autorest: ByClosing did not close the response body")
	}
}
Esempio n. 21
0
func TestByClosingClosesEvenAfterErrors(t *testing.T) {
	var e error

	r := mocks.NewResponse()
	Respond(r,
		withErrorRespondDecorator(&e),
		ByClosing())

	if r.Body.(*mocks.Body).IsOpen() {
		t.Fatalf("autorest: ByClosing did not close the response body after an error occurred")
	}
}
Esempio n. 22
0
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)
	}
}
Esempio n. 23
0
func TestByClosingIfErrorClosesIfAnErrorOccurs(t *testing.T) {
	var e error

	r := mocks.NewResponse()
	Respond(r,
		withErrorRespondDecorator(&e),
		ByClosingIfError())

	if r.Body.(*mocks.Body).IsOpen() {
		t.Errorf("autorest: ByClosingIfError did not close the response body after an error occurred")
	}
}
Esempio n. 24
0
func TestByClosingClosesReturnsNestedErrors(t *testing.T) {
	var e error

	r := mocks.NewResponse()
	err := Respond(r,
		withErrorRespondDecorator(&e),
		ByClosing())

	if err == nil || !reflect.DeepEqual(e, err) {
		t.Errorf("autorest: ByClosing failed to return a nested error")
	}
}
Esempio n. 25
0
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)
	}
}
Esempio n. 26
0
func TestWithErrorUnlessOKEmitsErrorIfNotOK(t *testing.T) {
	r := mocks.NewResponse()
	r.Request = mocks.NewRequest()
	r.Status = "400 BadRequest"
	r.StatusCode = 400

	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)
	}
}
Esempio n. 27
0
func TestWithErrorUnlessStatusCodeEmitsErrorForUnacceptableStatusCode(t *testing.T) {
	r := mocks.NewResponse()
	r.Request = mocks.NewRequest()
	r.Status = "400 BadRequest"
	r.StatusCode = 400

	err := Respond(r,
		WithErrorUnlessStatusCode(200, 300, 500),
		ByClosingIfError())

	if err == nil {
		t.Errorf("autorest: WithErrorUnlessStatusCode failed to return an error for an unacceptable status code (%s)", r.Status)
	}
}
Esempio n. 28
0
func TestWithErrorUnlessStatusCode(t *testing.T) {
	r := mocks.NewResponse()
	r.Request = mocks.NewRequest()
	r.Status = "400 BadRequest"
	r.StatusCode = 400

	err := Respond(r,
		WithErrorUnlessStatusCode(300, 400, 500),
		ByClosingIfError())

	if err != nil {
		t.Errorf("autorest: WithErrorUnlessStatusCode returned an error (%v) for an acceptable status code (%s)", err, r.Status)
	}
}
Esempio n. 29
0
func TestWithErrorUnlessStatusCode(t *testing.T) {
	r := mocks.NewResponse()
	r.Request = mocks.NewRequest()
	r.Status = "400 BadRequest"
	r.StatusCode = http.StatusBadRequest

	err := Respond(r,
		WithErrorUnlessStatusCode(http.StatusBadRequest, http.StatusUnauthorized, http.StatusInternalServerError),
		ByClosingIfError())

	if err != nil {
		t.Fatalf("autorest: WithErrorUnlessStatusCode returned an error (%v) for an acceptable status code (%s)", err, r.Status)
	}
}
Esempio n. 30
0
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)
	}
}