Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Closes issue #138
- Loading branch information
Showing
3 changed files
with
126 additions
and
10 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
WordPressPCL.Tests.Selfhosted/Exception_Unexpected_Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using WordPressPCL.Models; | ||
|
||
namespace WordPressPCL.Tests.Selfhosted | ||
{ | ||
[TestClass] | ||
public class Exception_Unexpected_Tests | ||
{ | ||
private WordPressClient _badConnectionClient; | ||
|
||
[TestInitialize] | ||
public void Initialize() | ||
{ | ||
_badConnectionClient = new WordPressClient("https://microsoft.com"); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Tags_Get_UnexpectedException() | ||
{ | ||
await CheckForUnexpectedException(async () => await _badConnectionClient.Tags.Get()); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Tags_Post_UnexpectedException() | ||
{ | ||
await CheckForUnexpectedException(async () => await _badConnectionClient.Tags.Update(new Tag())); | ||
} | ||
|
||
[TestMethod] | ||
public async Task Tags_Delete_UnexpectedException() | ||
{ | ||
await CheckForUnexpectedException(async () => await _badConnectionClient.Tags.Delete(1), HttpStatusCode.MethodNotAllowed); | ||
} | ||
|
||
private async Task CheckForUnexpectedException(Func<Task> task, HttpStatusCode expectedStatus = HttpStatusCode.NotFound) | ||
{ | ||
bool exceptionCaught = false; | ||
|
||
try | ||
{ | ||
await task(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
exceptionCaught = true; | ||
|
||
if (!(ex is WPUnexpectedException typedException)) | ||
{ | ||
Assert.Fail($"Expected an exception of type WPUnexpectedException, but saw exception of type {ex.GetType()}"); | ||
} | ||
else | ||
{ | ||
Assert.AreEqual($"Server returned HTTP status {expectedStatus}", typedException.Message); | ||
Assert.IsNotNull(typedException.Response, "No HTTP response has been returned"); | ||
Assert.AreEqual(expectedStatus, typedException.Response.StatusCode); | ||
Assert.IsFalse(string.IsNullOrEmpty(typedException.ResponseBody), "Expected a response body but didn't see one"); | ||
} | ||
} | ||
|
||
Assert.IsTrue(exceptionCaught, "Exception was expected but none was seen"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters