Skip to content

Commit 751104f

Browse files
Finish prose tests
1 parent 6590f6e commit 751104f

File tree

13 files changed

+433
-60
lines changed

13 files changed

+433
-60
lines changed

internal/assert/assertbsoncore/assertions_bsoncore.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,41 @@
77
package assertbsoncore
88

99
import (
10+
"errors"
1011
"testing"
1112

1213
"go.mongodb.org/mongo-driver/v2/internal/assert"
14+
"go.mongodb.org/mongo-driver/v2/internal/handshake"
1315
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
1416
)
1517

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.
1622
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)
2124
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+
}
2332
}
2433

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+
}
2843
}
2944

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))
3247
}

internal/handshake/handshake.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,29 @@
66

77
package handshake
88

9+
import (
10+
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
11+
)
12+
913
// LegacyHello is the legacy version of the hello command.
1014
var LegacyHello = "isMaster"
1115

1216
// LegacyHelloLowercase is the lowercase, legacy version of the hello command.
1317
var LegacyHelloLowercase = "ismaster"
18+
19+
func ParseClientMetadata(msg []byte) ([]byte, error) {
20+
command := bsoncore.Document(msg)
21+
22+
// Lookup the "client" field in the command document.
23+
clientMetadataRaw, err := command.LookupErr("client")
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
clientMetadata, ok := clientMetadataRaw.DocumentOK()
29+
if !ok {
30+
return nil, err
31+
}
32+
33+
return clientMetadata, nil
34+
}

internal/integration/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ func TestClient(t *testing.T) {
457457
err := mt.Client.Ping(context.Background(), mtest.PrimaryRp)
458458
assert.Nil(mt, err, "Ping error: %v", err)
459459

460-
want := test.EncodeClientMetadata(mt, test.WithClientMentadataAppName("foo"))
460+
want := test.EncodeClientMetadata(mt, test.WithClientMetadataAppName("foo"))
461461
for i := 0; i < 2; i++ {
462462
message := mt.GetProxyCapture().TryNext()
463463
require.NotNil(mt, message, "expected handshake message, got nil")

0 commit comments

Comments
 (0)