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


Golang mocks.NewResponseWithContent函數代碼示例

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


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

示例1: Do

func (s *deviceTokenSender) Do(req *http.Request) (*http.Response, error) {
	var resp *http.Response
	if s.attempts < 1 {
		s.attempts++
		resp = mocks.NewResponseWithContent(errorDeviceTokenResponse(s.errorString))
	} else {
		resp = mocks.NewResponseWithContent(MockDeviceTokenResponse)
	}
	return resp, nil
}
開發者ID:Azure,項目名稱:go-autorest,代碼行數:10,代碼來源:devicetoken_test.go

示例2: TestWithErrorUnlessStatusCode_NotAnAzureError

func TestWithErrorUnlessStatusCode_NotAnAzureError(t *testing.T) {
	body := `<html>
		<head>
			<title>IIS Error page</title>
		</head>
		<body>Some non-JSON error page</body>
	</html>`
	r := mocks.NewResponseWithContent(body)
	r.Request = mocks.NewRequest()
	r.StatusCode = http.StatusBadRequest
	r.Status = http.StatusText(r.StatusCode)

	err := autorest.Respond(r,
		WithErrorUnlessStatusCode(http.StatusOK),
		autorest.ByClosing())
	ok, _ := err.(*RequestError)
	if ok != nil {
		t.Fatalf("azure: azure.RequestError returned from malformed response: %v", err)
	}

	// the error body should still be there
	defer r.Body.Close()
	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		t.Fatal(err)
	}
	if string(b) != body {
		t.Fatalf("response body is wrong. got=%q exptected=%q", string(b), body)
	}
}
開發者ID:Azure,項目名稱:go-autorest,代碼行數:30,代碼來源:azure_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.NewClient()
	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:oaastest,項目名稱:go-autorest,代碼行數:30,代碼來源:token_test.go

示例4: TestRequestErrorString_WithError

func TestRequestErrorString_WithError(t *testing.T) {
	j := `{
		"error": {
			"code": "InternalError",
			"message": "Conflict",
			"details": [{"code": "conflict1", "message":"error message1"}]
		}
	}`
	uuid := "71FDB9F4-5E49-4C12-B266-DE7B4FD999A6"
	r := mocks.NewResponseWithContent(j)
	mocks.SetResponseHeader(r, HeaderRequestID, uuid)
	r.Request = mocks.NewRequest()
	r.StatusCode = http.StatusInternalServerError
	r.Status = http.StatusText(r.StatusCode)

	err := autorest.Respond(r,
		WithErrorUnlessStatusCode(http.StatusOK),
		autorest.ByClosing())

	if err == nil {
		t.Fatalf("azure: returned nil error for proper error response")
	}
	azErr, _ := err.(*RequestError)
	expected := "autorest/azure: Service returned an error. Status=500 Code=\"InternalError\" Message=\"Conflict\" Details=[{\"code\":\"conflict1\",\"message\":\"error message1\"}]"
	if expected != azErr.Error() {
		t.Fatalf("azure: send wrong RequestError.\nexpected=%v\ngot=%v", expected, azErr.Error())
	}
}
開發者ID:Azure,項目名稱:go-autorest,代碼行數:28,代碼來源:azure_test.go

示例5: TestUpdatePollingState_ReturnsAnErrorIfOneOccurs

func TestUpdatePollingState_ReturnsAnErrorIfOneOccurs(t *testing.T) {
	resp := mocks.NewResponseWithContent(operationResourceIllegal)
	err := updatePollingState(resp, &pollingState{})
	if err == nil {
		t.Fatalf("azure: updatePollingState failed to return an error after a JSON parsing error")
	}
}
開發者ID:Azure,項目名稱:go-autorest,代碼行數:7,代碼來源:async_test.go

示例6: TestWithErrorUnlessStatusCode_FoundAzureErrorWithDetails

