Skip to content

Commit

Permalink
Added AllowCORS parameter... (#93)
Browse files Browse the repository at this point in the history
Added AllowCORS parameter to HttpImposter and HttpsImposters. Additionally added tests for the parameter in respective test files.
  • Loading branch information
tgmike authored Jun 29, 2022
1 parent 3baadf3 commit bf854a2
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 11 deletions.
24 changes: 24 additions & 0 deletions MbDotNet.Tests/Models/Imposters/HttpImposterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions MbDotNet.Tests/Models/Imposters/HttpsImposterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions MbDotNet/IClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ public interface IClient
/// verification.
/// </param>
/// <param name="defaultResponse">The default response to send if no predicate matches</param>
/// <param name="allowCORS">Will allow all CORS preflight requests if set to true</param>
/// <returns>The newly created imposter</returns>
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);

/// <summary>
/// Creates a new imposter on the specified port with the HTTPS protocol. The Submit method
Expand All @@ -54,10 +55,10 @@ public interface IClient
/// verification.
/// </param>
/// <param name="defaultResponse">The default response to send if no predicate matches</param>
/// <param name="allowCORS">Will allow all CORS preflight requests if set to true</param>
/// <returns>The newly created imposter</returns>
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);
/// <summary>
/// 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
Expand Down
9 changes: 8 additions & 1 deletion MbDotNet/Models/Imposters/HttpImposter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
/// <summary>
/// Enables CORS requests when set to true, false by default
/// </summary>
[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<HttpStub>();
DefaultResponse = defaultResponse;
AllowCORS = allowCORS;
}

public HttpStub AddStub()
Expand Down
13 changes: 10 additions & 3 deletions MbDotNet/Models/Imposters/HttpsImposter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
/// <summary>
/// Enables CORS requests when set to true, false by default
/// </summary>
[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<HttpStub>();
DefaultResponse = defaultResponse;
AllowCORS = allowCORS;
}

public HttpStub AddStub()
Expand Down
10 changes: 6 additions & 4 deletions MbDotNet/MountebankClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ internal MountebankClient(IRequestProxy requestProxy)
/// verification.
/// </param>
/// <param name="defaultResponse">The default response to send if no predicate matches</param>
/// <param name="allowCORS">Will allow all CORS preflight requests if set to true</param>
/// <returns>The newly created imposter</returns>
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);
}

/// <summary>
Expand All @@ -66,10 +67,11 @@ public HttpImposter CreateHttpImposter(int? port = null, string name = null, boo
/// verification.
/// </param>
/// <param name="defaultResponse">The default response to send if no predicate matches</param>
/// <param name="allowCORS">Will allow all CORS preflight requests if set to true</param>
/// <returns>The newly created imposter</returns>
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))
{
Expand All @@ -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)
Expand Down

0 comments on commit bf854a2

Please sign in to comment.