From ad24146d9fa26629f7bbffbee0a4ab4aefb9038f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20B=C3=BCchel?= Date: Mon, 6 Apr 2020 14:13:13 +0200 Subject: [PATCH] SEBWIN-380: Implemented unit tests for browser resource handler. --- .../Handlers/ResourceHandlerTests.cs | 179 ++++++++++++++++++ .../SafeExamBrowser.Browser.UnitTests.csproj | 1 + .../Handlers/ResourceHandler.cs | 10 +- 3 files changed, 185 insertions(+), 5 deletions(-) create mode 100644 SafeExamBrowser.Browser.UnitTests/Handlers/ResourceHandlerTests.cs diff --git a/SafeExamBrowser.Browser.UnitTests/Handlers/ResourceHandlerTests.cs b/SafeExamBrowser.Browser.UnitTests/Handlers/ResourceHandlerTests.cs new file mode 100644 index 00000000..abf1875e --- /dev/null +++ b/SafeExamBrowser.Browser.UnitTests/Handlers/ResourceHandlerTests.cs @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2020 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System; +using System.Collections.Specialized; +using System.Net.Mime; +using CefSharp; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using SafeExamBrowser.Browser.Contracts.Filters; +using SafeExamBrowser.Configuration.Contracts; +using SafeExamBrowser.I18n.Contracts; +using SafeExamBrowser.Logging.Contracts; +using SafeExamBrowser.Settings.Browser.Filter; +using BrowserSettings = SafeExamBrowser.Settings.Browser.BrowserSettings; +using Request = SafeExamBrowser.Browser.Contracts.Filters.Request; +using ResourceHandler = SafeExamBrowser.Browser.Handlers.ResourceHandler; + +namespace SafeExamBrowser.Browser.UnitTests.Handlers +{ + [TestClass] + public class ResourceHandlerTests + { + private AppConfig appConfig; + private BrowserSettings settings; + private Mock filter; + private Mock logger; + private Mock text; + private TestableResourceHandler sut; + + [TestInitialize] + public void Initialize() + { + appConfig = new AppConfig(); + settings = new BrowserSettings(); + filter = new Mock(); + logger = new Mock(); + text = new Mock(); + + sut = new TestableResourceHandler(appConfig, settings, filter.Object, logger.Object, text.Object); + } + + [TestMethod] + public void MustAppendCustomHeaders() + { + var headers = default(NameValueCollection); + var request = new Mock(); + + request.SetupGet(r => r.Headers).Returns(new NameValueCollection()); + request.SetupGet(r => r.Url).Returns("http://www.host.org"); + request.SetupSet(r => r.Headers = It.IsAny()).Callback((h) => headers = h); + settings.SendConfigurationKey = true; + settings.SendExamKey = true; + + var result = sut.OnBeforeResourceLoad(Mock.Of(), Mock.Of(), Mock.Of