示例#1
0
        public static void SaveRequestData(HttpContextBase context)
        {
            var url = context.Request.Url.ToString();

            var data = new BsonDocumentWrapper(new
            {
                //_id = ObjectId.GenerateNewId(),
                at = DateTime.UtcNow,
                url,
                method = context.Request.HttpMethod,
                headers = From(context.Request.Headers),
                clientIp = context.Request.UserHostAddress
            }).ToBsonDocument();
            if (context.Request.Form.Count > 0)
            {
                data.Add("form", From(context.Request.Form));
            }
            var cookies = From(context.Response.Cookies);
            if (cookies.Length > 0)
                data.Add("cookies", BsonArray.Create((IEnumerable)cookies));
            var referrer = context.Request.UrlReferrer;
            if (referrer != null)
                data.Add("referrer", referrer.ToString());

            try
            {
                DB.Database.GetCollection("requestsLog").Insert(data);
            }
            catch
            {
            }
        }
 public void TestAddNameValue()
 {
     var wrapper = new BsonDocumentWrapper(new BsonDocument());
     Assert.AreEqual(false, wrapper.IsMaterialized);
     wrapper.Add("x", 1);
     Assert.AreEqual(true, wrapper.IsMaterialized);
     Assert.AreEqual(1, wrapper.ElementCount);
     Assert.AreEqual("x", wrapper.GetElement(0).Name);
     Assert.AreEqual(new BsonInt32(1), wrapper[0]);
 }
 internal MongoQueryMessage(
     string collectionFullName,
     QueryFlags flags,
     int numberToSkip,
     int numberToReturn,
     object query,
     BsonDocumentWrapper fields
 )
     : this(collectionFullName, flags, numberToSkip, numberToReturn, query, fields, null)
 {
 }
 //  methods
 protected override IWireProtocol<BsonDocument> CreateProtocol(IConnectionHandle connection, WriteRequest request)
 {
     var insertRequest = (InsertRequest)request;
     var wrapper = new BsonDocumentWrapper(insertRequest.Document, insertRequest.Serializer);
     var documentSource = new BatchableSource<BsonDocument>(new[] { wrapper });
     return new InsertWireProtocol<BsonDocument>(
         DatabaseName,
         CollectionName,
         WriteConcern,
         BsonDocumentSerializer.Instance,
         documentSource,
         connection.Description.MaxBatchCount,
         connection.Description.MaxMessageSize,
         continueOnError: false);               
 }
 internal MongoQueryMessage(
     string collectionFullName,
     QueryFlags flags,
     int numberToSkip,
     int numberToReturn,
     object query,
     BsonDocumentWrapper fields,
     BsonBuffer buffer
 )
     : base(MessageOpcode.Query, buffer)
 {
     this.collectionFullName = collectionFullName;
     this.flags = flags;
     this.numberToSkip = numberToSkip;
     this.numberToReturn = numberToReturn;
     this.query = query;
     this.fields = fields;
 }
 public void TestTryGetValue()
 {
     var wrapper = new BsonDocumentWrapper(new BsonDocument("x", 1));
     Assert.AreEqual(false, wrapper.IsMaterialized);
     BsonValue value;
     var result = wrapper.TryGetValue("x", out value);
     Assert.AreEqual(true, wrapper.IsMaterialized);
     Assert.AreEqual(true, result);
     Assert.AreEqual(new BsonInt32(1), value);
 }
 public void TestTryGetElement()
 {
     var wrapper = new BsonDocumentWrapper(new BsonDocument("x", 1));
     Assert.AreEqual(false, wrapper.IsMaterialized);
     BsonElement element;
     var result = wrapper.TryGetElement("x", out element);
     Assert.AreEqual(true, wrapper.IsMaterialized);
     Assert.AreEqual(true, result);
     Assert.AreEqual("x", element.Name);
     Assert.AreEqual(new BsonInt32(1), element.Value);
 }
 public void TestToString()
 {
     var wrapper = new BsonDocumentWrapper(new BsonDocument("x", 1));
     Assert.AreEqual(false, wrapper.IsMaterialized);
     var expected = "{ 'x' : 1 }".Replace("'", "\"");
     var s = wrapper.ToString();
     Assert.AreEqual(false, wrapper.IsMaterialized); // ToString just serializes, doesn't materialize
     Assert.AreEqual(expected, s);
 }
 public void TestGetValueByName()
 {
     var wrapper = new BsonDocumentWrapper(new BsonDocument("x", 1));
     Assert.AreEqual(false, wrapper.IsMaterialized);
     var value = wrapper.GetValue("x");
     Assert.AreEqual(true, wrapper.IsMaterialized);
     Assert.AreEqual(new BsonInt32(1), value);
 }
 public void TestConstructorWithNullNominalTypeAndObjectAndIsUpdateDocument()
 {
     Assert.Throws<ArgumentNullException>(() => { var wrapper = new BsonDocumentWrapper(null, _c, false); });
 }
 public void TestConstructorWithNominalTypeAndObject()
 {
     var wrapper = new BsonDocumentWrapper(typeof(C), _c);
     var expected = "{ \"X\" : 1 }";
     Assert.AreEqual(expected, wrapper.ToJson());
 }
 public void TestConstructorWithObject()
 {
     var wrapper = new BsonDocumentWrapper(_c);
     var expected = "{ \"X\" : 1 }";
     Assert.AreEqual(expected, wrapper.ToJson());
 }
        public void TestRawValues()
        {
#pragma warning disable 618
            var wrapper = new BsonDocumentWrapper(new BsonDocument());
            Assert.AreEqual(false, wrapper.IsMaterialized);
            var values = wrapper.RawValues.ToArray();
            Assert.AreEqual(true, wrapper.IsMaterialized);
            Assert.AreEqual(0, values.Length);

            wrapper = new BsonDocumentWrapper(new BsonDocument("x", 1));
            Assert.AreEqual(false, wrapper.IsMaterialized);
            values = wrapper.RawValues.ToArray();
            Assert.AreEqual(true, wrapper.IsMaterialized);
            Assert.AreEqual(1, values.Length);
            Assert.AreEqual(1, values[0]);
#pragma warning restore
        }
        public void TestNames()
        {
            var wrapper = new BsonDocumentWrapper(new BsonDocument());
            Assert.AreEqual(false, wrapper.IsMaterialized);
            var names = wrapper.Names.ToArray();
            Assert.AreEqual(true, wrapper.IsMaterialized);
            Assert.AreEqual(0, names.Length);

            wrapper = new BsonDocumentWrapper(new BsonDocument("x", 1));
            Assert.AreEqual(false, wrapper.IsMaterialized);
            names = wrapper.Names.ToArray();
            Assert.AreEqual(true, wrapper.IsMaterialized);
            Assert.AreEqual(1, names.Length);
            Assert.AreEqual("x", names[0]);
        }
 public void TestMergeOverwrite()
 {
     var wrapper = new BsonDocumentWrapper(new BsonDocument("x", 1));
     Assert.AreEqual(false, wrapper.IsMaterialized);
     var result = wrapper.Merge(new BsonDocument("x", 2), true);
     Assert.AreEqual(true, wrapper.IsMaterialized);
     Assert.AreSame(wrapper, result);
     Assert.AreEqual(1, wrapper.ElementCount);
     Assert.AreEqual("x", wrapper.GetElement(0).Name);
     Assert.AreEqual(new BsonInt32(2), wrapper[0]);
 }
        public void TestIndexerByName()
        {
            var wrapper = new BsonDocumentWrapper(new BsonDocument("x", 1));
            Assert.AreEqual(false, wrapper.IsMaterialized);
            var value = wrapper["x"];
            Assert.AreEqual(true, wrapper.IsMaterialized);
            Assert.AreEqual(new BsonInt32(1), value);

            wrapper = new BsonDocumentWrapper(new BsonDocument());
            Assert.AreEqual(false, wrapper.IsMaterialized);
            wrapper["x"] = 1;
            Assert.AreEqual(true, wrapper.IsMaterialized);
            Assert.AreEqual(1, wrapper.ElementCount);
            Assert.AreEqual("x", wrapper.GetElement(0).Name);
            Assert.AreEqual(new BsonInt32(1), wrapper[0]);
        }
        public void TestIndexer()
        {
            var wrapper = new BsonDocumentWrapper(new BsonDocument("x", 1));
            Assert.AreEqual(false, wrapper.IsMaterialized);
            var value = wrapper[0];
            Assert.AreEqual(true, wrapper.IsMaterialized);
            Assert.AreEqual(new BsonInt32(1), value);

            wrapper = new BsonDocumentWrapper(new BsonDocument("x", 1));
            Assert.AreEqual(false, wrapper.IsMaterialized);
            wrapper[0] = 2;
            Assert.AreEqual(true, wrapper.IsMaterialized);
            Assert.AreEqual(1, wrapper.ElementCount);
            Assert.AreEqual(new BsonInt32(2), wrapper[0]);
        }
 public void TestGetValueByNameWithDefaultValue()
 {
     var wrapper = new BsonDocumentWrapper(new BsonDocument("x", 1));
     Assert.AreEqual(false, wrapper.IsMaterialized);
     var x = wrapper.GetValue("x", 2);
     var y = wrapper.GetValue("y", 2);
     Assert.AreEqual(true, wrapper.IsMaterialized);
     Assert.AreEqual(new BsonInt32(1), x);
     Assert.AreEqual(new BsonInt32(2), y);
 }
 public void TestGetEnumerator()
 {
     var wrapper = new BsonDocumentWrapper(new BsonDocument("x", 1));
     Assert.AreEqual(false, wrapper.IsMaterialized);
     var enumerator = wrapper.GetEnumerator();
     Assert.AreEqual(true, wrapper.IsMaterialized);
     Assert.IsTrue(enumerator.MoveNext());
     Assert.AreEqual("x", enumerator.Current.Name);
     Assert.AreEqual(new BsonInt32(1), enumerator.Current.Value);
     Assert.IsFalse(enumerator.MoveNext());
 }
        public void TestValues()
        {
            var wrapper = new BsonDocumentWrapper(new BsonDocument());
            Assert.AreEqual(false, wrapper.IsMaterialized);
            var values = wrapper.Values.ToArray();
            Assert.AreEqual(true, wrapper.IsMaterialized);
            Assert.AreEqual(0, values.Length);

            wrapper = new BsonDocumentWrapper(new BsonDocument("x", 1));
            Assert.AreEqual(false, wrapper.IsMaterialized);
            values = wrapper.Values.ToArray();
            Assert.AreEqual(true, wrapper.IsMaterialized);
            Assert.AreEqual(1, values.Length);
            Assert.AreEqual(new BsonInt32(1), values[0]);
        }
        public void TestAddNameValueFactoryWithCondition()
        {
            var wrapper = new BsonDocumentWrapper(new BsonDocument());
            Assert.AreEqual(false, wrapper.IsMaterialized);
            wrapper.Add("x", () => 1, false);
            Assert.AreEqual(true, wrapper.IsMaterialized);
            Assert.AreEqual(0, wrapper.ElementCount);

            wrapper = new BsonDocumentWrapper(new BsonDocument());
            Assert.AreEqual(false, wrapper.IsMaterialized);
            wrapper.Add("x", () => 1, true);
            Assert.AreEqual(true, wrapper.IsMaterialized);
            Assert.AreEqual(1, wrapper.ElementCount);
            Assert.AreEqual("x", wrapper.GetElement(0).Name);
            Assert.AreEqual(new BsonInt32(1), wrapper[0]);
        }
 public void TestRemoveElement()
 {
     var wrapper = new BsonDocumentWrapper(new BsonDocument { { "x", 1 }, { "y", 2 } });
     Assert.AreEqual(false, wrapper.IsMaterialized);
     wrapper.RemoveElement(new BsonElement("y", 2));
     Assert.AreEqual(true, wrapper.IsMaterialized);
     Assert.AreEqual(1, wrapper.ElementCount);
     Assert.AreEqual("x", wrapper.GetElement(0).Name);
     Assert.AreEqual(new BsonInt32(1), wrapper[0]);
 }
 public void TestConstructorWithNullObject()
 {
     var wrapper = new BsonDocumentWrapper(null);
     var expected = "null";
     Assert.AreEqual(expected, wrapper.ToJson());
 }
        // private methods
        private CommandDocument CreateWriteCommand(BatchSerializer batchSerializer, Batch<WriteRequest> batch)
        {
            var wrappedActualType = batch.GetType();
            var batchWrapper = new BsonDocumentWrapper(wrappedActualType, batch, batchSerializer, null, false);

            var writeConcern = _args.WriteConcern.ToBsonDocument();
            if (writeConcern.ElementCount == 0)
            {
                writeConcern = null; // omit field if writeConcern is { }
            }

            return new CommandDocument
            {
                { CommandName, _args.CollectionName },
                { "writeConcern", writeConcern, writeConcern != null },
                { "ordered", _args.IsOrdered },
                { RequestsElementName, new BsonArray { batchWrapper } }
            };
        }
 public void TestConstructorWithNominalTypeAndNullObjectAndIsUpdateDocument()
 {
     var wrapper = new BsonDocumentWrapper(typeof(C), null, false);
     var expected = "null";
     Assert.AreEqual(expected, wrapper.ToJson());
 }
 public void TestToHashtable()
 {
     var wrapper = new BsonDocumentWrapper(new BsonDocument { { "x", 1 }, { "y", 2 } });
     Assert.AreEqual(false, wrapper.IsMaterialized);
     var hashtable = wrapper.ToHashtable();
     Assert.AreEqual(true, wrapper.IsMaterialized);
     Assert.AreEqual(2, hashtable.Count);
     Assert.AreEqual(1, hashtable["x"]);
     Assert.AreEqual(2, hashtable["y"]);
 }
 public void TestSetElementByName()
 {
     var wrapper = new BsonDocumentWrapper(new BsonDocument("x", 1));
     Assert.AreEqual(false, wrapper.IsMaterialized);
     wrapper.SetElement(new BsonElement("x", 2));
     Assert.AreEqual(true, wrapper.IsMaterialized);
     Assert.AreEqual(1, wrapper.ElementCount);
     Assert.AreEqual("x", wrapper.GetElement(0).Name);
     Assert.AreEqual(new BsonInt32(2), wrapper[0]);
 }
 public void TestGetHashCode()
 {
     var wrapper1 = new BsonDocumentWrapper(new BsonDocument("x", 1));
     var wrapper2 = new BsonDocumentWrapper(new BsonDocument("x", 1));
     Assert.AreEqual(false, wrapper1.IsMaterialized);
     Assert.AreEqual(false, wrapper2.IsMaterialized);
     var hashCode1 = wrapper1.GetHashCode();
     var hashCode2 = wrapper2.GetHashCode();
     Assert.AreEqual(true, wrapper1.IsMaterialized);
     Assert.AreEqual(true, wrapper2.IsMaterialized);
     Assert.AreEqual(hashCode1, hashCode2);
 }
 public TestClass(
     BsonDocumentWrapper value
 )
 {
     this.B = value;
     this.V = value;
 }
 public void TestToDictionary()
 {
     var wrapper = new BsonDocumentWrapper(new BsonDocument { { "x", 1 }, { "y", 2 } });
     Assert.AreEqual(false, wrapper.IsMaterialized);
     var dictionary = wrapper.ToDictionary();
     Assert.AreEqual(true, wrapper.IsMaterialized);
     Assert.AreEqual(2, dictionary.Count);
     Assert.AreEqual(1, dictionary["x"]);
     Assert.AreEqual(2, dictionary["y"]);
 }