Skip to content

Commit e25c11e

Browse files
mshlzlucianopf
authored andcommitted
add unit tests
1 parent a6fe73c commit e25c11e

File tree

4 files changed

+275
-2
lines changed

4 files changed

+275
-2
lines changed

test/unit/cep-promise-node.spec.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ describe('[unit] cep-promise for node', () => {
3434
path.join(__dirname, '/fixtures/response-cep-05010000-found.xml')
3535
)
3636

37+
nock('https://buscacepinter.correios.com.br')
38+
.get('/app/cep/carrega-cep.php?cep=05010000')
39+
.replyWithFile(
40+
200,
41+
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
42+
)
43+
3744
nock('https://viacep.com.br')
3845
.get('/ws/05010000/json/')
3946
.replyWithFile(
@@ -156,6 +163,13 @@ describe('[unit] cep-promise for node', () => {
156163
path.join(__dirname, '/fixtures/response-cep-05010000-found.xml')
157164
)
158165

166+
nock('https://buscacepinter.correios.com.br')
167+
.get('/app/cep/carrega-cep.php?cep=05010000')
168+
.replyWithFile(
169+
200,
170+
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
171+
)
172+
159173
nock('https://viacep.com.br')
160174
.get('/ws/05010000/json/')
161175
.replyWithFile(
@@ -199,6 +213,13 @@ describe('[unit] cep-promise for node', () => {
199213
path.join(__dirname, '/fixtures/response-cep-05010000-found.xml')
200214
)
201215

216+
nock('https://buscacepinter.correios.com.br')
217+
.get('/app/cep/carrega-cep.php?cep=05010000')
218+
.replyWithFile(
219+
200,
220+
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
221+
)
222+
202223
nock('https://viacep.com.br')
203224
.get('/ws/05010000/json/')
204225
.replyWithFile(
@@ -242,6 +263,64 @@ describe('[unit] cep-promise for node', () => {
242263
path.join(__dirname, '/fixtures/response-cep-05010000-found.xml')
243264
)
244265

266+
nock('https://buscacepinter.correios.com.br')
267+
.get('/app/cep/carrega-cep.php?cep=05010000')
268+
.replyWithFile(
269+
200,
270+
path.join(__dirname, '/fixtures/correios-alt-cep-99999999-error.json')
271+
)
272+
273+
nock('https://viacep.com.br')
274+
.get('/ws/05010000/json/')
275+
.replyWithFile(
276+
200,
277+
path.join(__dirname, '/fixtures/viacep-cep-99999999-error.json')
278+
)
279+
280+
nock('https://cep.widenet.host')
281+
.get('/busca-cep/api/cep/05010000.json')
282+
.replyWithFile(
283+
200,
284+
path.join(__dirname, '/fixtures/widenet-cep-99999999-error.json')
285+
)
286+
287+
nock('https://brasilapi.com.br/')
288+
.get('/api/cep/v1/99999999')
289+
.replyWithFile(
290+
404,
291+
path.join(__dirname, '/fixtures/brasilapi-cep-99999999-error.json')
292+
)
293+
294+
295+
return cep('05010000')
296+
.then(address => expect(address).to.deep.equal({
297+
cep: '05010000',
298+
state: 'SP',
299+
city: 'São Paulo',
300+
neighborhood: 'Perdizes',
301+
street: 'Rua Caiubi',
302+
service: 'correios'
303+
})
304+
)
305+
})
306+
})
307+
308+
describe('Should succeed only with correios-alt service', () => {
309+
it('should fulfill with correct address', () => {
310+
nock('https://apps.correios.com.br')
311+
.post('/SigepMasterJPA/AtendeClienteService/AtendeCliente')
312+
.replyWithFile(
313+
500,
314+
path.join(__dirname, '/fixtures/response-unknown-format.xml')
315+
)
316+
317+
nock('https://buscacepinter.correios.com.br')
318+
.get('/app/cep/carrega-cep.php?cep=05010000')
319+
.replyWithFile(
320+
200,
321+
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
322+
)
323+
245324
nock('https://viacep.com.br')
246325
.get('/ws/05010000/json/')
247326
.replyWithFile(
@@ -286,6 +365,13 @@ describe('[unit] cep-promise for node', () => {
286365
path.join(__dirname, '/fixtures/response-unknown-format.xml')
287366
)
288367

368+
nock('https://buscacepinter.correios.com.br')
369+
.get('/app/cep/carrega-cep.php?cep=05010000')
370+
.replyWithFile(
371+
200,
372+
path.join(__dirname, '/fixtures/correios-alt-cep-99999999-error.json')
373+
)
374+
289375
nock('https://viacep.com.br')
290376
.get('/ws/05010000/json/')
291377
.replyWithFile(
@@ -329,6 +415,13 @@ describe('[unit] cep-promise for node', () => {
329415
path.join(__dirname, '/fixtures/response-unknown-format.xml')
330416
)
331417

418+
nock('https://buscacepinter.correios.com.br')
419+
.get('/app/cep/carrega-cep.php?cep=05010000')
420+
.replyWithFile(
421+
200,
422+
path.join(__dirname, '/fixtures/correios-alt-cep-99999999-error.json')
423+
)
424+
332425
nock('https://viacep.com.br')
333426
.get('/ws/05010000/json/')
334427
.replyWithFile(
@@ -371,6 +464,13 @@ describe('[unit] cep-promise for node', () => {
371464
path.join(__dirname, '/fixtures/response-unknown-format.xml')
372465
)
373466

467+
nock('https://buscacepinter.correios.com.br')
468+
.get('/app/cep/carrega-cep.php?cep=05010000')
469+
.replyWithFile(
470+
200,
471+
path.join(__dirname, '/fixtures/correios-alt-cep-99999999-error.json')
472+
)
473+
374474
nock('https://viacep.com.br')
375475
.get('/ws/05010000/json/')
376476
.replyWithFile(
@@ -413,6 +513,13 @@ describe('[unit] cep-promise for node', () => {
413513
path.join(__dirname, '/fixtures/response-bad-xml.xml')
414514
)
415515

516+
nock('https://buscacepinter.correios.com.br')
517+
.get('/app/cep/carrega-cep.php?cep=05010000')
518+
.replyWithFile(
519+
200,
520+
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
521+
)
522+
416523
nock('https://viacep.com.br')
417524
.get('/ws/05010000/json/')
418525
.replyWithFile(
@@ -454,6 +561,13 @@ describe('[unit] cep-promise for node', () => {
454561
'getaddrinfo ENOTFOUND apps.correios.com.br apps.correios.com.br:443'
455562
)
456563

564+
nock('https://buscacepinter.correios.com.br')
565+
.get('/app/cep/carrega-cep.php?cep=05010000')
566+
.replyWithFile(
567+
200,
568+
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
569+
)
570+
457571
nock('https://viacep.com.br')
458572
.get('/ws/05010000/json/')
459573
.replyWithFile(
@@ -496,6 +610,13 @@ describe('[unit] cep-promise for node', () => {
496610
path.join(__dirname, '/fixtures/response-cep-not-found.xml')
497611
)
498612

613+
nock('https://buscacepinter.correios.com.br')
614+
.get('/app/cep/carrega-cep.php?cep=05010000')
615+
.replyWithFile(
616+
200,
617+
path.join(__dirname, '/fixtures/correios-alt-cep-99999999-error.json')
618+
)
619+
499620
nock('https://viacep.com.br')
500621
.get('/ws/99999999/json/')
501622
.replyWithFile(
@@ -571,6 +692,10 @@ describe('[unit] cep-promise for node', () => {
571692
'getaddrinfo ENOTFOUND apps.correios.com.br apps.correios.com.br:443'
572693
)
573694

695+
nock('https://buscacepinter.correios.com.br')
696+
.get('/app/cep/carrega-cep.php?cep=05010000')
697+
.reply(400, '<h2>Bad Request (400)</h2>')
698+
574699
nock('https://viacep.com.br')
575700
.get('/ws/05010000/json/')
576701
.reply(400, '<h2>Bad Request (400)</h2>')
@@ -591,6 +716,10 @@ describe('[unit] cep-promise for node', () => {
591716
message: 'Todos os serviços de CEP retornaram erro.',
592717
type: 'service_error',
593718
errors: [
719+
{
720+
message: 'Erro ao se conectar com o serviço dos Correios.',
721+
service: 'correios'
722+
},
594723
{
595724
message: 'Erro ao se conectar com o serviço dos Correios.',
596725
service: 'correios'
@@ -623,6 +752,10 @@ describe('[unit] cep-promise for node', () => {
623752
path.join(__dirname, '/fixtures/response-bad-xml.xml')
624753
)
625754

755+
nock('https://buscacepinter.correios.com.br')
756+
.get('/app/cep/carrega-cep.php?cep=05010000')
757+
.reply(200, {erro:true})
758+
626759
nock('https://viacep.com.br')
627760
.get('/ws/05010000/json/')
628761
.reply(400, '<h2>Bad Request (400)</h2>')
@@ -647,6 +780,10 @@ describe('[unit] cep-promise for node', () => {
647780
message: 'Não foi possível interpretar o XML de resposta.',
648781
service: 'correios'
649782
},
783+
{
784+
message: 'CEP não encontrado na base dos Correios.',
785+
service: 'correios'
786+
},
650787
{
651788
message: 'Erro ao se conectar com o serviço ViaCEP.',
652789
service: 'viacep'
@@ -674,6 +811,10 @@ describe('[unit] cep-promise for node', () => {
674811
'getaddrinfo ENOTFOUND apps.correios.com.br apps.correios.com.br:443'
675812
)
676813

814+
nock('https://buscacepinter.correios.com.br')
815+
.get('/app/cep/carrega-cep.php?cep=05010000')
816+
.reply(200, {erro:true})
817+
677818
nock('https://viacep.com.br')
678819
.get('/ws/05010000/json/')
679820
.replyWithError(
@@ -704,6 +845,10 @@ describe('[unit] cep-promise for node', () => {
704845
message: 'Erro ao se conectar com o serviço dos Correios.',
705846
service: 'correios'
706847
},
848+
{
849+
message: 'CEP não encontrado na base dos Correios.',
850+
service: 'correios'
851+
},
707852
{
708853
message: 'Erro ao se conectar com o serviço ViaCEP.',
709854
service: 'viacep'

0 commit comments

Comments
 (0)