本文整理汇总了Golang中github.com/SEEK-Jobs/pact-go/provider.NewJSONRequest函数的典型用法代码示例。如果您正苦于以下问题:Golang NewJSONRequest函数的具体用法?Golang NewJSONRequest怎么用?Golang NewJSONRequest使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewJSONRequest函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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")
}
}
示例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")
}
}
示例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)
}
}
示例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)
}
}
示例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()
}
}
示例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")
}
}
示例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())
}
}
示例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)
}
}
示例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
}
示例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")
}
}
示例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")
}
}
示例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")
}
}
示例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")
}
}
示例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")
}
}
示例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")
}
}