| 
7 | 7 | package assertbsoncore  | 
8 | 8 | 
 
  | 
9 | 9 | import (  | 
 | 10 | +	"errors"  | 
10 | 11 | 	"testing"  | 
11 | 12 | 
 
  | 
12 | 13 | 	"go.mongodb.org/mongo-driver/v2/internal/assert"  | 
 | 14 | +	"go.mongodb.org/mongo-driver/v2/internal/handshake"  | 
13 | 15 | 	"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"  | 
14 | 16 | )  | 
15 | 17 | 
 
  | 
 | 18 | +// HandshakeClientMetadata compares the client metadata in two wire messages. It  | 
 | 19 | +// extracts the client metadata document from each wire message and compares  | 
 | 20 | +// them. If the document is not found, it assumes the wire message is just the  | 
 | 21 | +// value of the client metadata document itself.  | 
16 | 22 | func HandshakeClientMetadata(t testing.TB, expectedWM, actualWM []byte) bool {  | 
17 |  | -	command := bsoncore.Document(actualWM)  | 
18 |  | - | 
19 |  | -	// Lookup the "client" field in the command document.  | 
20 |  | -	clientVal, err := command.LookupErr("client")  | 
 | 23 | +	gotCommand, err := handshake.ParseClientMetadata(actualWM)  | 
21 | 24 | 	if err != nil {  | 
22 |  | -		return assert.Fail(t, "expected command to contain the 'client' field")  | 
 | 25 | +		if errors.Is(err, bsoncore.ErrElementNotFound) {  | 
 | 26 | +			// If the element is not found, the actual wire message may just be the  | 
 | 27 | +			// client metadata document itself.  | 
 | 28 | +			gotCommand = bsoncore.Document(actualWM)  | 
 | 29 | +		} else {  | 
 | 30 | +			return assert.Fail(t, "error parsing actual wire message: %v", err)  | 
 | 31 | +		}  | 
23 | 32 | 	}  | 
24 | 33 | 
 
  | 
25 |  | -	GotCommand, ok := clientVal.DocumentOK()  | 
26 |  | -	if !ok {  | 
27 |  | -		return assert.Fail(t, "expected client field to be a document, got %s", clientVal.Type)  | 
 | 34 | +	wantCommand, err := handshake.ParseClientMetadata(expectedWM)  | 
 | 35 | +	if err != nil {  | 
 | 36 | +		// If the element is not found, the expected wire message may just be the  | 
 | 37 | +		// client metadata document itself.  | 
 | 38 | +		if errors.Is(err, bsoncore.ErrElementNotFound) {  | 
 | 39 | +			wantCommand = bsoncore.Document(expectedWM)  | 
 | 40 | +		} else {  | 
 | 41 | +			return assert.Fail(t, "error parsing expected wire message: %v", err)  | 
 | 42 | +		}  | 
28 | 43 | 	}  | 
29 | 44 | 
 
  | 
30 |  | -	wantCommand := bsoncore.Document(expectedWM)  | 
31 |  | -	return assert.Equal(t, wantCommand, GotCommand, "want: %v, got: %v", wantCommand, GotCommand)  | 
 | 45 | +	return assert.Equal(t, wantCommand, gotCommand,  | 
 | 46 | +		"expected: %v, got: %v", bsoncore.Document(wantCommand), bsoncore.Document(gotCommand))  | 
32 | 47 | }  | 
0 commit comments