• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang provider.NewJSONResponse函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/SEEK-Jobs/pact-go/provider.NewJSONResponse函数的典型用法代码示例。如果您正苦于以下问题:Golang NewJSONResponse函数的具体用法?Golang NewJSONResponse怎么用?Golang NewJSONResponse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NewJSONResponse函数的19个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
	}
}
开发者ID:goodgravy,项目名称:pact-go,代码行数:34,代码来源:builder_test.go


示例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()
	}
}
开发者ID:goodgravy,项目名称:pact-go,代码行数:28,代码来源:provider_service_test.go


示例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
}
开发者ID:goodgravy,项目名称:pact-go,代码行数:11,代码来源:http_server_test.go


示例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")
	}
}
开发者ID:goodgravy,项目名称:pact-go,代码行数:12,代码来源:provider_service_test.go


示例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()
}
开发者ID:compasses,项目名称:GOProjects,代码行数:46,代码来源:pactgenerate.go


示例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)
	}
}
开发者ID:goodgravy,项目名称:pact-go,代码行数:17,代码来源:provider_service_test.go


示例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")
	}
}
开发者ID:goodgravy,项目名称:pact-go,代码行数:19,代码来源:consumer_validator_test.go


示例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")
	}

}
开发者ID:goodgravy,项目名称:pact-go,代码行数:21,代码来源:provider_service_test.go


示例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")
	}
}
开发者ID:goodgravy,项目名称:pact-go,代码行数:21,代码来源:provider_service_test.go


示例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")
	}
}
开发者ID:goodgravy,项目名称:pact-go,代码行数:22,代码来源:provider_service_test.go


示例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)
	}

}
开发者ID:goodgravy,项目名称:pact-go,代码行数:23,代码来源:provider_service_test.go


示例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)
	}

}
开发者ID:goodgravy,项目名称:pact-go,代码行数:14,代码来源:consumer_validator_test.go


示例13: buildTestProviderResponse

func buildTestProviderResponse(status int, h http.Header, body string) *provider.Response {
	pr := provider.NewJSONResponse(200, h)
	pr.SetBody(body)
	return pr
}
开发者ID:SEEK-Jobs,项目名称:pact-go,代码行数:5,代码来源:http_response_matcher_test.go


示例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())
	}
}
开发者ID:goodgravy,项目名称:pact-go,代码行数:8,代码来源:interactions_verifier_test.go


示例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")
	}
}
开发者ID:goodgravy,项目名称:pact-go,代码行数:12,代码来源:interactions_verifier_test.go


示例16: Test_ShouldVerify_MultipleSameInteractionCalls

func Test_ShouldVerify_MultipleSameInteractionCalls(t *testing.T) {
	i, _ := NewInteraction("description", "state", provider.NewJSONRequest("GET", "/", "", nil), provider.NewJSONResponse(200, nil))
	registered := []*Interaction{i}
	requested := []*Interaction{i, i, i}
	if err := verifyInteractions(registered, requested); err != nil {
		if !strings.Contains(err.Error(), fmt.Sprintf(errIntMultipleCalls, i.Description, i.State, len(requested))) {
			t.Errorf("expected message to contain: %s, actual message: %s",
				fmt.Sprintf(errIntMultipleCalls, i.Description, i.State, len(requested)), err.Error())
		}
	} else if err == nil {
		t.Error("expected multiple calls interaction error")
	}
}
开发者ID:goodgravy,项目名称:pact-go,代码行数:13,代码来源:interactions_verifier_test.go


示例17: Test_Validator_ExecutesSetupsAndTeardowns

