Error When Using PostAsync
joshpennington opened this issue · comments
When writing tests on a post method I am getting the following exception
The value cannot be null or empty. Parameter name: mediaType
The code for doing the actual post request is (* is where the exception is happening):
`var url = InstanceUrl + Endpoint + "sobjects/" + sfObject.SFObjectName;
var json = JsonConvert.SerializeObject(sfObject,
new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore});
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
*var response = await Client.PostAsync(url, stringContent);`
The stack trace for the error is as follows:
Test method SM.Arch.Tests.Services.SalesForce.SalesForceServiceTests.CreateObject_Calls_Api threw exception: System.AggregateException: One or more errors occurred. (The value cannot be null or empty. Parameter name: mediaType) ---> System.ArgumentException: The value cannot be null or empty. Parameter name: mediaType at System.Net.Http.Headers.MediaTypeHeaderValue.CheckMediaTypeFormat(String mediaType, String parameterName) at System.Net.Http.Headers.MediaTypeHeaderValue..ctor(String mediaType) at System.Net.Http.StringContent..ctor(String content, Encoding encoding, String mediaType) at RichardSzalay.MockHttp.MockedRequestExtensions.<>c__DisplayClass25_0.<Respond>b__0(HttpRequestMessage _) at RichardSzalay.MockHttp.MockedRequestExtensions.<>c__DisplayClass39_0.<Respond>b__0(HttpRequestMessage req) at RichardSzalay.MockHttp.MockedRequestExtensions.<>c__DisplayClass40_0.<Respond>b__0(HttpRequestMessage req) at RichardSzalay.MockHttp.MockedRequest.SendAsync(HttpRequestMessage message, CancellationToken cancellationToken) at RichardSzalay.MockHttp.MockHttpMessageHandler.<>c__DisplayClass17_0.<SendAsync>b__0(Task
1 _)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask2.InnerInvoke() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) --- End of stack trace from previous location where exception was thrown --- at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot) --- End of stack trace from previous location where exception was thrown --- at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task
1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at SM.Arch.Services.SalesForce.SalesForceService.CreateObjectAsync(ISalesForceObject sfObject) in C:\Users\josh\dev\staymetrics\system2\src\SM.Arch.Core\Services\SalesForce\SalesForceService.cs:line 88
at SM.Arch.Services.SalesForce.SalesForceService.CreateObjectAsync(ISalesForceObject sfObject) in C:\Users\josh\dev\staymetrics\system2\src\SM.Arch.Core\Services\SalesForce\SalesForceService.cs:line 101
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at SM.Arch.Tests.Services.SalesForce.SalesForceServiceTests.CreateObject_Calls_Api() in C:\Users\josh\dev\staymetrics\system2\tests\SM.Arch.Tests\Services\SalesForce\SalesForceServiceTests.cs:line 87
As you can see I am setting the mediaType in my stringContent variable but it looks like it is maybe not being set somewhere in RichardSzalay.MockHttp.MockedRequestExtensions.
Thanks!
Sorry... this is the matching I am doing for this particular test:
mockHttp.When("https://instance.salesforce.com/services/data/v46.0/sobjects/*") .Respond("", "{ success: \"true\" }");
Nevermind. I noticed that I was leaving the mediaType blank in my .Respond.