mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-17 11:07:18 +02:00
controlplane: xds unit tests (#770)
* xds: use plain functions, add unit tests for control plane routes * xds: add test for grpc routes * xds: add test for pomerium http routes * xds: add test for policy routes * xds: use plain functions * xds: test get all routeable domains * xds: add build downstream tls context test * more tests * test for client cert * more tests
This commit is contained in:
parent
7b96d2de66
commit
dedf4b1428
8 changed files with 834 additions and 98 deletions
34
internal/testutil/testutil.go
Normal file
34
internal/testutil/testutil.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
// Package testutil contains helper functions for unit tests.
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
)
|
||||
|
||||
// AssertProtoJSONEqual asserts that a protobuf message matches the given JSON. The protoMsg can also be a slice
|
||||
// of protobuf messages.
|
||||
func AssertProtoJSONEqual(t *testing.T, expected string, protoMsg interface{}, msgAndArgs ...interface{}) bool {
|
||||
protoMsgVal := reflect.ValueOf(protoMsg)
|
||||
if protoMsgVal.Kind() == reflect.Slice {
|
||||
var protoMsgs []json.RawMessage
|
||||
for i := 0; i < protoMsgVal.Len(); i++ {
|
||||
protoMsgs = append(protoMsgs, toProtoJSON(protoMsgVal.Index(i).Interface()))
|
||||
}
|
||||
bs, _ := json.Marshal(protoMsgs)
|
||||
return assert.JSONEq(t, expected, string(bs), msgAndArgs...)
|
||||
}
|
||||
|
||||
return assert.JSONEq(t, expected, string(toProtoJSON(protoMsg)), msgAndArgs...)
|
||||
}
|
||||
|
||||
func toProtoJSON(protoMsg interface{}) json.RawMessage {
|
||||
v2 := proto.MessageV2(protoMsg)
|
||||
bs, _ := protojson.Marshal(v2)
|
||||
return bs
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue