-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
218 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
//go:build consumer | ||
// +build consumer | ||
|
||
package avro | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"testing" | ||
|
||
"github.com/pact-foundation/pact-go/v2/consumer" | ||
|
||
"path/filepath" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var dir, _ = os.Getwd() | ||
|
||
func TestAvroHTTP(t *testing.T) { | ||
mockProvider, err := consumer.NewV4Pact(consumer.MockHTTPProviderConfig{ | ||
Consumer: "AvroConsumer", | ||
Provider: "AvroProvider", | ||
PactDir: filepath.ToSlash(fmt.Sprintf("%s/../pacts", dir)), | ||
}) | ||
assert.NoError(t, err) | ||
|
||
dir, _ := os.Getwd() | ||
path := fmt.Sprintf("%s/user.avsc", dir) | ||
|
||
avroResponse := `{ | ||
"pact:avro": "` + path + `", | ||
"pact:record-name": "User", | ||
"pact:content-type": "avro/binary", | ||
"id": "matching(number, 1)", | ||
"username": "notEmpty('matt')" | ||
}` | ||
|
||
// Set up our expected interactions. | ||
err = mockProvider. | ||
AddInteraction(). | ||
UponReceiving("A request to do get some Avro stuff"). | ||
UsingPlugin(consumer.PluginConfig{ | ||
Plugin: "avro", | ||
Version: "0.0.2", | ||
}). | ||
WithRequest("GET", "/avro"). | ||
WillRespondWith(200, func(res *consumer.V4InteractionWithPluginResponseBuilder) { | ||
res.PluginContents("avro/binary", avroResponse) | ||
}). | ||
ExecuteTest(t, func(msc consumer.MockServerConfig) error { | ||
resp, err := callServiceHTTP(msc) | ||
|
||
assert.Equal(t, int64(1), resp.ID) | ||
assert.Equal(t, "matt", resp.Username) // ??????! | ||
|
||
return err | ||
}) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func callServiceHTTP(msc consumer.MockServerConfig) (*User, error) { | ||
client := &http.Client{} | ||
req := &http.Request{ | ||
Method: "GET", | ||
URL: &url.URL{ | ||
Host: fmt.Sprintf("%s:%d", msc.Host, msc.Port), | ||
Scheme: "http", | ||
Path: "/avro", | ||
}, | ||
Header: make(http.Header), | ||
} | ||
|
||
req.Header.Set("Content-Type", "avro/binary;record=User") | ||
|
||
res, err := client.Do(req) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bytes, err := ioutil.ReadAll(res.Body) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
codec := getCodec() | ||
native, _, err := codec.NativeFromBinary(bytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
user := &User{ | ||
ID: native.(map[string]interface{})["id"].(int64), | ||
Username: native.(map[string]interface{})["username"].(string), | ||
} | ||
|
||
return user, 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,71 @@ | ||
//go:build provider | ||
// +build provider | ||
|
||
package avro | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/pact-foundation/pact-go/v2/provider" | ||
"github.com/pact-foundation/pact-go/v2/utils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var dir, _ = os.Getwd() | ||
var pactDir = fmt.Sprintf("%s/../pacts", dir) | ||
|
||
func TestAvroHTTPProvider(t *testing.T) { | ||
httpPort, _ := utils.GetFreePort() | ||
|
||
// Start provider API in the background | ||
go startHTTPProvider(httpPort) | ||
|
||
verifier := provider.NewVerifier() | ||
|
||
// Verify the Provider with local Pact Files | ||
err := verifier.VerifyProvider(t, provider.VerifyRequest{ | ||
ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", httpPort), | ||
Provider: "AvroProvider", | ||
PactFiles: []string{ | ||
filepath.ToSlash(fmt.Sprintf("%s/AvroConsumer-AvroProvider.json", pactDir)), | ||
}, | ||
}) | ||
|
||
assert.NoError(t, err) | ||
} | ||
|
||
func startHTTPProvider(port int) { | ||
mux := http.NewServeMux() | ||
|
||
mux.HandleFunc("/avro", func(w http.ResponseWriter, req *http.Request) { | ||
w.Header().Add("Content-Type", "avro/binary;record=User") | ||
|
||
user := &User{ | ||
ID: 1, | ||
Username: "matt", | ||
// Username: "sally", // matching rules not supported? | ||
} | ||
|
||
codec := getCodec() | ||
binary, err := codec.BinaryFromNative(nil, map[string]interface{}{ | ||
"id": user.ID, | ||
"username": user.Username, | ||
}) | ||
if err != nil { | ||
log.Println("ERROR: ", err) | ||
w.WriteHeader(500) | ||
} else { | ||
fmt.Fprintf(w, string(binary)) | ||
w.WriteHeader(200) | ||
} | ||
}) | ||
|
||
log.Printf("started HTTP server on port: %d\n", port) | ||
log.Fatal(http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", port), mux)) | ||
} |
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,21 @@ | ||
package avro | ||
|
||
import ( | ||
"io/ioutil" | ||
|
||
"github.com/linkedin/goavro/v2" | ||
) | ||
|
||
func getCodec() *goavro.Codec { | ||
schema, err := ioutil.ReadFile("user.avsc") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
codec, err := goavro.NewCodec(string(schema)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return codec | ||
} |
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,9 @@ | ||
{ | ||
"type": "record", | ||
"name": "User", | ||
"namespace": "io.pact", | ||
"fields" : [ | ||
{"name": "id", "type": "long"}, | ||
{"name": "username", "type": "string"} | ||
] | ||
} |
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,6 @@ | ||
package avro | ||
|
||
type User struct { | ||
ID int64 `json:"id"` | ||
Username string `json:"username"` | ||
} |
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