本文整理汇总了Golang中github.com/SEEK-Jobs/pact-go/provider.NewJSONResponse函数的典型用法代码示例。如果您正苦于以下问题:Golang NewJSONResponse函数的具体用法?Golang NewJSONResponse怎么用?Golang NewJSONResponse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewJSONResponse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Test_Builder_CanBuild
func Test_Builder_CanBuild(t *testing.T) {
builder := NewConsumerPactBuilder(&BuilderConfig{PactPath: "./pact_examples"}).
ServiceConsumer("chrome browser").
HasPactWith("go api")
ps, _ := builder.GetMockProviderService()
request := provider.NewJSONRequest("GET", "/user", "id=23", nil)
header := make(http.Header)
header.Add("content-type", "application/json")
response := provider.NewJSONResponse(200, header)
response.SetBody(`{ "id": 23, "firstName": "John", "lastName": "Doe" }`)
if err := ps.Given("there is a user with id {23}").
UponReceiving("get request for user with id {23}").
With(*request).
WillRespondWith(*response); err != nil {
t.Error(err)
t.FailNow()
}
request.Query = "id=200"
response.Status = 404
response.Headers = nil
response.ResetContent()
ps.Given("there is no user with id {200}").
UponReceiving("get request for user with id {200}").
With(*request).
WillRespondWith(*response)
if err := builder.Build(); err != nil {
t.Error(err)
}
}
示例2: Test_ProviderService_CanVerifyInteractions
func Test_ProviderService_CanVerifyInteractions(t *testing.T) {
ps := newMockProviderService(&BuilderConfig{})
request := provider.NewJSONRequest("POST", "/luke", "action=attack", nil)
response := provider.NewJSONResponse(200, nil)
if err := ps.Given("Force is strong with Luke Skywalker").
UponReceiving("Destroy death star").
With(*request).
WillRespondWith(*response); err != nil {
t.Error(err)
}
url := ps.start()
defer ps.stop()
client := &http.Client{}
if req, err := http.NewRequest(request.Method, fmt.Sprintf("%s%s?%s", url, request.Path, request.Query), nil); err != nil {
t.Error(err)
t.FailNow()
} else if _, err := client.Do(req); err != nil {
t.Error(err)
t.FailNow()
} else if err := ps.VerifyInteractions(); err != nil {
t.Error(err)
t.FailNow()
}
}
示例3: getFakeInteraction
func getFakeInteraction() *Interaction {
header := make(http.Header)
header.Add("content-type", "application/json")
i, _ := NewInteraction("description of the interaction",
"some state",
provider.NewJSONRequest("GET", "/", "param=xyzmk", header),
provider.NewJSONResponse(201, header))
i.Request.SetBody(`{ "firstName": "John", "lastName": "Doe" }`)
return i
}
示例4: Test_ProviderService_CannotReigsterInteraction_WithInvalidData
func Test_ProviderService_CannotReigsterInteraction_WithInvalidData(t *testing.T) {
ps := newMockProviderService(&BuilderConfig{})
request := provider.NewJSONRequest("POST", "/luke", "action=attack", nil)
response := provider.NewJSONResponse(200, nil)
if err := ps.Given("Force is strong with Luke Skywalker").
With(*request).
WillRespondWith(*response); err == nil {
t.Error("Should not be able to register interaction with empty description")
}
}
示例5: RunPact
func (middleware *offlinemiddleware) RunPact(builder pact.Builder, path, method string, reqBody, respBody interface{}, statusCode int,
consumerName, providerName string) {
ms, msUrl := builder.GetMockProviderService()
request := provider.NewJSONRequest(method, path, "", nil)
if reqBody != nil {
err := request.SetBody(reqBody)
if err != nil {
log.Println("Set request error ", err, " reqBody ", respBody)
}
}
header := make(http.Header)
header.Add("content-type", "application/json")
response := provider.NewJSONResponse(statusCode, header)
if respBody != nil {
err := response.SetBody(respBody)
if err != nil {
log.Println("Set Response error ", err, " respBody ", respBody)
}
}
//Register interaction for this test scope
if err := ms.Given(consumerName).
UponReceiving(providerName).
With(*request).
WillRespondWith(*response); err != nil {
log.Println(err)
}
//log.Println("Register: ", " Request ", string(req), " response ", respBody)
//test
client := &ProviderAPIClient{baseURL: msUrl}
if err := client.ClientRun(method, path, reqBody); err != nil {
log.Println(err)
}
//Verify registered interaction
if err := ms.VerifyInteractions(); err != nil {
log.Println(err)
}
//Clear interaction for this test scope, if you need to register and verify another interaction for another test scope
ms.ClearInteractions()
}
示例6: Test_ProviderService_CanReigsterInteraction_WithValidData
func Test_ProviderService_CanReigsterInteraction_WithValidData(t *testing.T) {
ps := newMockProviderService(&BuilderConfig{})
header := make(http.Header)
header.Add("content-type", "payload/nuclear")
request := provider.NewJSONRequest("POST", "/luke", "action=attack", header)
request.SetBody(`{ "simulation": false, "target": "Death Star" }`)
response := provider.NewJSONResponse(200, nil)
if err := ps.Given("Force is strong with Luke Skywalker").
UponReceiving("Destroy death star").
With(*request).
WillRespondWith(*response); err != nil {
t.Error(err)
}
}
示例7: Test_Validator_ReturnsErrorFromResponseMatcher
func Test_Validator_ReturnsErrorFromResponseMatcher(t *testing.T) {
v := newConsumerValidator(nil, nil, DefaultLogger)
r := provider.NewJSONResponse(200, nil)
r.SetBody(`{"name":"John Doe"}`)
interaction, _ := consumer.NewInteraction("description", "state", provider.NewJSONRequest("Get", "/", "", nil), r)
f := io.NewPactFile("consumer", "provider", []*consumer.Interaction{interaction})
sa := &stateAction{setup: nil, teardown: nil}
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(interaction.Response.Status)
w.Write([]byte("bad json"))
}))
defer s.Close()
u, _ := url.Parse(s.URL)
v.ProviderService(&http.Client{}, u)
if _, err := v.Validate(f, map[string]*stateAction{"state": sa}); err == nil {
t.Errorf("expected error from response matcher")
}
}
示例8: Test_ProviderService_CannotReigster_DuplicateInteraction
func Test_ProviderService_CannotReigster_DuplicateInteraction(t *testing.T) {
ps := newMockProviderService(&BuilderConfig{})
request := provider.NewJSONRequest("POST", "/luke", "action=attack", nil)
response := provider.NewJSONResponse(200, nil)
if err := ps.Given("Force is strong with Luke Skywalker").
UponReceiving("Destroy death star").
With(*request).
WillRespondWith(*response); err != nil {
t.Error(err)
}
if err := ps.Given("Force is strong with Luke Skywalker").
UponReceiving("Destroy death star").
With(*request).
WillRespondWith(*response); err == nil {
t.Error("Should not allow to register duplicate interaction")
}
}
示例9: Test_ProviderService_CanResetTransientState_AfterRegistration
func Test_ProviderService_CanResetTransientState_AfterRegistration(t *testing.T) {
ps := newMockProviderService(&BuilderConfig{})
header := make(http.Header)
header.Add("content-type", "payload/nuclear")
request := provider.NewJSONRequest("POST", "/luke", "action=attack", header)
request.SetBody(`{ "simulation": false, "target": "Death Star" }`)
response := provider.NewJSONResponse(200, nil)
if err := ps.Given("Force is strong with Luke Skywalker").
UponReceiving("Destroy death star").
With(*request).
WillRespondWith(*response); err != nil {
t.Error(err)
}
if ps.state != "" || ps.description != "" || ps.providerRequest != nil || ps.providerResponse != nil {
t.Error("Provider services transient state is not cleared after interaction registration")
}
}
示例10: Test_ProviderService_CanClearInteractions
func Test_ProviderService_CanClearInteractions(t *testing.T) {
ps := newMockProviderService(&BuilderConfig{})
request := provider.NewJSONRequest("POST", "/luke", "action=attack", nil)
response := provider.NewJSONResponse(200, nil)
if err := ps.Given("Force is strong with Luke Skywalker").
UponReceiving("Destroy death star").
With(*request).
WillRespondWith(*response); err != nil {
t.Error(err)
}
ps.ClearInteractions()
if !ps.service.IsTestScopeClear() {
t.Error("Interactions have not been cleared")
}
if ps.state != "" || ps.description != "" || ps.providerRequest != nil || ps.providerResponse != nil {
t.Error("Provider services transient state is not cleared after interaction registration")
}
}
示例11: Test_ProviderService_CanReigsterDuplicateInteraction_InDifferentScope
func Test_ProviderService_CanReigsterDuplicateInteraction_InDifferentScope(t *testing.T) {
ps := newMockProviderService(&BuilderConfig{})
request := provider.NewJSONRequest("POST", "/luke", "action=attack", nil)
response := provider.NewJSONResponse(200, nil)
if err := ps.Given("Force is strong with Luke Skywalker").
UponReceiving("Destroy death star").
With(*request).
WillRespondWith(*response); err != nil {
t.Error(err)
}
ps.ClearInteractions()
if err := ps.Given("Force is strong with Luke Skywalker").
UponReceiving("Destroy death star").
With(*request).
WillRespondWith(*response); err != nil {
t.Error(err)
}
}
示例12: Test_Validator_ReturnsErrorWhenProvierStateIsMissing
func Test_Validator_ReturnsErrorWhenProvierStateIsMissing(t *testing.T) {
v := newConsumerValidator(nil, nil, DefaultLogger)
interaction, _ := consumer.NewInteraction("description", "state", provider.NewJSONRequest("Get", "/", "", nil), provider.NewJSONResponse(200, nil))
f := io.NewPactFile("consumer", "provider", []*consumer.Interaction{interaction})
expErrMsg := fmt.Sprintf(errNotFoundProviderStateMsg, interaction.State)
v.ProviderService(&http.Client{}, &url.URL{})
if _, err := v.Validate(f, nil); err == nil {
t.Errorf("expected %s", expErrMsg)
} else if err.Error() != expErrMsg {
t.Errorf("expected %s, got %s", expErrMsg, err)
}
}
示例13: buildTestProviderResponse
func buildTestProviderResponse(status int, h http.Header, body string) *provider.Response {
pr := provider.NewJSONResponse(200, h)
pr.SetBody(body)
return pr
}
示例14: Test_ShouldVerify_Interactions
func Test_ShouldVerify_Interactions(t *testing.T) {
i, _ := NewInteraction("description", "state", provider.NewJSONRequest("GET", "/", "", nil), provider.NewJSONResponse(200, nil))
registered := []*Interaction{i}
requested := []*Interaction{i}
if err := verifyInteractions(registered, requested); err != nil {
t.Errorf("expected verfication to succed, got error: %s", err.Error())
}
}
示例15: Test_ShouldVerify_UnexpectedInteractionVerfications
func Test_ShouldVerify_UnexpectedInteractionVerfications(t *testing.T) {
i, _ := NewInteraction("description", "state", provider.NewJSONRequest("GET", "/", "", nil), provider.NewJSONResponse(200, nil))
registered := []*Interaction{}
requested := []*Interaction{i}
if err := verifyInteractions(registered, requested); err != nil {
if !strings.Contains(err.Error(), errIntShouldNotBeCalled) {
t.Errorf("expected message to contain: %s, actual message: %s", errIntShouldNotBeCalled, err.Error())
}
} else if err == nil {
t.Error("expected unexpected calls interaction error")
}
}