Skip to content

Commit 911460e

Browse files
authored
Merge pull request #42 from ipinfo/fix-tests
Fix failing resproxy tests caused by data change
2 parents b72ae32 + fed6197 commit 911460e

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

IPinfo.Tests/IPApiTest.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Net;
4+
using System.Net.Http;
5+
using RichardSzalay.MockHttp;
36
using Xunit;
47

58
using IPinfo.Models;
@@ -106,24 +109,49 @@ public void TestNonBogonIPV6()
106109
public void TestGetResproxy()
107110
{
108111
string ip = "175.107.211.204";
112+
string mockResponseBody = @"{
113+
""ip"": ""175.107.211.204"",
114+
""last_seen"": ""2025-01-20"",
115+
""percent_days_seen"": 0.85,
116+
""service"": ""example_service""
117+
}";
118+
119+
var mockHttp = new MockHttpMessageHandler();
120+
mockHttp.When($"https://ipinfo.io/resproxy/{ip}*")
121+
.Respond("application/json", mockResponseBody);
122+
123+
var httpClient = mockHttp.ToHttpClient();
124+
109125
IPinfoClient client = new IPinfoClient.Builder()
110-
.AccessToken(Environment.GetEnvironmentVariable("IPINFO_TOKEN"))
126+
.AccessToken("test_token")
127+
.HttpClientConfig(config => config.HttpClientInstance(httpClient, true))
128+
.Cache(null)
111129
.Build();
112130

113131
IPResponseResproxy actual = client.IPApi.GetResproxy(ip);
114132

115133
Assert.Equal("175.107.211.204", actual.IP);
116-
Assert.NotNull(actual.LastSeen);
117-
Assert.NotNull(actual.PercentDaysSeen);
118-
Assert.NotNull(actual.Service);
134+
Assert.Equal("2025-01-20", actual.LastSeen);
135+
Assert.Equal(0.85, actual.PercentDaysSeen);
136+
Assert.Equal("example_service", actual.Service);
119137
}
120138

121139
[Fact]
122140
public void TestGetResproxyNotFound()
123141
{
124142
string ip = "8.8.8.8";
143+
string mockResponseBody = "{}";
144+
145+
var mockHttp = new MockHttpMessageHandler();
146+
mockHttp.When($"https://ipinfo.io/resproxy/{ip}*")
147+
.Respond("application/json", mockResponseBody);
148+
149+
var httpClient = mockHttp.ToHttpClient();
150+
125151
IPinfoClient client = new IPinfoClient.Builder()
126-
.AccessToken(Environment.GetEnvironmentVariable("IPINFO_TOKEN"))
152+
.AccessToken("test_token")
153+
.HttpClientConfig(config => config.HttpClientInstance(httpClient, true))
154+
.Cache(null)
127155
.Build();
128156

129157
IPResponseResproxy actual = client.IPApi.GetResproxy(ip);

IPinfo.Tests/IPinfo.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
12+
<PackageReference Include="RichardSzalay.MockHttp" Version="7.0.0" />
1213
<PackageReference Include="xunit" Version="2.4.1" />
1314
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
1415
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

0 commit comments

Comments
 (0)