Skip to content

Commit 306548d

Browse files
committed
fix: correios-alt provider
- fix correios api call - fix unit tests - fix e2e tests
1 parent 695ee4e commit 306548d

File tree

4 files changed

+43
-39
lines changed

4 files changed

+43
-39
lines changed

src/services/correios-alt.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import ServiceError from '../errors/service.js'
55

66
export default function fetchCorreiosAltAPIService(
77
cepWithLeftPad,
8-
proxyURL = ''
8+
configurations
99
) {
10-
const url = `${proxyURL}https://buscacepinter.correios.com.br/app/cep/carrega-cep.php?cep=${cepWithLeftPad}`
10+
const url = 'https://buscacepinter.correios.com.br/app/cep/carrega-cep.php'
1111
const options = {
12-
method: 'GET',
12+
method: 'POST',
1313
mode: 'cors',
1414
headers: {
15-
'content-type': 'application/json;charset=utf-8'
16-
}
15+
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
16+
},
17+
body: `cep=${cepWithLeftPad}`,
18+
timeout: configurations.timeout || 30000
1719
}
1820

1921
return fetch(url, options)
@@ -22,14 +24,13 @@ export default function fetchCorreiosAltAPIService(
2224
.catch(throwApplicationError)
2325
}
2426

25-
async function parseResponse(response) {
26-
response = await response.json()
27-
28-
if (response.erro === true || response.total === 0) {
29-
throw new Error('CEP não encontrado na base dos Correios.')
30-
}
31-
32-
return response
27+
function parseResponse(response) {
28+
return response.json().then(result => {
29+
if (result.erro === true || result.total === 0) {
30+
throw new Error('CEP não encontrado na base dos Correios.')
31+
}
32+
return result
33+
})
3334
}
3435

3536
function extractCepValuesFromResponse(response) {

test/e2e/cep-promise.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('[e2e] cep-promise', () => {
6262
service: 'correios'
6363
},
6464
{
65-
message: 'CEP não encontrado na base dos Correios.',
65+
message: 'Erro ao se conectar com o serviço dos Correios Alt.',
6666
service: 'correios-alt'
6767
},
6868
{

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('[unit] cep-promise for node', () => {
3535
)
3636

3737
nock('https://buscacepinter.correios.com.br')
38-
.get('/app/cep/carrega-cep.php?cep=05010000')
38+
.post('/app/cep/carrega-cep.php')
3939
.replyWithFile(
4040
200,
4141
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
@@ -164,7 +164,7 @@ describe('[unit] cep-promise for node', () => {
164164
)
165165

166166
nock('https://buscacepinter.correios.com.br')
167-
.get('/app/cep/carrega-cep.php?cep=05010000')
167+
.post('/app/cep/carrega-cep.php')
168168
.replyWithFile(
169169
200,
170170
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
@@ -214,7 +214,7 @@ describe('[unit] cep-promise for node', () => {
214214
)
215215

216216
nock('https://buscacepinter.correios.com.br')
217-
.get('/app/cep/carrega-cep.php?cep=05010000')
217+
.post('/app/cep/carrega-cep.php')
218218
.replyWithFile(
219219
200,
220220
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
@@ -264,7 +264,7 @@ describe('[unit] cep-promise for node', () => {
264264
)
265265

266266
nock('https://buscacepinter.correios.com.br')
267-
.get('/app/cep/carrega-cep.php?cep=05010000')
267+
.post('/app/cep/carrega-cep.php')
268268
.replyWithFile(
269269
200,
270270
path.join(__dirname, '/fixtures/correios-alt-cep-99999999-error.json')
@@ -299,7 +299,7 @@ describe('[unit] cep-promise for node', () => {
299299
city: 'São Paulo',
300300
neighborhood: 'Perdizes',
301301
street: 'Rua Caiubi',
302-
service: 'correios-alt'
302+
service: 'correios'
303303
})
304304
)
305305
})
@@ -315,7 +315,7 @@ describe('[unit] cep-promise for node', () => {
315315
)
316316

317317
nock('https://buscacepinter.correios.com.br')
318-
.get('/app/cep/carrega-cep.php?cep=05010000')
318+
.post('/app/cep/carrega-cep.php')
319319
.replyWithFile(
320320
200,
321321
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
@@ -350,7 +350,7 @@ describe('[unit] cep-promise for node', () => {
350350
city: 'São Paulo',
351351
neighborhood: 'Perdizes',
352352
street: 'Rua Caiubi',
353-
service: 'correios'
353+
service: 'correios-alt'
354354
})
355355
)
356356
})
@@ -366,7 +366,7 @@ describe('[unit] cep-promise for node', () => {
366366
)
367367

368368
nock('https://buscacepinter.correios.com.br')
369-
.get('/app/cep/carrega-cep.php?cep=05010000')
369+
.post('/app/cep/carrega-cep.php')
370370
.replyWithFile(
371371
200,
372372
path.join(__dirname, '/fixtures/correios-alt-cep-99999999-error.json')
@@ -416,7 +416,7 @@ describe('[unit] cep-promise for node', () => {
416416
)
417417

418418
nock('https://buscacepinter.correios.com.br')
419-
.get('/app/cep/carrega-cep.php?cep=05010000')
419+
.post('/app/cep/carrega-cep.php')
420420
.replyWithFile(
421421
200,
422422
path.join(__dirname, '/fixtures/correios-alt-cep-99999999-error.json')
@@ -465,7 +465,7 @@ describe('[unit] cep-promise for node', () => {
465465
)
466466

467467
nock('https://buscacepinter.correios.com.br')
468-
.get('/app/cep/carrega-cep.php?cep=05010000')
468+
.post('/app/cep/carrega-cep.php')
469469
.replyWithFile(
470470
200,
471471
path.join(__dirname, '/fixtures/correios-alt-cep-99999999-error.json')
@@ -514,7 +514,7 @@ describe('[unit] cep-promise for node', () => {
514514
)
515515

516516
nock('https://buscacepinter.correios.com.br')
517-
.get('/app/cep/carrega-cep.php?cep=05010000')
517+
.post('/app/cep/carrega-cep.php')
518518
.replyWithFile(
519519
200,
520520
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
@@ -562,7 +562,7 @@ describe('[unit] cep-promise for node', () => {
562562
)
563563

564564
nock('https://buscacepinter.correios.com.br')
565-
.get('/app/cep/carrega-cep.php?cep=05010000')
565+
.post('/app/cep/carrega-cep.php')
566566
.replyWithFile(
567567
200,
568568
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
@@ -611,7 +611,7 @@ describe('[unit] cep-promise for node', () => {
611611
)
612612

613613
nock('https://buscacepinter.correios.com.br')
614-
.get('/app/cep/carrega-cep.php?cep=05010000')
614+
.post('/app/cep/carrega-cep.php')
615615
.replyWithFile(
616616
200,
617617
path.join(__dirname, '/fixtures/correios-alt-cep-99999999-error.json')
@@ -651,7 +651,7 @@ describe('[unit] cep-promise for node', () => {
651651
service: 'correios'
652652
},
653653
{
654-
message: 'Erro ao se conectar com o serviço dos Correios Alt.',
654+
message: 'CEP não encontrado na base dos Correios.',
655655
service: 'correios-alt'
656656
},
657657
{
@@ -697,7 +697,7 @@ describe('[unit] cep-promise for node', () => {
697697
)
698698

699699
nock('https://buscacepinter.correios.com.br')
700-
.get('/app/cep/carrega-cep.php?cep=05010000')
700+
.post('/app/cep/carrega-cep.php')
701701
.reply(400, '<h2>Bad Request (400)</h2>')
702702

703703
nock('https://viacep.com.br')
@@ -757,7 +757,7 @@ describe('[unit] cep-promise for node', () => {
757757
)
758758

759759
nock('https://buscacepinter.correios.com.br')
760-
.get('/app/cep/carrega-cep.php?cep=05010000')
760+
.post('/app/cep/carrega-cep.php')
761761
.reply(200, {erro:true})
762762

763763
nock('https://viacep.com.br')
@@ -816,7 +816,7 @@ describe('[unit] cep-promise for node', () => {
816816
)
817817

818818
nock('https://buscacepinter.correios.com.br')
819-
.get('/app/cep/carrega-cep.php?cep=05010000')
819+
.post('/app/cep/carrega-cep.php')
820820
.reply(200, {erro:true})
821821

822822
nock('https://viacep.com.br')

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import path from 'path'
77

88
import cep from '../../src/cep-promise.js'
99
import CepPromiseError from '../../src/errors/cep-promise.js'
10+
import { getAvailableServices } from '../../src/services/index.js'
1011

1112
chai.use(chaiSubset)
1213

@@ -99,6 +100,8 @@ describe('when invoked with providers parameter', () => {
99100

100101
describe('and the providers param is a invalid array', () => {
101102
it('should reject with "validation_error"', () => {
103+
const availableProviders = Object.keys(getAvailableServices())
104+
102105
return cep('05010000', { providers: [123, 'viacep'] }).catch(error => {
103106
return expect(error)
104107
.to.be.an.instanceOf(CepPromiseError)
@@ -108,7 +111,7 @@ describe('when invoked with providers parameter', () => {
108111
errors: [
109112
{
110113
message:
111-
'O provider "123" é inválido. Os providers disponíveis são: ["brasilapi", "correios", "correios-alt", "viacep", "widenet"].',
114+
`O provider "123" é inválido. Os providers disponíveis são: ["${availableProviders.join('", "')}"].`,
112115
service: 'providers_validation'
113116
}
114117
]
@@ -127,7 +130,7 @@ describe('when invoked with providers parameter', () => {
127130
)
128131

129132
const correiosAltMock = nock('https://buscacepinter.correios.com.br')
130-
.get('/app/cep/carrega-cep.php?cep=05010000')
133+
.post('/app/cep/carrega-cep.php')
131134
.replyWithFile(
132135
200,
133136
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
@@ -176,7 +179,7 @@ describe('when invoked with providers parameter', () => {
176179
)
177180

178181
const correiosAltMock = nock('https://buscacepinter.correios.com.br')
179-
.get('/app/cep/carrega-cep.php?cep=05010000')
182+
.post('/app/cep/carrega-cep.php')
180183
.replyWithFile(
181184
200,
182185
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
@@ -225,7 +228,7 @@ describe('when invoked with providers parameter', () => {
225228
)
226229

227230
const correiosAltMock = nock('https://buscacepinter.correios.com.br')
228-
.get('/app/cep/carrega-cep.php?cep=05010000')
231+
.post('/app/cep/carrega-cep.php')
229232
.replyWithFile(
230233
200,
231234
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
@@ -274,7 +277,7 @@ describe('when invoked with providers parameter', () => {
274277
)
275278

276279
const correiosAltMock = nock('https://buscacepinter.correios.com.br')
277-
.get('/app/cep/carrega-cep.php?cep=05010000')
280+
.post('/app/cep/carrega-cep.php')
278281
.replyWithFile(
279282
200,
280283
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
@@ -323,7 +326,7 @@ describe('when invoked with providers parameter', () => {
323326
)
324327

325328
const correiosAltMock = nock('https://buscacepinter.correios.com.br')
326-
.get('/app/cep/carrega-cep.php?cep=05010000')
329+
.post('/app/cep/carrega-cep.php')
327330
.replyWithFile(
328331
200,
329332
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
@@ -380,7 +383,7 @@ describe('when invoked with providers parameter', () => {
380383
)
381384

382385
const correiosAltMock = nock('https://buscacepinter.correios.com.br')
383-
.get('/app/cep/carrega-cep.php?cep=05010000')
386+
.post('/app/cep/carrega-cep.php')
384387
.replyWithFile(
385388
200,
386389
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')
@@ -429,7 +432,7 @@ describe('when invoked with providers parameter', () => {
429432
)
430433

431434
const correiosAltMock = nock('https://buscacepinter.correios.com.br')
432-
.get('/app/cep/carrega-cep.php?cep=05010000')
435+
.post('/app/cep/carrega-cep.php')
433436
.replyWithFile(
434437
200,
435438
path.join(__dirname, '/fixtures/correios-alt-cep-05010000-found.json')

0 commit comments

Comments
 (0)