本文整理汇总了Golang中github.com/SEEK-Jobs/pact-go/provider.NewJSONRequest函数的典型用法代码示例。如果您正苦于以下问题:Golang NewJSONRequest函数的具体用法?Golang NewJSONRequest怎么用?Golang NewJSONRequest使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewJSONRequest函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: Test_UrlIsDifferent_WillNotMatch
func Test_UrlIsDifferent_WillNotMatch(t *testing.T) {
a := provider.NewJSONRequest("GET", "", "", nil)
b := provider.NewJSONRequest("GET", "/", "", nil)
result, err := MatchRequest(a, b)
if err != nil {
t.Error(err)
t.FailNow()
}
if result {
t.Error("The request should not match")
}
}
开发者ID:SEEK-Jobs,项目名称:pact-go,代码行数:15,代码来源:http_request_matcher_test.go
示例2: Test_ExpectedNoBodyButActualRequestHasBody_WillMatch
func Test_ExpectedNoBodyButActualRequestHasBody_WillMatch(t *testing.T) {
a := provider.NewJSONRequest("GET", "/test", "", nil)
b := provider.NewJSONRequest("GET", "/test", "", nil)
b.SetBody(`{"name": "John"}`)
result, err := MatchRequest(a, b)
if err != nil {
t.Error(err)
t.FailNow()
}
if !result {
t.Error("The request should match")
}
}
开发者ID:SEEK-Jobs,项目名称:pact-go,代码行数:16,代码来源:http_request_matcher_test.go
示例3: Test_BodyIsDifferent_WillNotMatch
func Test_BodyIsDifferent_WillNotMatch(t *testing.T) {
a := provider.NewJSONRequest("GET", "/test", "", nil)
a.SetBody(`{"name": "John", "age": 12 }`)
b := provider.NewJSONRequest("GET", "/test", "", nil)
b.SetBody(`{"name": "John"}`)
result, err := MatchRequest(a, b)
if result {
t.Error("The request should not match")
}
if err != nil {
t.Error(err)
}
}
开发者ID:SEEK-Jobs,项目名称:pact-go,代码行数:16,代码来源:http_request_matcher_test.go
示例4: 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
示例5: 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
示例6: Test_HeadersAreMissing_WillNotMatch
func Test_HeadersAreMissing_WillNotMatch(t *testing.T) {
aHeader := make(http.Header)
aHeader.Add("content-type", "application/json")
a := provider.NewJSONRequest("GET", "/test", "", aHeader)
b := provider.NewJSONRequest("GET", "/test", "", nil)
result, err := MatchRequest(a, b)
if err != nil {
t.Error(err)
t.FailNow()
}
if result {
t.Error("The request should not match")
}
}
开发者ID:SEEK-Jobs,项目名称:pact-go,代码行数:17,代码来源:http_request_matcher_test.go
示例7: 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
示例8: 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
示例9: 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
示例10: Test_Validator_ReturnsErrorWhenRequestCreationFails
func Test_Validator_ReturnsErrorWhenRequestCreationFails(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}
v.ProviderService(&http.Client{}, &url.URL{})
if _, err := v.Validate(f, map[string]*stateAction{"state": sa}); err == nil {
t.Errorf("expected error whilst creating the request")
}
}
开发者ID:goodgravy,项目名称:pact-go,代码行数:11,代码来源:consumer_validator_test.go
示例11: 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
示例12: 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
示例13: 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
示例14: Test_AllHeadersFound_WillMatch
func Test_AllHeadersFound_WillMatch(t *testing.T) {
aHeader := make(http.Header)
bHeader := make(http.Header)
aHeader.Add("content-type", "application/json")
bHeader.Add("content-type", "application/json")
bHeader.Add("extra-header", "value")
a := provider.NewJSONRequest("GET", "/test", "", aHeader)
b := provider.NewJSONRequest("GET", "/test", "", bHeader)
result, err := MatchRequest(a, b)
if err != nil {
t.Error(err)
t.FailNow()
}
if !result {
t.Error("The request should match")
}
}
开发者ID:SEEK-Jobs,项目名称:pact-go,代码行数:22,代码来源:http_request_matcher_test.go
示例15: 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
示例16: 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
示例17: 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
示例18: 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
示例19: 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
示例20: 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
注:本文中的github.com/SEEK-Jobs/pact-go/provider.NewJSONRequest函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论