-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support binary bodies in async plugin messages
Fixes #265
- Loading branch information
Showing
5 changed files
with
137 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//go:build consumer | ||
// +build consumer | ||
|
||
package protobuf | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
message "github.com/pact-foundation/pact-go/v2/message/v4" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var dir, _ = os.Getwd() | ||
|
||
func TestPluginMessageConsumer(t *testing.T) { | ||
p, _ := message.NewAsynchronousPact(message.Config{ | ||
Consumer: "protobufmessageconsumer", | ||
Provider: "protobufmessageprovider", | ||
PactDir: filepath.ToSlash(fmt.Sprintf("%s/../pacts", dir)), | ||
}) | ||
|
||
dir, _ := os.Getwd() | ||
path := fmt.Sprintf("%s/../grpc/routeguide/route_guide.proto", dir) | ||
|
||
protoMessage := `{ | ||
"pact:proto": "` + path + `", | ||
"pact:message-type": "Feature", | ||
"pact:content-type": "application/protobuf", | ||
"name": "notEmpty('Big Tree')", | ||
"location": { | ||
"latitude": "matching(number, 180)", | ||
"longitude": "matching(number, 200)" | ||
} | ||
}` | ||
|
||
err := p.AddAsynchronousMessage(). | ||
Given("the world exists"). | ||
ExpectsToReceive("feature message"). | ||
UsingPlugin(message.PluginConfig{ | ||
Plugin: "protobuf", | ||
Version: "0.3.4", | ||
}). | ||
WithContents(protoMessage, "application/protobuf"). | ||
ExecuteTest(t, func(m message.AsynchronousMessage) error { | ||
// TODO: normally would actually read/consume the message | ||
return nil | ||
}) | ||
|
||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//go:build provider | ||
// +build provider | ||
|
||
package protobuf | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/golang/protobuf/proto" | ||
"github.com/pact-foundation/pact-go/v2/examples/grpc/routeguide" | ||
pactlog "github.com/pact-foundation/pact-go/v2/log" | ||
"github.com/pact-foundation/pact-go/v2/message" | ||
"github.com/pact-foundation/pact-go/v2/models" | ||
"github.com/pact-foundation/pact-go/v2/provider" | ||
pactversion "github.com/pact-foundation/pact-go/v2/version" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPluginMessageProvider(t *testing.T) { | ||
var dir, _ = os.Getwd() | ||
var pactDir = fmt.Sprintf("%s/../pacts", dir) | ||
|
||
err := pactlog.SetLogLevel("TRACE") | ||
assert.NoError(t, err) | ||
|
||
pactversion.CheckVersion() | ||
|
||
verifier := provider.NewVerifier() | ||
|
||
functionMappings := message.Handlers{ | ||
"feature message": func([]models.ProviderState) (message.Body, message.Metadata, error) { | ||
fmt.Println("feature message handler") | ||
feature, _ := proto.Marshal(&routeguide.Feature{ | ||
Name: "fake feature", | ||
Location: &routeguide.Point{ | ||
Latitude: int32(1), | ||
Longitude: int32(1), | ||
}, | ||
}) | ||
return feature, message.Metadata{ | ||
"contentType": "application/protobuf;message=Feature", // <- This is required to ensure the correct type is matched | ||
}, nil | ||
}, | ||
} | ||
|
||
err = verifier.VerifyProvider(t, provider.VerifyRequest{ | ||
PactFiles: []string{ | ||
filepath.ToSlash(fmt.Sprintf("%s/protobufmessageconsumer-protobufmessageprovider.json", pactDir)), | ||
}, | ||
Provider: "protobufmessageprovider", | ||
MessageHandlers: functionMappings, | ||
}) | ||
|
||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters