當前位置: 首頁>>代碼示例>>Golang>>正文


Golang mocks.NewSender函數代碼示例

本文整理匯總了Golang中github.com/Azure/azure-sdk-for-go/Godeps/_workspace/src/github.com/Azure/go-autorest/autorest/mocks.NewSender函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewSender函數的具體用法?Golang NewSender怎麽用?Golang NewSender使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewSender函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: ExampleSendWithSender

func ExampleSendWithSender() {
	client := mocks.NewSender()
	client.EmitStatus("202 Accepted", http.StatusAccepted)

	logger := log.New(os.Stdout, "autorest: ", 0)
	na := NullAuthorizer{}

	req, _ := Prepare(&http.Request{},
		AsGet(),
		WithBaseURL("https://microsoft.com/a/b/c/"),
		na.WithAuthorization())

	r, _ := SendWithSender(client, req,
		WithLogging(logger),
		DoErrorIfStatusCode(http.StatusAccepted),
		DoCloseIfError(),
		DoRetryForAttempts(5, time.Duration(0)))

	Respond(r,
		ByClosing())

	// Output:
	// autorest: Sending GET https://microsoft.com/a/b/c/
	// autorest: GET https://microsoft.com/a/b/c/ received 202 Accepted
	// autorest: Sending GET https://microsoft.com/a/b/c/
	// autorest: GET https://microsoft.com/a/b/c/ received 202 Accepted
	// autorest: Sending GET https://microsoft.com/a/b/c/
	// autorest: GET https://microsoft.com/a/b/c/ received 202 Accepted
	// autorest: Sending GET https://microsoft.com/a/b/c/
	// autorest: GET https://microsoft.com/a/b/c/ received 202 Accepted
	// autorest: Sending GET https://microsoft.com/a/b/c/
	// autorest: GET https://microsoft.com/a/b/c/ received 202 Accepted
}
開發者ID:kbxkb,項目名稱:azure-sdk-for-go,代碼行數:33,代碼來源:sender_test.go

示例2: TestClientPollAsNeededPollsForDuration

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())
}
開發者ID:kbxkb,項目名稱:azure-sdk-for-go,代碼行數:25,代碼來源:client_test.go

示例3: TestServicePrincipalTokenRefreshUnmarshals

func TestServicePrincipalTokenRefreshUnmarshals(t *testing.T) {
	spt := newServicePrincipalToken()

	expiresOn := strconv.Itoa(int(time.Now().Add(3600 * time.Second).Sub(expirationBase).Seconds()))
	j := newTokenJSON(expiresOn, "resource")
	resp := mocks.NewResponseWithContent(j)
	c := mocks.NewSender()
	s := autorest.DecorateSender(c,
		(func() autorest.SendDecorator {
			return func(s autorest.Sender) autorest.Sender {
				return autorest.SenderFunc(func(r *http.Request) (*http.Response, error) {
					return resp, nil
				})
			}
		})())
	spt.SetSender(s)

	err := spt.Refresh()
	if err != nil {
		t.Errorf("azure: ServicePrincipalToken#Refresh returned an unexpected error (%v)", err)
	} else if spt.AccessToken != "accessToken" ||
		spt.ExpiresIn != "3600" ||
		spt.ExpiresOn != expiresOn ||
		spt.NotBefore != expiresOn ||
		spt.Resource != "resource" ||
		spt.Type != "Bearer" {
		t.Errorf("azure: ServicePrincipalToken#Refresh failed correctly unmarshal the JSON -- expected %v, received %v",
			j, *spt)
	}
}
開發者ID:kbxkb,項目名稱:azure-sdk-for-go,代碼行數:30,代碼來源:token_test.go

示例4: TestAllInstancesResourceGroupNotFound

func (s *environSuite) TestAllInstancesResourceGroupNotFound(c *gc.C) {
	env := s.openEnviron(c)
	sender := mocks.NewSender()
	sender.EmitStatus("resource group not found", http.StatusNotFound)
	s.sender = azuretesting.Senders{sender}
	_, err := env.AllInstances()
	c.Assert(err, jc.ErrorIsNil)
}
開發者ID:makyo,項目名稱:juju,代碼行數:8,代碼來源:environ_test.go

