1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
namespace Cepis.Tests;
/// <summary>A single lookup: the happy path, the coordinate, and the refusals.</summary>
public sealed class LookupTests : HttpTestBase
{
[Fact]
public async Task ReadsEveryField()
{
Server.Respond("/01001000", Reply.Of(200, Fixtures.SeAddress));
var address = await Client.LookupAsync("01001-000");
Assert.Equal("01001000", address.Cep);
Assert.Equal("01001-000", address.FormattedCep);
Assert.Equal("Praça da Sé", address.Logradouro);
Assert.Equal("lado ímpar", address.Complemento);
Assert.Equal("", address.Unidade);
Assert.Equal("Sé", address.Bairro);
Assert.Equal("São Paulo", address.Localidade);
Assert.Equal("SP", address.UF);
Assert.Equal("São Paulo", address.Estado);
Assert.Equal("Sudeste", address.Regiao);
Assert.Equal("3550308", address.Ibge);
Assert.Equal("1004", address.Gia);
Assert.Equal("11", address.Ddd);
Assert.Equal("7107", address.Siafi);
}
[Fact]
public async Task TheMaskIsStrippedBeforeTheRequest()
{
Server.Respond("/01001000", Reply.Of(200, Fixtures.SeAddress));
await Client.LookupAsync("01001-000");
Assert.Equal("/01001000", Assert.Single(Server.Calls).Path);
}
[Fact]
public async Task ReadsTheCoordinate()
{
Server.Respond("/01001000", Reply.Of(200, Fixtures.SeAddress));
var address = await Client.LookupAsync("01001000");
var geo = address.Geo;
Assert.NotNull(geo);
Assert.Equal(-23.567202, geo.Lat, 6);
Assert.Equal(-46.649659, geo.Lon, 6);
Assert.Equal(1084, geo.RaioM);
Assert.Equal("logradouro", geo.Precisao);
Assert.True(geo.IsPrecise);
}
[Fact]
public async Task AnAbsentCoordinateIsNull()
{
Server.Respond("/20040020", Reply.Of(200, Fixtures.NoGeoAddress));
var address = await Client.LookupAsync("20040020");
Assert.Null(address.Geo);
Assert.Equal("Rio de Janeiro", address.Localidade);
}
[Fact]
public async Task AMalformedCoordinateDoesNotLoseTheAddress()
{
Server.Respond("/01001000", Reply.Of(200, Fixtures.BrokenGeoAddress));
var address = await Client.LookupAsync("01001000");
Assert.Null(address.Geo);
Assert.Equal("São Paulo", address.Localidade);
}
[Fact]
public async Task ACoordinateOfTheWrongShapeIsDropped()
{
Server.Respond("/01001000", Reply.Of(200, """{"cep":"01001000","localidade":"X","geo":[]}"""));
var address = await Client.LookupAsync("01001000");
Assert.Null(address.Geo);
Assert.Equal("X", address.Localidade);
}
[Fact]
public async Task AFieldThisLibraryDoesNotKnowIsKept()
{
Server.Respond("/01001000", Reply.Of(200, Fixtures.FutureAddress));
var address = await Client.LookupAsync("01001000");
Assert.Equal("São Paulo", address.Localidade);
var extra = address.Extra;
Assert.NotNull(extra);
Assert.Equal("01000000", extra["cep_pai"].GetString());
}
[Fact]
public async Task AMissingCepThrows()
{
Server.Respond("/99999999", Reply.Of(404, Fixtures.NotFound));
var failure = await Assert.ThrowsAsync<CepNotFoundException>(
() => Client.LookupAsync("99999999"));
Assert.Contains("99999999", failure.Message, StringComparison.Ordinal);
}
[Fact]
public async Task A404IsNeverRetried()
{
Server.Respond("/99999999", Reply.Of(404, Fixtures.NotFound));
await Assert.ThrowsAsync<CepNotFoundException>(() => Client.LookupAsync("99999999"));
Assert.Equal(1, Server.Count("/99999999"));
}
[Fact]
public async Task A400CarriesTheApiMessage()
{
Server.Respond("/00000000", Reply.Of(400, Fixtures.InvalidCep));
var failure = await Assert.ThrowsAsync<InvalidCepException>(
() => Client.LookupAsync("00000000"));
Assert.Equal("CEP deve ter 8 dígitos", failure.Message);
Assert.Equal(1, Server.Count("/00000000"));
}
[Fact]
public async Task AMalformedCepCostsNoRequest()
{
await Assert.ThrowsAsync<InvalidCepException>(() => Client.LookupAsync("1234567"));
Assert.Empty(Server.Calls);
}
[Fact]
public async Task AnUnintelligibleBodyIsDiagnosed()
{
Server.Respond("/01001000", Reply.Of(200, "<html><body>captive portal</body></html>"));
await Assert.ThrowsAsync<InvalidResponseException>(() => Client.LookupAsync("01001000"));
}
[Fact]
public async Task AJsonBodyThatIsNotAnObjectIsDiagnosed()
{
Server.Respond("/01001000", Reply.Of(200, "null"));
await Assert.ThrowsAsync<InvalidResponseException>(() => Client.LookupAsync("01001000"));
}
[Fact]
public async Task ACancelledCallDoesNotBecomeANetworkError()
{
Server.Respond("/01001000", Reply.Of(200, Fixtures.SeAddress));
using var cancellation = new CancellationTokenSource();
await cancellation.CancelAsync();
await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => Client.LookupAsync("01001000", cancellation.Token));
}
}
|