Skip to content

Commit 2329869

Browse files
authored
Merge pull request #345 from pactumjs/bugfix/344-auth-override-issue-in-request
Fix withCore overriding request core options when using withAuth
2 parents 47dbb61 + 935ce72 commit 2329869

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

src/models/Spec.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fs = require('fs');
22
const lc = require('lightcookie');
3-
3+
const override = require('deep-override');
44
const path = require('path');
55
const Tosser = require('./Tosser');
66
const Expect = require('./expect');
@@ -284,7 +284,8 @@ class Spec {
284284
}
285285

286286
withCore(options) {
287-
this._request.core = options;
287+
this._request.core = this._request.core || {};
288+
override(this._request.core, options);
288289
return this;
289290
}
290291

test/component/interactions.spec.js

+73
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,79 @@ describe('Mock', () => {
604604
.expectStatus(200);
605605
});
606606

607+
it('GET - with core options & auth - override with auth', async () => {
608+
await pactum.spec()
609+
.useInteraction({
610+
request: {
611+
method: 'GET',
612+
path: '/api/core',
613+
headers: {
614+
'authorization': 'Basic dXNlcjpwYXNz'
615+
}
616+
},
617+
response: {
618+
status: 200
619+
}
620+
})
621+
.get('http://localhost:9393')
622+
.withCore({
623+
path: '/api/core',
624+
auth: 'user:invalid-pass'
625+
})
626+
.withAuth('user', 'pass')
627+
.expectStatus(200);
628+
});
629+
630+
it('GET - with core options & auth - override with core', async () => {
631+
await pactum.spec()
632+
.useInteraction({
633+
request: {
634+
method: 'GET',
635+
path: '/api/core',
636+
headers: {
637+
'authorization': 'Basic dXNlcjpwYXNz'
638+
}
639+
},
640+
response: {
641+
status: 200
642+
}
643+
})
644+
.get('http://localhost:9393')
645+
.withAuth('user', 'invalid-pass')
646+
.withCore({
647+
path: '/api/core',
648+
auth: 'user:pass'
649+
})
650+
.expectStatus(200);
651+
});
652+
653+
it('GET - with core options & auth - override with core invalid auth', async () => {
654+
try {
655+
await pactum.spec()
656+
.useInteraction({
657+
request: {
658+
method: 'GET',
659+
path: '/api/core',
660+
headers: {
661+
'authorization': 'Basic dXNlcjpwYXNz'
662+
}
663+
},
664+
response: {
665+
status: 200
666+
}
667+
})
668+
.get('http://localhost:9393')
669+
.withAuth('user', 'pass')
670+
.withCore({
671+
path: '/api/core',
672+
auth: 'user:invalid-pass'
673+
})
674+
} catch (error) {
675+
err = error
676+
}
677+
expect(err.message).contains('Interaction not exercised: GET - /api/core');
678+
});
679+
607680
it('GET - with auth', async () => {
608681
await pactum.spec()
609682
.useInteraction({

0 commit comments

Comments
 (0)