示例5: TestStopInstancesNotFound

func (s *environSuite) TestStopInstancesNotFound(c *gc.C) {
	env := s.openEnviron(c)
	sender := mocks.NewSender()
	sender.EmitStatus("vm not found", http.StatusNotFound)
	s.sender = azuretesting.Senders{sender, sender, sender}
	err := env.StopInstances("a", "b")
	c.Assert(err, jc.ErrorIsNil)
}
開發者ID:makyo,項目名稱:juju,代碼行數:8,代碼來源:environ_test.go

示例6: NewSenderWithValue

// NewSenderWithValue returns a *mocks.Sender that marshals the provided object
// to JSON and sets it as the content. This function will panic if marshalling
// fails.
func NewSenderWithValue(v interface{}) *MockSender {
	content, err := json.Marshal(v)
	if err != nil {
		panic(err)
	}
	sender := &MockSender{Sender: mocks.NewSender()}
	sender.EmitContent(string(content))
	return sender
}
開發者ID:imoapps,項目名稱:juju,代碼行數:12,代碼來源:senders.go

示例7: TestServicePrincipalTokenSetSender

func TestServicePrincipalTokenSetSender(t *testing.T) {
	spt := newServicePrincipalToken()

	var s autorest.Sender
	s = mocks.NewSender()
	spt.SetSender(s)
	if !reflect.DeepEqual(s, spt.sender) {
		t.Error("azure: ServicePrincipalToken#SetSender did not set the sender")
	}
}
開發者ID:kbxkb,項目名稱:azure-sdk-for-go,代碼行數:10,代碼來源:token_test.go

示例8: TestClientSendReturnsPrepareError

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")
	}
}
開發者ID:kbxkb,項目名稱:azure-sdk-for-go,代碼行數:10,代碼來源:client_test.go

示例9: TestClientSendSends

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")
	}
}
開發者ID:kbxkb,項目名稱:azure-sdk-for-go,代碼行數:10,代碼來源:client_test.go

示例10: TestClientSenderReturnsSetSender

func TestClientSenderReturnsSetSender(t *testing.T) {
	c := Client{}

	s := mocks.NewSender()
	c.Sender = s

	if c.sender() != s {
		t.Error("autorest: Client#sender failed to return set Sender")
	}
}
開發者ID:kbxkb,項目名稱:azure-sdk-for-go,代碼行數:10,代碼來源:client_test.go

示例11: TestClientDoInvokesSender

func TestClientDoInvokesSender(t *testing.T) {
	c := Client{}

	s := mocks.NewSender()
	c.Sender = s

	c.Do(&http.Request{})
	if s.Attempts() != 1 {
		t.Error("autorest: Client#Do failed to invoke the Sender")
	}
}
開發者ID:kbxkb,項目名稱:azure-sdk-for-go,代碼行數:11,代碼來源:client_test.go

示例12: TestClientSendInvokesInspector

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")
	}
}
開發者ID:kbxkb,項目名稱:azure-sdk-for-go,代碼行數:11,代碼來源:client_test.go

示例13: TestClientSendDoesNotPollIfUnnecessary

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())
	}
}
開發者ID:kbxkb,項目名稱:azure-sdk-for-go,代碼行數:11,代碼來源:client_test.go

示例14: TestClientSendReturnsErrorWithUnexpectedStatusCode

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")
	}
}
開發者ID:kbxkb,項目名稱:azure-sdk-for-go,代碼行數:11,代碼來源:client_test.go

示例15: TestClientSendDefaultsToUsingStatusCodeOK

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)
	}
}
開發者ID:kbxkb,項目名稱:azure-sdk-for-go,代碼行數:11,代碼來源:client_test.go


注:本文中的github.com/Azure/azure-sdk-for-go/Godeps/_workspace/src/github.com/Azure/go-autorest/autorest/mocks.NewSender函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。