From bf854a2f7a44c8fe5df9aa16b6893a923a5e188e Mon Sep 17 00:00:00 2001 From: Michael Velasquez Date: Wed, 29 Jun 2022 09:32:56 -0700 Subject: [PATCH] Added AllowCORS parameter... (#93) Added AllowCORS parameter to HttpImposter and HttpsImposters. Additionally added tests for the parameter in respective test files. --- .../Models/Imposters/HttpImposterTests.cs | 24 +++++++++++++++++++ .../Models/Imposters/HttpsImposterTests.cs | 24 +++++++++++++++++++ MbDotNet/IClient.cs | 7 +++--- MbDotNet/Models/Imposters/HttpImposter.cs | 9 ++++++- MbDotNet/Models/Imposters/HttpsImposter.cs | 13 +++++++--- MbDotNet/MountebankClient.cs | 10 ++++---- 6 files changed, 76 insertions(+), 11 deletions(-) diff --git a/MbDotNet.Tests/Models/Imposters/HttpImposterTests.cs b/MbDotNet.Tests/Models/Imposters/HttpImposterTests.cs index e1f1df2..128fa3a 100644 --- a/MbDotNet.Tests/Models/Imposters/HttpImposterTests.cs +++ b/MbDotNet.Tests/Models/Imposters/HttpImposterTests.cs @@ -56,6 +56,30 @@ public void Constructor_InitializesDefaultResponse() Assert.IsNotNull(imposter.DefaultResponse); } + [TestMethod] + public void Constructor_InitialAllowCORS() + { + const bool allowCORS = false; + var imposter = new HttpImposter(null, null); + Assert.AreEqual(allowCORS, imposter.AllowCORS); + } + + [TestMethod] + public void Constructor_AllowCORSTrue() + { + const bool allowCORS = true; + var imposter = new HttpImposter(null, null, false, null, true); + Assert.AreEqual(allowCORS, imposter.AllowCORS); + } + + [TestMethod] + public void Constructor_AllowCORSFalse() + { + const bool allowCORS = false; + var imposter = new HttpImposter(null, null, false, null, false); + Assert.AreEqual(allowCORS, imposter.AllowCORS); + } + #endregion #region Stub Tests diff --git a/MbDotNet.Tests/Models/Imposters/HttpsImposterTests.cs b/MbDotNet.Tests/Models/Imposters/HttpsImposterTests.cs index 87fb695..342ca65 100644 --- a/MbDotNet.Tests/Models/Imposters/HttpsImposterTests.cs +++ b/MbDotNet.Tests/Models/Imposters/HttpsImposterTests.cs @@ -100,6 +100,30 @@ public void HttpsImposter_Constructor_SetsDefaultResponse() Assert.IsNotNull(imposter.DefaultResponse); } + [TestMethod] + public void Constructor_InitialAllowCORS() + { + const bool allowCORS = false; + var imposter = new HttpsImposter(null, null); + Assert.AreEqual(allowCORS, imposter.AllowCORS); + } + + [TestMethod] + public void Constructor_AllowCORSTrue() + { + const bool allowCORS = true; + var imposter = new HttpsImposter(null, null, false, null, true); + Assert.AreEqual(allowCORS, imposter.AllowCORS); + } + + [TestMethod] + public void Constructor_AllowCORSFalse() + { + const bool allowCORS = false; + var imposter = new HttpsImposter(null, null, false, null, false); + Assert.AreEqual(allowCORS, imposter.AllowCORS); + } + #endregion #region Stub Tests diff --git a/MbDotNet/IClient.cs b/MbDotNet/IClient.cs index 7ffad7a..21f3c40 100644 --- a/MbDotNet/IClient.cs +++ b/MbDotNet/IClient.cs @@ -32,8 +32,9 @@ public interface IClient /// verification. /// /// The default response to send if no predicate matches + /// Will allow all CORS preflight requests if set to true /// The newly created imposter - HttpImposter CreateHttpImposter(int? port = null, string name = null, bool recordRequests = false, HttpResponseFields defaultResponse = null); + HttpImposter CreateHttpImposter(int? port = null, string name = null, bool recordRequests = false, HttpResponseFields defaultResponse = null, bool allowCORS = false); /// /// Creates a new imposter on the specified port with the HTTPS protocol. The Submit method @@ -54,10 +55,10 @@ public interface IClient /// verification. /// /// The default response to send if no predicate matches + /// Will allow all CORS preflight requests if set to true /// The newly created imposter HttpsImposter CreateHttpsImposter(int? port = null, string name = null, string key = null, string cert = null, - bool mutualAuthRequired = false, bool recordRequests = false, HttpResponseFields defaultResponse = null); - + bool mutualAuthRequired = false, bool recordRequests = false, HttpResponseFields defaultResponse = null, bool allowCORS = false); /// /// Creates a new imposter on the specified port with the TCP protocol. The Submit method /// must be called on the client in order to submit the imposter to mountebank. If the port diff --git a/MbDotNet/Models/Imposters/HttpImposter.cs b/MbDotNet/Models/Imposters/HttpImposter.cs index 21c70c7..91aae96 100644 --- a/MbDotNet/Models/Imposters/HttpImposter.cs +++ b/MbDotNet/Models/Imposters/HttpImposter.cs @@ -16,11 +16,18 @@ public class HttpImposter : Imposter [JsonProperty("defaultResponse", NullValueHandling = NullValueHandling.Ignore)] public HttpResponseFields DefaultResponse { get; private set; } - public HttpImposter(int? port, string name, bool recordRequests = false, HttpResponseFields defaultResponse = null) + /// + /// Enables CORS requests when set to true, false by default + /// + [JsonProperty("allowCORS")] + public bool AllowCORS { get; private set; } + + public HttpImposter(int? port, string name, bool recordRequests = false, HttpResponseFields defaultResponse = null, bool allowCORS = false) : base(port, Enums.Protocol.Http, name, recordRequests) { Stubs = new List(); DefaultResponse = defaultResponse; + AllowCORS = allowCORS; } public HttpStub AddStub() diff --git a/MbDotNet/Models/Imposters/HttpsImposter.cs b/MbDotNet/Models/Imposters/HttpsImposter.cs index 1ddaf40..8f4c94a 100644 --- a/MbDotNet/Models/Imposters/HttpsImposter.cs +++ b/MbDotNet/Models/Imposters/HttpsImposter.cs @@ -25,19 +25,26 @@ public class HttpsImposter : Imposter [JsonProperty("defaultResponse", NullValueHandling = NullValueHandling.Ignore)] public HttpResponseFields DefaultResponse { get; private set; } - public HttpsImposter(int? port, string name, bool recordRequests = false, HttpResponseFields defaultResponse = null) - : this(port, name, null, null, false, recordRequests, defaultResponse) + /// + /// Enables CORS requests when set to true, false by default + /// + [JsonProperty("allowCORS")] + public bool AllowCORS { get; private set; } + + public HttpsImposter(int? port, string name, bool recordRequests = false, HttpResponseFields defaultResponse = null, bool allowCORS = false) + : this(port, name, null, null, false, recordRequests, defaultResponse, allowCORS) { } public HttpsImposter(int? port, string name, string key, string cert, bool mutualAuthRequired, - bool recordRequests = false, HttpResponseFields defaultResponse = null) : base(port, Enums.Protocol.Https, name, recordRequests) + bool recordRequests = false, HttpResponseFields defaultResponse = null, bool allowCORS = false) : base(port, Enums.Protocol.Https, name, recordRequests) { Cert = cert; Key = key; MutualAuthRequired = mutualAuthRequired; Stubs = new List(); DefaultResponse = defaultResponse; + AllowCORS = allowCORS; } public HttpStub AddStub() diff --git a/MbDotNet/MountebankClient.cs b/MbDotNet/MountebankClient.cs index 56547b4..a94069c 100644 --- a/MbDotNet/MountebankClient.cs +++ b/MbDotNet/MountebankClient.cs @@ -43,10 +43,11 @@ internal MountebankClient(IRequestProxy requestProxy) /// verification. /// /// The default response to send if no predicate matches + /// Will allow all CORS preflight requests if set to true /// The newly created imposter - public HttpImposter CreateHttpImposter(int? port = null, string name = null, bool recordRequests = false, HttpResponseFields defaultResponse = null) + public HttpImposter CreateHttpImposter(int? port = null, string name = null, bool recordRequests = false, HttpResponseFields defaultResponse = null, bool allowCORS = false) { - return new HttpImposter(port, name, recordRequests, defaultResponse); + return new HttpImposter(port, name, recordRequests, defaultResponse, allowCORS); } /// @@ -66,10 +67,11 @@ public HttpImposter CreateHttpImposter(int? port = null, string name = null, boo /// verification. /// /// The default response to send if no predicate matches + /// Will allow all CORS preflight requests if set to true /// The newly created imposter public HttpsImposter CreateHttpsImposter(int? port = null, string name = null, string key = null, string cert = null, bool mutualAuthRequired = false, bool recordRequests = false, - HttpResponseFields defaultResponse = null) + HttpResponseFields defaultResponse = null, bool allowCORS = false) { if (key != null && !IsPEMFormatted(key)) { @@ -81,7 +83,7 @@ public HttpsImposter CreateHttpsImposter(int? port = null, string name = null, s throw new InvalidOperationException("Provided certificate must be PEM-formatted"); } - return new HttpsImposter(port, name, key, cert, mutualAuthRequired, recordRequests, defaultResponse); + return new HttpsImposter(port, name, key, cert, mutualAuthRequired, recordRequests, defaultResponse, allowCORS); } private bool IsPEMFormatted(string value)