Skip to content

Commit 7e813a0

Browse files
committed
fix(execute): append new cookies to existing instead of replacing
1 parent ec05e07 commit 7e813a0

File tree

2 files changed

+116
-2
lines changed

2 files changed

+116
-2
lines changed

src/execute/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { identity } from 'ramda';
2-
import { isPlainObject } from 'ramda-adjunct';
2+
import { isPlainObject, isNonEmptyString } from 'ramda-adjunct';
33
import {
44
test as testServerURLTemplate,
55
substitute as substituteServerURLTemplate,
@@ -309,7 +309,11 @@ export function buildRequest(options) {
309309
},
310310
});
311311

312-
req.headers.Cookie = cookieString;
312+
if (isNonEmptyString(req.headers.Cookie)) {
313+
req.headers.Cookie += `; ${cookieString}`;
314+
} else {
315+
req.headers.Cookie = cookieString;
316+
}
313317
}
314318

315319
if (req.cookies) {

test/oas3/execute/authorization.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,54 @@ describe('Authorization - OpenAPI Specification 3.0', () => {
465465
});
466466
});
467467
test('should add apiKey credentials as a cookie', () => {
468+
const spec = {
469+
openapi: '3.0.0',
470+
components: {
471+
securitySchemes: {
472+
myApiKey: {
473+
type: 'apiKey',
474+
name: 'MyApiKeyCookie',
475+
in: 'cookie',
476+
},
477+
},
478+
},
479+
paths: {
480+
'/': {
481+
get: {
482+
operationId: 'myOperation',
483+
security: [
484+
{
485+
myApiKey: [],
486+
},
487+
],
488+
},
489+
},
490+
},
491+
};
492+
493+
// when
494+
const req = buildRequest({
495+
spec,
496+
operationId: 'myOperation',
497+
securities: {
498+
authorized: {
499+
myApiKey: {
500+
value: 'MyToken',
501+
},
502+
},
503+
},
504+
});
505+
506+
expect(req).toEqual({
507+
method: 'GET',
508+
url: '/',
509+
credentials: 'same-origin',
510+
headers: {
511+
Cookie: 'MyApiKeyCookie=MyToken',
512+
},
513+
});
514+
});
515+
test('should add multiple apiKey credentials as a cookie', () => {
468516
const spec = {
469517
openapi: '3.0.0',
470518
components: {
@@ -521,6 +569,68 @@ describe('Authorization - OpenAPI Specification 3.0', () => {
521569
},
522570
});
523571
});
572+
test('should append apiKey credentials to a cookie', () => {
573+
const spec = {
574+
openapi: '3.0.0',
575+
components: {
576+
securitySchemes: {
577+
myApiKey: {
578+
type: 'apiKey',
579+
name: 'MyApiKeyCookie',
580+
in: 'cookie',
581+
},
582+
},
583+
},
584+
paths: {
585+
'/': {
586+
get: {
587+
operationId: 'myOperation',
588+
parameters: [
589+
{
590+
name: 'id',
591+
in: 'cookie',
592+
style: 'form',
593+
explode: true,
594+
},
595+
],
596+
security: [
597+
{
598+
myApiKey: [],
599+
},
600+
],
601+
},
602+
},
603+
},
604+
};
605+
606+
// when
607+
const req = buildRequest({
608+
spec,
609+
operationId: 'myOperation',
610+
parameters: {
611+
id: [1, 2, 3],
612+
},
613+
securities: {
614+
authorized: {
615+
myApiKey: {
616+
value: 'MyToken',
617+
},
618+
myApiKey1: {
619+
value: 'MyToken1',
620+
},
621+
},
622+
},
623+
});
624+
625+
expect(req).toEqual({
626+
method: 'GET',
627+
url: '/',
628+
credentials: 'same-origin',
629+
headers: {
630+
Cookie: 'id=1&id=2&id=3; MyApiKeyCookie=MyToken',
631+
},
632+
});
633+
});
524634
test('should not add credentials if operation does not call for security', () => {
525635
const spec = {
526636
openapi: '3.0.0',

0 commit comments

Comments
 (0)