本文整理汇总了Golang中github.com/azure/go-autorest/autorest/mocks.NewSender函数的典型用法代码示例。如果您正苦于以下问题:Golang NewSender函数的具体用法?Golang NewSender怎么用?Golang NewSender使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewSender函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestInstanceClosePorts
func (s *instanceSuite) TestInstanceClosePorts(c *gc.C) {
inst := s.getInstance(c)
sender := mocks.NewSender()
notFoundSender := mocks.NewSender()
notFoundSender.AppendResponse(mocks.NewResponseWithStatus(
"rule not found", http.StatusNotFound,
))
s.sender = azuretesting.Senders{sender, notFoundSender}
err := inst.ClosePorts("0", []jujunetwork.PortRange{{
Protocol: "tcp",
FromPort: 1000,
ToPort: 1000,
}, {
Protocol: "udp",
FromPort: 1000,
ToPort: 2000,
}})
c.Assert(err, jc.ErrorIsNil)
c.Assert(s.requests, gc.HasLen, 2)
c.Assert(s.requests[0].Method, gc.Equals, "DELETE")
c.Assert(s.requests[0].URL.Path, gc.Equals, securityRulePath("machine-0-tcp-1000"))
c.Assert(s.requests[1].Method, gc.Equals, "DELETE")
c.Assert(s.requests[1].URL.Path, gc.Equals, securityRulePath("machine-0-udp-1000-2000"))
}
示例2: TestStopInstancesNotFound
func (s *environSuite) TestStopInstancesNotFound(c *gc.C) {
env := s.openEnviron(c)
sender0 := mocks.NewSender()
sender0.AppendResponse(mocks.NewResponseWithStatus(
"vm not found", http.StatusNotFound,
))
sender1 := mocks.NewSender()
sender1.AppendResponse(mocks.NewResponseWithStatus(
"vm not found", http.StatusNotFound,
))
s.sender = azuretesting.Senders{sender0, sender1}
err := env.StopInstances("a", "b")
c.Assert(err, jc.ErrorIsNil)
}
示例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.Fatalf("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.Fatalf("azure: ServicePrincipalToken#Refresh failed correctly unmarshal the JSON -- expected %v, received %v",
j, *spt)
}
}
示例4: TestServicePrincipalTokenRefreshUsesPOST
func TestServicePrincipalTokenRefreshUsesPOST(t *testing.T) {
spt := newServicePrincipalToken()
body := mocks.NewBody("")
resp := mocks.NewResponseWithBodyAndStatus(body, 200, "OK")
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) {
if r.Method != "POST" {
t.Fatalf("azure: ServicePrincipalToken#Refresh did not correctly set HTTP method -- expected %v, received %v", "POST", r.Method)
}
return resp, nil
})
}
})())
spt.SetSender(s)
spt.Refresh()
if body.IsOpen() {
t.Fatalf("the response was not closed!")
}
}
示例5: TestDoPollForStatusCodes_CanBeCanceled
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")
}
}
示例6: 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())
}
示例7: 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
}
示例8: TestDoPollForAsynchronous_StopsPollingIfItReceivesAnInvalidOperationResource
func TestDoPollForAsynchronous_StopsPollingIfItReceivesAnInvalidOperationResource(t *testing.T) {
r1 := newAsynchronousResponse()
r2 := newOperationResourceResponse("busy")
r3 := newOperationResourceResponse("busy")
r3.Body = mocks.NewBody(operationResourceIllegal)
r4 := newOperationResourceResponse(operationSucceeded)
client := mocks.NewSender()
client.AppendResponse(r1)
client.AppendAndRepeatResponse(r2, 2)
client.AppendAndRepeatResponse(r3, 1)
client.AppendAndRepeatResponse(r4, 1)
r, err := autorest.SendWithSender(client, mocks.NewRequest(),
DoPollForAsynchronous(time.Millisecond))
if client.Attempts() > 4 {
t.Fatalf("azure: DoPollForAsynchronous failed to stop polling after receiving an invalid OperationResource")
}
if err == nil {
t.Fatalf("azure: DoPollForAsynchronous failed to return an error after receving an invalid OperationResource")
}
autorest.Respond(r,
autorest.ByClosing())
}
示例9: TestDoPollForAsynchronous_ReturnsErrorForLastErrorResponse
func TestDoPollForAsynchronous_ReturnsErrorForLastErrorResponse(t *testing.T) {
// Return error code and message if error present in last response
r1 := newAsynchronousResponse()
r1.Header.Del(http.CanonicalHeaderKey(headerAsyncOperation))
r2 := newProvisioningStatusResponse("busy")
r2.Header.Del(http.CanonicalHeaderKey(headerAsyncOperation))
r3 := newAsynchronousResponseWithError()
r3.Header.Del(http.CanonicalHeaderKey(headerAsyncOperation))
client := mocks.NewSender()
client.AppendResponse(r1)
client.AppendAndRepeatResponse(r2, 2)
client.AppendAndRepeatResponse(r3, 1)
r, err := autorest.SendWithSender(client, mocks.NewRequest(),
DoPollForAsynchronous(time.Millisecond))
expected := makeLongRunningOperationErrorString("InvalidParameter", "tom-service-DISCOVERY-server-base-v1.core.local' is not a valid captured VHD blob name prefix.")
if err.Error() != expected {
t.Fatalf("azure: DoPollForAsynchronous failed to return an appropriate error message for an unknown error. \n expected=%q \n got=%q",
expected, err.Error())
}
autorest.Respond(r,
autorest.ByClosing())
}
示例10: TestDoPollForAsynchronous_ReturnsAnUnknownErrorForFailedOperations
func TestDoPollForAsynchronous_ReturnsAnUnknownErrorForFailedOperations(t *testing.T) {
// Return unknown error if error not present in last response
r1 := newAsynchronousResponse()
r1.Header.Del(http.CanonicalHeaderKey(headerAsyncOperation))
r2 := newProvisioningStatusResponse("busy")
r2.Header.Del(http.CanonicalHeaderKey(headerAsyncOperation))
r3 := newProvisioningStatusResponse(operationFailed)
r3.Header.Del(http.CanonicalHeaderKey(headerAsyncOperation))
client := mocks.NewSender()
client.AppendResponse(r1)
client.AppendAndRepeatResponse(r2, 2)
client.AppendAndRepeatResponse(r3, 1)
r, err := autorest.SendWithSender(client, mocks.NewRequest(),
DoPollForAsynchronous(time.Millisecond))
expected := makeLongRunningOperationErrorString("Unknown", "None")
if err.Error() != expected {
t.Fatalf("azure: DoPollForAsynchronous failed to return an appropriate error message for an unknown error. \n expected=%q \n got=%q",
expected, err.Error())
}
autorest.Respond(r,
autorest.ByClosing())
}
示例11: TestDoPollForAsynchronous_CanBeCanceled
func TestDoPollForAsynchronous_CanBeCanceled(t *testing.T) {
cancel := make(chan struct{})
delay := 5 * time.Second
r1 := newAsynchronousResponse()
client := mocks.NewSender()
client.AppendResponse(r1)
client.AppendAndRepeatResponse(newOperationResourceResponse("Busy"), -1)
var wg sync.WaitGroup
wg.Add(1)
start := time.Now()
go func() {
req := mocks.NewRequest()
req.Cancel = cancel
wg.Done()
r, _ := autorest.SendWithSender(client, req,
DoPollForAsynchronous(10*time.Second))
autorest.Respond(r,
autorest.ByClosing())
}()
wg.Wait()
close(cancel)
time.Sleep(5 * time.Millisecond)
if time.Since(start) >= delay {
t.Fatalf("azure: DoPollForAsynchronous failed to cancel")
}
}
示例12: oauthConfigSender
func oauthConfigSender() autorest.Sender {
sender := mocks.NewSender()
resp := mocks.NewResponseWithStatus("", http.StatusUnauthorized)
mocks.SetResponseHeaderValues(resp, "WWW-Authenticate", []string{
`authorization_uri="https://testing.invalid/` + fakeTenantId + `"`,
})
sender.AppendResponse(resp)
return sender
}
示例13: 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.AppendResponse(mocks.NewResponseWithContent(string(content)))
return sender
}
示例14: TestDeviceCodeReturnsErrorIfSendingFails
func TestDeviceCodeReturnsErrorIfSendingFails(t *testing.T) {
sender := mocks.NewSender()
sender.SetError(fmt.Errorf("this is an error"))
client := &autorest.Client{Sender: sender}
_, err := InitiateDeviceAuth(client, TestOAuthConfig, TestClientID, TestResource)
if err == nil || !strings.Contains(err.Error(), errCodeSendingFails) {
t.Fatalf("azure: failed to get correct error expected(%s) actual(%s)", errCodeSendingFails, err.Error())
}
}
示例15: 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")
}
}