/* * Copyright (c) 2024 ETH Zürich, IT Services * * 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 System.Threading; using CefSharp; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using SafeExamBrowser.Browser.Contracts.Filters; using SafeExamBrowser.Configuration.Contracts; using SafeExamBrowser.Configuration.Contracts.Cryptography; using SafeExamBrowser.I18n.Contracts; using SafeExamBrowser.Logging.Contracts; using SafeExamBrowser.Settings; using SafeExamBrowser.Settings.Browser; 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 Mock filter; private Mock keyGenerator; private Mock logger; private BrowserSettings settings; private WindowSettings windowSettings; private Mock text; private TestableResourceHandler sut; [TestInitialize] public void Initialize() { appConfig = new AppConfig(); filter = new Mock(); keyGenerator = new Mock(); logger = new Mock(); settings = new BrowserSettings(); windowSettings = new WindowSettings(); text = new Mock(); sut = new TestableResourceHandler(appConfig, filter.Object, keyGenerator.Object, logger.Object, SessionMode.Server, settings, windowSettings, text.Object); } [TestMethod] public void MustAppendCustomHeadersForSameDomain() { var browser = new Mock(); var headers = default(NameValueCollection); var request = new Mock(); browser.SetupGet(b => b.Address).Returns("http://www.host.org"); keyGenerator.Setup(g => g.CalculateBrowserExamKeyHash(It.IsAny(), It.IsAny(), It.IsAny())).Returns(new Random().Next().ToString()); keyGenerator.Setup(g => g.CalculateConfigurationKeyHash(It.IsAny(), It.IsAny())).Returns(new Random().Next().ToString()); 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.SendBrowserExamKey = true; var result = sut.OnBeforeResourceLoad(browser.Object, Mock.Of(), Mock.Of