|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Net; |
| 4 | +using System.Net.Http; |
| 5 | +using RichardSzalay.MockHttp; |
3 | 6 | using Xunit; |
4 | 7 |
|
5 | 8 | using IPinfo.Models; |
@@ -106,24 +109,49 @@ public void TestNonBogonIPV6() |
106 | 109 | public void TestGetResproxy() |
107 | 110 | { |
108 | 111 | 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 | + |
109 | 125 | 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) |
111 | 129 | .Build(); |
112 | 130 |
|
113 | 131 | IPResponseResproxy actual = client.IPApi.GetResproxy(ip); |
114 | 132 |
|
115 | 133 | 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); |
119 | 137 | } |
120 | 138 |
|
121 | 139 | [Fact] |
122 | 140 | public void TestGetResproxyNotFound() |
123 | 141 | { |
124 | 142 | 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 | + |
125 | 151 | 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) |
127 | 155 | .Build(); |
128 | 156 |
|
129 | 157 | IPResponseResproxy actual = client.IPApi.GetResproxy(ip); |
|
0 commit comments