Skip to content

Commit 7437b1a

Browse files
authored
Merge pull request #52 from RoushTech/release/0.5.2
Release/0.5.2
2 parents 9d19dba + 4b39108 commit 7437b1a

File tree

10 files changed

+101
-19
lines changed

10 files changed

+101
-19
lines changed

CHANGELOG.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
# 0.5.1
1+
# 0.5.2
2+
<sup>Released: 2017/4/21</sup>
3+
4+
## Features
5+
6+
- Updated documentation. #49
7+
- Can now disable Rollbar from configuration. #48
8+
9+
## Bug Fixes
10+
11+
- Fix issue with trying to access Request.Form variables during POSTs that may not actually support form variables. #54
12+
13+
# 0.5.1
214
<sup>Released: 2017/3/10</sup>
315

416
## Features

Readme.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# RollbarDotNet
2+
[![NuGet](https://img.shields.io/nuget/v/RollbarDotNet.svg)]()
3+
4+
[![Build](https://img.shields.io/teamcity/https/teamcity.roushtech.net/s/RollbarDotNet_Build.svg)]()
5+
[![SonarQube Tech Debt](https://img.shields.io/sonar/https/sonarqube.roushtech.net/RollbarDotNet/tech_debt.svg)]()
6+
[![SonarQube Coverage](https://img.shields.io/sonar/https/sonarqube.roushtech.net/RollbarDotNet/coverage.svg)]()
27

38
Rollbar support for your .NET Core projects, relies on dependency injection and hooks up to your ASP.NET Core pipeline for easy
49

@@ -99,7 +104,11 @@ var rollbar = new Rollbar(
99104
new ConfigurationBuilder(rollbarOptions),
100105
new EnvironmentBuilder(new SystemDateTime()), // SystemDateTime abstracts DateTime for mocking
101106
new NotifierBuilder()
102-
}
107+
},
108+
new IExceptionBuilder[] {
109+
new ExceptionBuilder()
110+
},
111+
new RollBarClient(rollbarOptions)
103112
);
104113

105114
try

src/RollbarDotNet/Builder/RequestBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ protected void BuildRequest(Request request)
4444
{
4545
request.Get = this.QueryToDictionary(context.Request.Query);
4646
}
47-
else if (context.Request.Method.Equals("POST", StringComparison.OrdinalIgnoreCase))
47+
else if (context.Request.Method.Equals("POST", StringComparison.OrdinalIgnoreCase)
48+
&& context.Request.HasFormContentType)
4849
{
4950
request.Post = this.FormToDictionary(context.Request.Form);
5051
}

src/RollbarDotNet/Configuration/RollbarOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ public class RollbarOptions
44
{
55
public string AccessToken { get; set; }
66

7+
public bool Disabled { get; set; }
8+
79
public string Environment { get; set; }
810

911
public BlacklistConfiguration Blacklist { get; set; }

src/RollbarDotNet/Core/ServiceExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ public static class ServiceExtensions
1212
public static IServiceCollection AddRollbar(this IServiceCollection services)
1313
{
1414
return services
15-
.AddSingleton<IBuilder, Builder.ConfigurationBuilder>()
15+
.AddSingleton<IBuilder, ConfigurationBuilder>()
1616
.AddSingleton<IBuilder, EnvironmentBuilder>()
1717
.AddSingleton<IBuilder, NotifierBuilder>()
1818
.AddSingleton<IDateTime, SystemDateTime>()
1919
.AddSingleton<IEnvironment, SystemEnvironment>()
2020
.AddSingleton<IBlacklister, ConfigurationBlacklister>()
2121
.AddSingleton<IBlacklistCollection, BlacklistCollection>()
2222
.AddSingleton<IExceptionBuilder, ExceptionBuilder>()
23+
.AddSingleton<RollbarClient>()
2324
.AddScoped<Rollbar>();
2425
}
2526

src/RollbarDotNet/Rollbar.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
public class Rollbar
99
{
1010
public Rollbar(IEnumerable<IBuilder> builders,
11-
IEnumerable<IExceptionBuilder> exceptionBuilders)
11+
IEnumerable<IExceptionBuilder> exceptionBuilders,
12+
RollbarClient rollbarClient)
1213
{
1314
this.Builders = builders;
1415
this.ExceptionBuilders = exceptionBuilders;
15-
this.RollbarClient = new RollbarClient();
16+
this.RollbarClient = rollbarClient;
1617
}
1718

1819
protected IEnumerable<IBuilder> Builders { get; set; }

src/RollbarDotNet/RollbarClient.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,42 @@
11
namespace RollbarDotNet
22
{
3+
using Configuration;
4+
using Microsoft.Extensions.Options;
35
using Newtonsoft.Json;
6+
using Payloads;
47
using System;
5-
using System.Threading.Tasks;
68
using System.Net.Http;
79
using System.Net.Http.Headers;
8-
using Payloads;
10+
using System.Threading.Tasks;
911

1012
public class RollbarClient
1113
{
12-
public RollbarClient()
14+
public RollbarClient(IOptions<RollbarOptions> rollbarOptions)
1315
{
1416
this.Configuration = new Configuration.Configuration();
17+
this.RollbarOptions = rollbarOptions.Value;
1518
}
1619

1720
public Configuration.Configuration Configuration { get; set; }
1821

22+
protected RollbarOptions RollbarOptions { get; set; }
23+
1924
protected Uri RollbarUri { get { return new Uri("https://api.rollbar.com/api/1/item/"); } }
2025

2126
public async Task<Response> Send(Payload payload)
2227
{
23-
string json = this.Serialize(payload);
28+
if (this.RollbarOptions.Disabled)
29+
{
30+
return new Response
31+
{
32+
Result = new Result
33+
{
34+
Uuid = null
35+
}
36+
};
37+
}
2438

39+
string json = this.Serialize(payload);
2540
using (var httpClient = new HttpClient())
2641
{
2742
httpClient.DefaultRequestHeaders.Accept.Clear();

src/RollbarDotNet/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"Error"
1616
]
1717
},
18-
"version": "0.5.1",
18+
"version": "0.5.2",
1919
"dependencies": {
2020
"Microsoft.AspNetCore.Hosting.Abstractions": "1.0.0",
2121
"Microsoft.AspNetCore.Http": "1.0.0",

test/RollbarDotNet.Tests/Builder/RequestBuilderTests.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Moq;
77
using Payloads;
88
using RollbarDotNet.Builder;
9+
using System;
910
using System.Collections.Generic;
1011
using System.Net;
1112
using Xunit;
@@ -113,7 +114,23 @@ public void Blacklists_Payload_QueryString()
113114
Assert.Equal("?query=test&blacklist=**********", queryString);
114115
}
115116

116-
public IHttpContextAccessor CreateIHttpContextAccessor(string method)
117+
/// <summary>
118+
/// Caused when you POST a JSON payload -- Request.Form is NOT valid and the call will throw, this checks
119+
/// and makes sure we're properly checking the HasFormContentType flag.
120+
/// https://github.com/RoushTech/RollbarDotNet/issues/54
121+
/// </summary>
122+
[Fact]
123+
public void Bug_ThrowsIncorrectContentType()
124+
{
125+
var mock = this.CreateIHttpContextAccessor("POST");
126+
mock.Setup(h => h.HttpContext.Request.HasFormContentType).Returns(false);
127+
mock.Setup(h => h.HttpContext.Request.Form).Throws(new InvalidOperationException("Incorrect Content-Type: application/json; charset=UTF-8"));
128+
var requestBuilder = new RequestBuilder(this.GenerateBacklistCollection(), mock.Object);
129+
var payload = new Payload();
130+
requestBuilder.Execute(payload);
131+
}
132+
133+
public Mock<IHttpContextAccessor> CreateIHttpContextAccessor(string method)
117134
{
118135
var httpContextAccessorMock = new Mock<IHttpContextAccessor>();
119136
httpContextAccessorMock.Setup(h => h.HttpContext.Request.Scheme).Returns("http");
@@ -123,20 +140,24 @@ public IHttpContextAccessor CreateIHttpContextAccessor(string method)
123140
httpContextAccessorMock.Setup(h => h.HttpContext.Features.Get<IHttpConnectionFeature>().RemoteIpAddress).Returns(IPAddress.Loopback);
124141
httpContextAccessorMock.Setup(h => h.HttpContext.Request.Headers).Returns(this.GenerateHeaderDictionary);
125142
httpContextAccessorMock.Setup(h => h.HttpContext.Request.Query).Returns(this.GenerateQueryCollection);
143+
httpContextAccessorMock.Setup(h => h.HttpContext.Request.HasFormContentType).Returns(true);
126144
httpContextAccessorMock.Setup(h => h.HttpContext.Request.Form).Returns(this.GenerateFormCollection);
127145
httpContextAccessorMock.Setup(h => h.HttpContext.Request.Cookies).Returns(this.GenerateCookieCollection);
128146
httpContextAccessorMock.Setup(h => h.HttpContext.Request.QueryString).Returns(new QueryString("?query=test&blacklist=here"));
129-
return httpContextAccessorMock.Object;
147+
return httpContextAccessorMock;
130148
}
131149

132-
protected Payload GeneratePayload(bool enableBlacklist = false, string method = "GET")
150+
protected IBlacklistCollection GenerateBacklistCollection(bool enableBlacklist = false)
133151
{
134-
135152
var blacklistCollectionMock = new Mock<IBlacklistCollection>();
136153
blacklistCollectionMock.Setup(b => b.Check("test")).Returns(false);
137154
blacklistCollectionMock.Setup(b => b.Check("blacklist")).Returns(enableBlacklist);
155+
return blacklistCollectionMock.Object;
156+
}
138157

139-
var requestBuilder = new RequestBuilder(blacklistCollectionMock.Object, this.CreateIHttpContextAccessor(method));
158+
protected Payload GeneratePayload(bool enableBlacklist = false, string method = "GET")
159+
{
160+
var requestBuilder = new RequestBuilder(this.GenerateBacklistCollection(enableBlacklist), this.CreateIHttpContextAccessor(method).Object);
140161
var payload = new Payload();
141162
requestBuilder.Execute(payload);
142163
return payload;

test/RollbarDotNet.Tests/DependencyInjection/WebDependencyInjectionTests.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Core;
55
using Microsoft.AspNetCore.Hosting;
66
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Options;
78
using Moq;
89
using System;
910
using System.Threading.Tasks;
@@ -44,20 +45,39 @@ public async Task SucessfullyReportError()
4445
}
4546
catch(Exception exception)
4647
{
47-
await this.Rollbar.SendException(exception);
48+
var response = await this.Rollbar.SendException(exception);
49+
Assert.False(string.IsNullOrEmpty(response.Result.Uuid));
4850
}
4951
}
5052

5153
[Fact]
5254
public async Task SuccessfullyReportMessage()
5355
{
54-
await this.Rollbar.SendMessage("Hello");
56+
var response = await this.Rollbar.SendMessage("Hello");
57+
Assert.False(string.IsNullOrEmpty(response.Result.Uuid));
5558
}
5659

5760
[Fact]
5861
public async Task SuccessfullyReportMessageWithLevel()
5962
{
60-
await this.Rollbar.SendMessage(RollbarLevel.Debug, "Hello");
63+
var response = await this.Rollbar.SendMessage(RollbarLevel.Debug, "Hello");
64+
Assert.False(string.IsNullOrEmpty(response.Result.Uuid));
65+
}
66+
67+
[Fact]
68+
public async Task DisabledRollbar()
69+
{
70+
var options = this.ServiceProvider.GetService<IOptions<RollbarOptions>>().Value;
71+
this.Services.Configure<RollbarOptions>(o =>
72+
{
73+
o.AccessToken = options.AccessToken;
74+
o.Disabled = true;
75+
o.Environment = options.Environment;
76+
});
77+
var serviceProvider = this.Services.BuildServiceProvider();
78+
var rollbar = serviceProvider.GetService<Rollbar>();
79+
var response = await rollbar.SendMessage(RollbarLevel.Debug, "Hello");
80+
Assert.Null(response.Result.Uuid);
6181
}
6282
}
6383
}

0 commit comments

Comments
 (0)