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 ;
0 commit comments