func TestWithErrorUnlessStatusCode_FoundAzureErrorWithDetails(t *testing.T) {
	j := `{
		"error": {
			"code": "InternalError",
			"message": "Azure is having trouble right now.",
			"details": [{"code": "conflict1", "message":"error message1"}, 
						{"code": "conflict2", "message":"error message2"}]
		}
	}`
	uuid := "71FDB9F4-5E49-4C12-B266-DE7B4FD999A6"
	r := mocks.NewResponseWithContent(j)
	mocks.SetResponseHeader(r, HeaderRequestID, uuid)
	r.Request = mocks.NewRequest()
	r.StatusCode = http.StatusInternalServerError
	r.Status = http.StatusText(r.StatusCode)

	err := autorest.Respond(r,
		WithErrorUnlessStatusCode(http.StatusOK),
		autorest.ByClosing())

	if err == nil {
		t.Fatalf("azure: returned nil error for proper error response")
	}
	azErr, ok := err.(*RequestError)
	if !ok {
		t.Fatalf("azure: returned error is not azure.RequestError: %T", err)
	}

	if expected := "InternalError"; azErr.ServiceError.Code != expected {
		t.Fatalf("azure: wrong error code. expected=%q; got=%q", expected, azErr.ServiceError.Code)
	}
	if azErr.ServiceError.Message == "" {
		t.Fatalf("azure: error message is not unmarshaled properly")
	}
	b, _ := json.Marshal(*azErr.ServiceError.Details)
	if string(b) != `[{"code":"conflict1","message":"error message1"},{"code":"conflict2","message":"error message2"}]` {
		t.Fatalf("azure: error details is not unmarshaled properly")
	}

	if expected := http.StatusInternalServerError; azErr.StatusCode != expected {
		t.Fatalf("azure: got wrong StatusCode=%v Expected=%d", azErr.StatusCode, expected)
	}
	if expected := uuid; azErr.RequestID != expected {
		t.Fatalf("azure: wrong request ID in error. expected=%q; got=%q", expected, azErr.RequestID)
	}

	_ = azErr.Error()

	// the error body should still be there
	defer r.Body.Close()
	b, err = ioutil.ReadAll(r.Body)
	if err != nil {
		t.Fatal(err)
	}
	if string(b) != j {
		t.Fatalf("response body is wrong. got=%q expected=%q", string(b), j)
	}

}
開發者ID:Azure,項目名稱:go-autorest,代碼行數:59,代碼來源:azure_test.go

示例7: 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
}
開發者ID:bac,項目名稱:juju,代碼行數:12,代碼來源:senders.go

示例8: TestUpdatePollingState_ReturnsSuccessForSuccessfulOperationResourceState

func TestUpdatePollingState_ReturnsSuccessForSuccessfulOperationResourceState(t *testing.T) {
	resp := mocks.NewResponseWithContent(fmt.Sprintf(operationResourceFormat, operationSucceeded))
	resp.StatusCode = 42
	ps := &pollingState{responseFormat: usesOperationResponse}
	updatePollingState(resp, ps)
	if !ps.hasSucceeded() {
		t.Fatalf("azure: updatePollingState failed to return a successful pollingState for the '%s' state", operationSucceeded)
	}
}
開發者ID:Azure,項目名稱:go-autorest,代碼行數:9,代碼來源:async_test.go

示例9: TestUpdatePollingState_ReturnsFailedWhenProvisioningStateFieldIsAbsentForUnknownStatusCodes

func TestUpdatePollingState_ReturnsFailedWhenProvisioningStateFieldIsAbsentForUnknownStatusCodes(t *testing.T) {
	resp := mocks.NewResponseWithContent(pollingStateEmpty)
	resp.StatusCode = 42
	ps := &pollingState{responseFormat: usesProvisioningStatus}
	updatePollingState(resp, ps)
	if !ps.hasTerminated() || ps.hasSucceeded() {
		t.Fatalf("azure: updatePollingState did not return failed when the provisionState field is absent for an unknown Status Code")
	}
}
開發者ID:Azure,項目名稱:go-autorest,代碼行數:9,代碼來源:async_test.go

示例10: TestUpdatePollingState_ReturnsInProgressWhenProvisioningStateFieldIsAbsentForAccepted