func Test_Validator_ExecutesSetupsAndTeardowns(t *testing.T) {
	i := 1
	v := newConsumerValidator(func() error {
		if i != 1 {
			t.Errorf("Expected this action to be called at %v position but is at %d", 1, i)
		} else {
			i++
		}
		return nil
	}, func() error {
		if i != 4 {
			t.Errorf("Expected this action to be called at %d position but is at %d", 4, i)
		}
		return nil
	}, DefaultLogger)

	sa := &stateAction{setup: func() error {
		if i != 2 {
			t.Errorf("Expected this action to be called at %d position but is at %d", 2, i)
		} else {
			i++
		}
		return nil
	}, teardown: func() error {
		if i != 3 {
			t.Errorf("Expected this action to be called at %d position but is at %d", 3, i)
		} else {
			i++
		}
		return nil
	}}

	interaction, _ := consumer.NewInteraction("description", "state", provider.NewJSONRequest("Get", "/", "", nil), provider.NewJSONResponse(200, nil))
	f := io.NewPactFile("consumer", "provider", []*consumer.Interaction{interaction})

	s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(interaction.Response.Status)
	}))
	defer s.Close()
	u, _ := url.Parse(s.URL)

	v.ProviderService(&http.Client{}, u)
	if res, err := v.Validate(f, map[string]*stateAction{"state": sa}); err != nil {
		t.Error(err)
	} else if !res {
		t.Error("Validation Failed")
	} else if i != 4 {
		t.Error("Setup and teardown actions were not called correctly")
	}
}
开发者ID:goodgravy,项目名称:pact-go,代码行数:50,代码来源:consumer_validator_test.go


示例18: Test_Validator_ReturnsErrorWhenRequestFails

func Test_Validator_ReturnsErrorWhenRequestFails(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})
	sa := &stateAction{setup: nil, teardown: nil}
	u, _ := url.Parse("http://localhost:54322")

	v.ProviderService(&http.Client{}, u)
	if _, err := v.Validate(f, map[string]*stateAction{"state": sa}); err == nil {
		t.Errorf("expected error whilst making request")
	}
}
开发者ID:goodgravy,项目名称:pact-go,代码行数:12,代码来源:consumer_validator_test.go


示例19: Test_Validator_ThrowsErrorFromSetupsAndTeardowns

func Test_Validator_ThrowsErrorFromSetupsAndTeardowns(t *testing.T) {
	testErr := errors.New("action error")
	fn := func() error {
		return testErr
	}

	sa := &stateAction{setup: nil, teardown: nil}

	interaction, _ := consumer.NewInteraction("description", "state", provider.NewJSONRequest("Get", "/", "", nil), provider.NewJSONResponse(200, nil))
	f := io.NewPactFile("consumer", "provider", []*consumer.Interaction{interaction})

	s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(interaction.Response.Status)
	}))
	defer s.Close()

	u, _ := url.Parse(s.URL)

	//test setup action for every interaction
	v := newConsumerValidator(fn, nil, DefaultLogger)
	v.ProviderService(&http.Client{}, u)
	if _, err := v.Validate(f, map[string]*stateAction{"state": sa}); err == nil {
		t.Errorf("expected %s", testErr)
	} else if err != testErr {
		t.Errorf("expected %s, got %s", testErr, err)
	}

	//test teardown action for every interaction
	v = newConsumerValidator(nil, fn, DefaultLogger)
	v.ProviderService(&http.Client{}, u)
	if _, err := v.Validate(f, map[string]*stateAction{"state": sa}); err == nil {
		t.Errorf("expected %s", testErr)
	} else if err != testErr {
		t.Errorf("expected %s, got %s", testErr, err)
	}

	//test setup action for specific interaction
	sa = &stateAction{setup: fn, teardown: nil}
	v = newConsumerValidator(nil, fn, DefaultLogger)
	v.ProviderService(&http.Client{}, u)
	if _, err := v.Validate(f, map[string]*stateAction{"state": sa}); err == nil {
		t.Errorf("expected %s", testErr)
	} else if err != testErr {
		t.Errorf("expected %s, got %s", testErr, err)
	}

	//test teardown action for every interaction
	sa = &stateAction{setup: nil, teardown: fn}
	v = newConsumerValidator(nil, fn, DefaultLogger)
	v.ProviderService(&http.Client{}, u)
	if _, err := v.Validate(f, map[string]*stateAction{"state": sa}); err == nil {
		t.Errorf("expected %s", testErr)
	} else if err != testErr {
		t.Errorf("expected %s, got %s", testErr, err)
	}
}
开发者ID:goodgravy,项目名称:pact-go,代码行数:56,代码来源:consumer_validator_test.go



注:本文中的github.com/SEEK-Jobs/pact-go/provider.NewJSONResponse函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang utils.PanicOnError函数代码示例发布时间:2022-05-28
下一篇:
Golang provider.NewJSONRequest函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap