Skip to content

Commit

Permalink
Set json encoding for HTTP requests to UTF8
Browse files Browse the repository at this point in the history
  • Loading branch information
kayone committed Jun 6, 2019
1 parent 36eadf9 commit ac18eab
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/AppGet.Tests/AppGet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<Compile Include="FileTransfer\TransferCacheServiceFixture.cs" />
<Compile Include="HostSystem\EnvInfoFixture.cs" />
<Compile Include="HostSystem\MachineIdFixture.cs" />
<Compile Include="HttpBinResponse.cs" />
<Compile Include="Http\HttpClientFixture.cs" />
<Compile Include="Http\UserAgentBuilderFixture.cs" />
<Compile Include="Infrastructure\Composition\ContainerBuilderFixture.cs" />
Expand Down
24 changes: 23 additions & 1 deletion src/AppGet.Tests/Http/HttpClientFixture.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using AppGet.HostSystem;
using AppGet.Http;
using AppGet.Manifest;
using FluentAssertions;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using HttpClient = AppGet.Http.HttpClient;

namespace AppGet.Tests.Http
{
Expand Down Expand Up @@ -35,12 +40,29 @@ public void should_throw_name_resolution_exception()
ex.Message.Should().Contain("The remote name could not be resolved");
}


[Test]
public void should_throw_for_non_success()
{
Mocker.SetInstance<IUserAgentBuilder>(new UserAgentBuilder(new EnvInfo()));
Assert.ThrowsAsync<HttpException>(async () => await Subject.GetAsync(new Uri("https://google.com/not-a-valid-path"), TimeSpan.FromSeconds(10)));
}

[Test]
public async Task post_should_send_correct_encoding()
{
Mocker.SetInstance<IUserAgentBuilder>(new UserAgentBuilder(new EnvInfo()));

var request = new HttpRequestMessage(HttpMethod.Post, new Uri("https://httpbin.org/post"));
request.Content = new JsonContent(new PackageManifest());

var res = await Subject.SendAsync(request, TimeSpan.FromSeconds(10));
res.EnsureSuccessStatusCode();

var binResp = await res.Deserialize<HttpBinResponse>();

binResp.Headers["Content-Type"].Should().Be("Application/Json; charset=utf-8");
}
}


}
17 changes: 17 additions & 0 deletions src/AppGet.Tests/HttpBinResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

namespace AppGet.Tests
{
public class HttpBinResponse
{
public Dictionary<string, string> Args { get; set; }
public string Data { get; set; }
public Dictionary<string, string> Files { get; set; }
public Dictionary<string, string> Form { get; set; }
public Dictionary<string, string> Headers { get; set; }
public JObject Json { get; set; }
public string Origin { get; set; }
public string Url { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/AppGet/Http/JsonContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace AppGet.Http
public class JsonContent : StringContent
{
public JsonContent(object obj)
: base(Json.Serialize(obj), Encoding.Default, "Application/Json")
: base(Json.Serialize(obj), Encoding.UTF8, "Application/Json")
{
}
}
Expand Down

0 comments on commit ac18eab

Please sign in to comment.