func TestUpdatePollingState_ReturnsInProgressWhenProvisioningStateFieldIsAbsentForAccepted(t *testing.T) {
	resp := mocks.NewResponseWithContent(pollingStateEmpty)
	resp.StatusCode = http.StatusAccepted
	ps := &pollingState{responseFormat: usesProvisioningStatus}
	updatePollingState(resp, ps)
	if ps.hasTerminated() {
		t.Fatalf("azure: updatePollingState returned terminated when the provisionState field is absent for Status Code Accepted")
	}
}
開發者ID:Azure,項目名稱:go-autorest,代碼行數:9,代碼來源:async_test.go

示例11: TestByUnmarshallingJSONEmptyInput

func TestByUnmarshallingJSONEmptyInput(t *testing.T) {
	v := &mocks.T{}
	r := mocks.NewResponseWithContent(``)
	err := Respond(r,
		ByUnmarshallingJSON(v),
		ByClosing())
	if err != nil {
		t.Fatalf("autorest: ByUnmarshallingJSON failed to return nil in case of empty JSON (%v)", err)
	}
}
開發者ID:Azure,項目名稱:go-autorest,代碼行數:10,代碼來源:responder_test.go

示例12: TestUpdatePollingState_ReturnsInProgressForAllOtherOperationResourceStates

func TestUpdatePollingState_ReturnsInProgressForAllOtherOperationResourceStates(t *testing.T) {
	s := "not a recognized state"
	resp := mocks.NewResponseWithContent(fmt.Sprintf(operationResourceFormat, s))
	resp.StatusCode = 42
	ps := &pollingState{responseFormat: usesOperationResponse}
	updatePollingState(resp, ps)
	if ps.hasTerminated() {
		t.Fatalf("azure: updatePollingState returned terminated for unknown state '%s'", s)
	}
}
開發者ID:Azure,項目名稱:go-autorest,代碼行數:10,代碼來源:async_test.go

示例13: TestByUnmarhallingJSONIncludesJSONInErrors

func TestByUnmarhallingJSONIncludesJSONInErrors(t *testing.T) {
	v := &mocks.T{}
	j := jsonT[0 : len(jsonT)-2]
	r := mocks.NewResponseWithContent(j)
	err := Respond(r,
		ByUnmarshallingJSON(v),
		ByClosing())
	if err == nil || !strings.Contains(err.Error(), j) {
		t.Errorf("autorest: ByUnmarshallingJSON failed to return JSON in error (%v)", err)
	}
}
開發者ID:ahmetalpbalkan,項目名稱:go-autorest,代碼行數:11,代碼來源:responder_test.go

示例14: TestByUnmarshallingXMLIncludesXMLInErrors

func TestByUnmarshallingXMLIncludesXMLInErrors(t *testing.T) {
	v := &mocks.T{}
	x := xmlT[0 : len(xmlT)-2]
	r := mocks.NewResponseWithContent(x)
	err := Respond(r,
		ByUnmarshallingXML(v),
		ByClosing())
	if err == nil || !strings.Contains(err.Error(), x) {
		t.Fatalf("autorest: ByUnmarshallingXML failed to return XML in error (%v)", err)
	}
}
開發者ID:Azure,項目名稱:go-autorest,代碼行數:11,代碼來源:responder_test.go

示例15: TestUpdatePollingState_ReturnsSuccessWhenProvisioningStateFieldIsAbsentForSuccessStatusCodes

func TestUpdatePollingState_ReturnsSuccessWhenProvisioningStateFieldIsAbsentForSuccessStatusCodes(t *testing.T) {
	for _, sc := range []int{http.StatusOK, http.StatusCreated, http.StatusNoContent} {
		resp := mocks.NewResponseWithContent(pollingStateEmpty)
		resp.StatusCode = sc
		ps := &pollingState{responseFormat: usesProvisioningStatus}
		updatePollingState(resp, ps)
		if !ps.hasSucceeded() {
			t.Fatalf("azure: updatePollingState failed to return success when the provisionState field is absent for Status Code %d", sc)
		}
	}
}
開發者ID:Azure,項目名稱:go-autorest,代碼行數:11,代碼來源:async_test.go


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