1// Copyright (C) MongoDB, Inc. 2017-present.
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may
4// not use this file except in compliance with the License. You may obtain
5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
7package wiremessage
8
9import (
10	"testing"
11
12	"github.com/google/go-cmp/cmp"
13	"go.mongodb.org/mongo-driver/bson"
14)
15
16func TestReply(t *testing.T) {
17	t.Run("UnmarshalWireMessage", func(t *testing.T) {
18		testCases := []struct {
19			name string
20			b    []byte
21			r    Reply
22			err  error
23		}{
24			{
25				"success",
26				[]byte{
27					0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
28					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
29					0x00, 0x00, 0x00, 0x00,
30					0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
31					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32					0x0A, 0x00, 0x00, 0x00, 0x0A, 'f', 'o', 'o', 0x00, 0x00,
33					0x0A, 0x00, 0x00, 0x00, 0x0A, 'f', 'o', 'o', 0x00, 0x00,
34				},
35				Reply{
36					MsgHeader: Header{
37						MessageLength: 56,
38					},
39					CursorID: 256,
40					Documents: []bson.Raw{
41						{0x0A, 0x00, 0x00, 0x00, 0x0A, 'f', 'o', 'o', 0x00, 0x00},
42						{0x0A, 0x00, 0x00, 0x00, 0x0A, 'f', 'o', 'o', 0x00, 0x00},
43					},
44				},
45				nil,
46			},
47		}
48
49		for _, tc := range testCases {
50			t.Run(tc.name, func(t *testing.T) {
51				var r Reply
52				err := r.UnmarshalWireMessage(tc.b)
53				if err != tc.err {
54					t.Errorf("Errors do not match. got %v; want %v", err, tc.err)
55				}
56				if diff := cmp.Diff(r, tc.r); diff != "" {
57					t.Errorf("Reply's differ: (-got +want)\n%s", diff)
58				}
59			})
60		}
61	})
62}
63