-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
144 additions
and
15 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
using BugSplatDotNetStandard.Http; | ||
using Moq; | ||
using Moq.Protected; | ||
using Newtonsoft.Json.Linq; | ||
using NUnit.Framework; | ||
using static Tests.StackTraceFactory; | ||
|
||
|
@@ -18,22 +19,30 @@ namespace Tests | |
[TestFixture] | ||
public class CrashPostClientTest | ||
{ | ||
const string database = "fred"; | ||
const string application = "my-net-crasher"; | ||
const string version = "1.0"; | ||
|
||
FileInfo lockedFile; | ||
FileInfo minidumpFile; | ||
FileStream lockedFileWriter; | ||
|
||
string database; | ||
string application = "TestApp"; | ||
string version = "1.0.0"; | ||
string clientId; | ||
string clientSecret; | ||
|
||
[OneTimeSetUp] | ||
public void SetUp() | ||
{ | ||
DotNetEnv.Env.Load(); | ||
database = Environment.GetEnvironmentVariable("BUGSPLAT_DATABASE"); | ||
clientId = Environment.GetEnvironmentVariable("BUGSPLAT_CLIENT_ID"); | ||
clientSecret = Environment.GetEnvironmentVariable("BUGSPLAT_CLIENT_SECRET"); | ||
|
||
var bytesToWrite = Encoding.UTF8.GetBytes("This file is locked"); | ||
lockedFile = new FileInfo("lockedFile.txt"); | ||
lockedFileWriter = File.Open(lockedFile.FullName, FileMode.Create, FileAccess.Write, FileShare.None); | ||
lockedFileWriter.Write(bytesToWrite, 0, bytesToWrite.Length); | ||
lockedFileWriter.Flush(); | ||
|
||
minidumpFile = new FileInfo("minidump.dmp"); | ||
File.Create(minidumpFile.FullName).Close(); | ||
} | ||
|
@@ -61,7 +70,6 @@ public void CrashPostClient_Constructor_ShouldThrowIfS3ClientFactoryIsNullOrEmpt | |
[Test] | ||
public async Task CrashPostClient_PostException_ShouldReturn200() | ||
{ | ||
var expectedUri = $"https://{database}.bugsplat.com/api/getCrashUploadUrl?database={database}&appName={application}&appVersion={version}"; | ||
var stackTrace = CreateStackTrace(); | ||
var bugsplat = new BugSplat(database, application, version); | ||
var getCrashUrl = "https://fake.url.com"; | ||
|
@@ -86,7 +94,6 @@ public async Task CrashPostClient_PostException_ShouldReturn200() | |
[Test] | ||
public async Task CrashPostClient_PostMinidump_ShouldReturn200() | ||
{ | ||
var expectedUri = $"https://{database}.bugsplat.com/api/getCrashUploadUrl?database={database}&appName={application}&appVersion={version}"; | ||
var bugsplat = new BugSplat(database, application, version); | ||
var getCrashUrl = "https://fake.url.com"; | ||
var mockHttp = CreateMockHttpClientForExceptionPost(getCrashUrl); | ||
|
@@ -136,7 +143,6 @@ await sut.PostException( | |
[Test] | ||
public void CrashPostClient_PostCrashFile_ShouldNotThrowIfAttachmentLocked() | ||
{ | ||
var stackTrace = CreateStackTrace(); | ||
var bugsplat = new BugSplat(database, application, version); | ||
bugsplat.Attachments.Add(lockedFile); | ||
var getCrashUrl = "https://fake.url.com"; | ||
|
@@ -159,6 +165,43 @@ await sut.PostCrashFile( | |
}); | ||
} | ||
|
||
[Test] | ||
[Explicit] | ||
public async Task CrashPostClient_PostCrashFile_PostCrashAndMetadata() | ||
{ | ||
var stackTrace = CreateStackTrace(); | ||
var bugsplat = new BugSplat(database, application, version); | ||
bugsplat.Description = "Test description"; | ||
bugsplat.Email = "[email protected]"; | ||
bugsplat.Key = "Test key"; | ||
bugsplat.Notes = "Test notes"; | ||
bugsplat.User = "Test user"; | ||
var oauth2ApiClient = OAuth2ApiClient.Create(clientId, clientSecret) | ||
.Authenticate() | ||
.Result; | ||
var crashDetailsClient = CrashDetailsClient.Create(oauth2ApiClient); | ||
var sut = new CrashPostClient(HttpClientFactory.Default, S3ClientFactory.Default); | ||
|
||
var postResult = await sut.PostException( | ||
database, | ||
application, | ||
version, | ||
stackTrace, | ||
ExceptionPostOptions.Create(bugsplat) | ||
); | ||
var postResponseContent = JObject.Parse(postResult.Content.ReadAsStringAsync().Result); | ||
var id = postResponseContent["crashId"].Value<int>(); | ||
|
||
var crashDetails = await crashDetailsClient.GetCrashDetails(database, id); | ||
var crashDetailsContent = JObject.Parse(crashDetails.Content.ReadAsStringAsync().Result); | ||
Assert.AreEqual(HttpStatusCode.OK, postResult.StatusCode); | ||
Assert.AreEqual(bugsplat.Description, crashDetailsContent["description"].Value<string>()); | ||
Assert.AreEqual(bugsplat.Email, crashDetailsContent["email"].Value<string>()); | ||
Assert.AreEqual(bugsplat.Key, crashDetailsContent["appKey"].Value<string>()); | ||
Assert.AreEqual(bugsplat.Notes, crashDetailsContent["comments"].Value<string>()); | ||
Assert.AreEqual(bugsplat.User, crashDetailsContent["user"].Value<string>()); | ||
} | ||
|
||
private Mock<HttpMessageHandler> CreateMockHttpClientForExceptionPost(string crashUploadUrl) | ||
{ | ||
var getCrashUploadUrlResponse = new HttpResponseMessage(); | ||
|
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,51 @@ | ||
|
||
using System; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using static BugSplatDotNetStandard.Utils.ArgumentContracts; | ||
|
||
namespace BugSplatDotNetStandard.Api | ||
{ | ||
/// <summary> | ||
/// Used to make requests to the BugSplat Versions API | ||
/// </summary> | ||
public class CrashDetailsClient | ||
{ | ||
private IBugSplatApiClient bugsplatApiClient; | ||
|
||
internal CrashDetailsClient(IBugSplatApiClient bugsplatApiClient) | ||
{ | ||
ThrowIfArgumentIsNull(bugsplatApiClient, "bugsplatApiClient"); | ||
|
||
this.bugsplatApiClient = bugsplatApiClient; | ||
} | ||
|
||
/// <summary> | ||
/// Create a CrashDetailsApiClient, consumer is responsible for authentication | ||
/// </summary> | ||
/// <param name="bugsplatApiClient">An authenticated instance of BugSplatApiClient that will be used for API requests</param> | ||
public static CrashDetailsClient Create(IBugSplatApiClient bugsplatApiClient) | ||
{ | ||
return new CrashDetailsClient(bugsplatApiClient); | ||
} | ||
|
||
/// <summary> | ||
/// Get details of a crash from a BugSplat database by ID | ||
/// </summary> | ||
/// <param name="database">BugSplat database for which crash information will be retrieved</param> | ||
/// <param name="id">Id of the crash to retrieve</param> | ||
public async Task<HttpResponseMessage> GetCrashDetails(string database, int id) | ||
{ | ||
ThrowIfArgumentIsNullOrEmpty(database, "database"); | ||
ThrowIfArgumentIsNullOrNegative(id, "id"); | ||
|
||
ThrowIfNotAuthenticated(bugsplatApiClient); | ||
|
||
var response = await this.bugsplatApiClient.GetAsync($"/api/crash/details?database={database}&id={id}"); | ||
|
||
ThrowIfHttpRequestFailed(response); | ||
|
||
return response; | ||
} | ||
} | ||
} |
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