Skip to content
This repository has been archived by the owner on Apr 30, 2022. It is now read-only.

Commit

Permalink
Merge pull request #881 from Magenic/bug/WorkWithMissingContentType
Browse files Browse the repository at this point in the history
Handle content with no header
  • Loading branch information
TroyWalshProf authored Dec 21, 2021
2 parents 9c0f14d + 36934ec commit 9336ccf
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Framework/AppiumUnitTests/AppiumConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ namespace AppiumUnitTests
[TestClass]
public class AppiumConfigTests
{
/// <summary>
/// Setup config for test class
/// </summary>
/// <param name="testContext">The test context</param>
[ClassInitialize]
public static void SetupTests(TestContext testContext)
{
Config.UpdateWithVSTestContext(testContext);
}

/// <summary>
/// Test for getting Mobile Device OS
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions Framework/BaseWebServiceTest/EventFiringWebServiceDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ private void BuildContentMessage(StringBuilder message, HttpContent content)
return;
}

string mediaType = content.Headers.ContentType.MediaType;
string mediaType = content.Headers?.ContentType?.MediaType;

if (string.IsNullOrEmpty(mediaType))
{
message.AppendLine(" **Content media type is null or empty**");
message.AppendLine(" **Writing content with a null or empty media type to the log is not supported**");
}
else
{
Expand All @@ -231,7 +231,7 @@ private void BuildContentMessage(StringBuilder message, HttpContent content)
}
else
{
message.AppendLine(" **Writting this kind of content to the log is not supported**");
message.AppendLine($" **Writing '{mediaType}' content to the log is not supported**");
}
}
}
Expand Down
17 changes: 16 additions & 1 deletion Framework/SeleniumUnitTests/SeleniumConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,22 @@ public void GetProxyAddress()
[TestCategory(TestCategories.Selenium)]
public void OpenEdgeBrowser()
{
var driver = WebDriverFactory.GetBrowserWithDefaultConfiguration(BrowserType.Edge);
IWebDriver driver;

try
{
driver = WebDriverFactory.GetBrowserWithDefaultConfiguration(BrowserType.Edge);
}
catch
{
// May need to run headless on some systems
var headlessEdgeOptions = WebDriverFactory.GetDefaultEdgeOptions();
headlessEdgeOptions.AddArgument("--no-sandbox");
headlessEdgeOptions.AddArguments("--headless");

driver = WebDriverFactory.GetEdgeDriver(SeleniumConfig.GetCommandTimeout(), headlessEdgeOptions);
}

driver.Navigate().GoToUrl(Config.GetValueForSection(ConfigSection.SeleniumMaqs, "WebSiteBase"));
driver?.KillDriver();
}
Expand Down
76 changes: 76 additions & 0 deletions Framework/WebServiceUnitTests/EventFiringWebServiceDriverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,82 @@ public void SendWithResponse()
Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
}

/// <summary>
/// Verify that we can handle content with no header
/// </summary>
[TestMethod]
[TestCategory(TestCategories.WebService)]
[DoNotParallelize]
public void HandleContentWithNoHeader()
{
// Get the number of logging errors
int errors = ErrorEvents;

// Get message and headers
var message = GetValidRequestMessage();
var headers = message.Content.Headers.GetEnumerator();

// Remove all headers
while (headers.MoveNext())
{
message.Content.Headers.Remove(headers.Current.Key);
}

message.Content.Headers.Add("test", string.Empty);

// Make sure we can get the expected response
var result = SendWithResponse(message, MediaType.AppJson, false);
Assert.AreEqual(HttpStatusCode.UnsupportedMediaType, result.StatusCode);

// Make sure we didn't log an error event
Assert.AreEqual(errors, ErrorEvents);
}

/// <summary>
/// Verify that we can handle null content
/// </summary>
[TestMethod]
[TestCategory(TestCategories.WebService)]
[DoNotParallelize]
public void HandleNullContent()
{
// Get the number of logging errors
int errors = ErrorEvents;

var message = GetValidRequestMessage();
message.Method = HttpMethod.Head;
message.Content = null;

// Make sure we can get the expected response
var result = SendWithResponse(message, MediaType.AppJson, false);
Assert.AreEqual(HttpStatusCode.MethodNotAllowed, result.StatusCode);

// Make sure we didn't log an error event
Assert.AreEqual(errors, ErrorEvents);
}

/// <summary>
/// Verify that we can handle non standard content
/// </summary>
[TestMethod]
[TestCategory(TestCategories.WebService)]
[DoNotParallelize]
public void HandleNonStandardContent()
{
// Get the number of logging errors
int errors = ErrorEvents;

var message = GetValidRequestMessage();
message.Content.Headers.ContentType.MediaType = "application/xlsx";

// Make sure we can get the expected response
var result = SendWithResponse(message, MediaType.AppJson, false);
Assert.AreEqual(HttpStatusCode.UnsupportedMediaType, result.StatusCode);

// Make sure we didn't log an error event
Assert.AreEqual(errors, ErrorEvents);
}

/// <summary>
/// Verify that we can send with expected
/// </summary>
Expand Down

0 comments on commit 9336ccf

Please sign in to comment.