Example #1
0
        public async Task MessageWithoutTextData()
        {
            var data = new MessagePublishedData
            {
                Message = new PubsubMessage {
                    Attributes = { { "key", "value" } }
                }
            };
            var cloudEvent = new CloudEvent
            {
                Type   = MessagePublishedData.MessagePublishedCloudEventType,
                Source = new Uri("//pubsub.googleapis.com", UriKind.RelativeOrAbsolute),
                Id     = Guid.NewGuid().ToString(),
                Time   = DateTimeOffset.UtcNow
            };

            var logger   = new MemoryLogger <HelloPubSub.Function>();
            var function = new HelloPubSub.Function(logger);
            await function.HandleAsync(cloudEvent, data, CancellationToken.None);

            var logEntry = Assert.Single(logger.ListLogEntries());

            Assert.Equal("Hello world", logEntry.Message);
            Assert.Equal(LogLevel.Information, logEntry.Level);
        }
Example #2
0
        public async Task FileNameIsLogged()
        {
            // Prepare the inputs
            var data = new StorageObjectData {
                Name = "new-file.txt"
            };
            var cloudEvent = new CloudEvent
            {
                Type   = StorageObjectData.FinalizedCloudEventType,
                Source = new Uri("//storage.googleapis.com", UriKind.RelativeOrAbsolute),
                Id     = "1234",
                Data   = data
            };
            var logger = new MemoryLogger <HelloGcs.Function>();

            // Execute the function
            var function = new HelloGcs.Function(logger);
            await function.HandleAsync(cloudEvent, data, CancellationToken.None);

            // Check the log results - just the entry starting with "File:".
            var logEntry = Assert.Single(logger.ListLogEntries(), entry => entry.Message.StartsWith("File:"));

            Assert.Equal("File: new-file.txt", logEntry.Message);
            Assert.Equal(LogLevel.Information, logEntry.Level);
        }
Example #3
0
        public async Task MessageWithTextData()
        {
            var data = new MessagePublishedData {
                Message = new PubsubMessage {
                    TextData = "PubSub user"
                }
            };
            var cloudEvent = new CloudEvent(
                MessagePublishedData.MessagePublishedCloudEventType,
                new Uri("//pubsub.googleapis.com", UriKind.RelativeOrAbsolute),
                Guid.NewGuid().ToString(),
                DateTime.UtcNow);

            // Our function doesn't actually use the CloudEvent data, because that's provided separately.
            // If we wanted to make the CloudEvent look as realistic as possible, we could use
            // CloudEventConverters.PopulateCloudEvent.

            var logger   = new MemoryLogger <HelloPubSub.Function>();
            var function = new HelloPubSub.Function(logger);
            await function.HandleAsync(cloudEvent, data, CancellationToken.None);

            var logEntry = Assert.Single(logger.ListLogEntries());

            Assert.Equal("Hello PubSub user", logEntry.Message);
            Assert.Equal(LogLevel.Information, logEntry.Level);
        }
Example #4
0
        public async Task LogEntryIsRecorded()
        {
            var logger   = new MemoryLogger <SimpleDependencyInjection.Function>();
            var function = new SimpleDependencyInjection.Function(logger);

            // Constructing the function does not create any log entries.
            Assert.Empty(logger.ListLogEntries());

            // Make a request to the function.
            var context = new DefaultHttpContext
            {
                Request = { Path = "/sample-path" }
            };
            await function.HandleAsync(context);

            var logs  = logger.ListLogEntries();
            var entry = Assert.Single(logs);

            Assert.Equal(LogLevel.Information, entry.Level);
            Assert.Equal("Function called with path /sample-path", entry.Message);
        }
        public async Task FileNameIsLogged()
        {
            // Prepare the inputs
            var cloudEvent = new CloudEvent(StorageObjectData.FinalizedCloudEventType, new Uri("//storage.googleapis.com"));
            var data       = new StorageObjectData {
                Name = "new-file.txt"
            };

            CloudEventConverters.PopulateCloudEvent(cloudEvent, data);
            var logger = new MemoryLogger <HelloGcs.Function>();

            // Execute the function
            var function = new HelloGcs.Function(logger);
            await function.HandleAsync(cloudEvent, data, CancellationToken.None);

            // Check the log results
            var logEntry = Assert.Single(logger.ListLogEntries());

            Assert.Equal("File new-file.txt uploaded", logEntry.Message);
            Assert.Equal(LogLevel.Information, logEntry.Level);
        }