Skip to content

Commit

Permalink
Add cors headers by default during OpenAPI import (mockoon#1649)
Browse files Browse the repository at this point in the history
This is a follow up from mockoon#1392
Closes mockoon#1593
  • Loading branch information
255kb authored Jan 17, 2025
1 parent 91515a9 commit 1c9dacd
Show file tree
Hide file tree
Showing 18 changed files with 235 additions and 25 deletions.
15 changes: 14 additions & 1 deletion packages/cli/test/data/envs/petstore-imported.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
6 changes: 4 additions & 2 deletions packages/commons-server/src/libs/openapi-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ export class OpenAPIConverter {
port?: number
): Environment {
const newEnvironment = BuildEnvironment({
hasDefaultHeader: false,
hasContentTypeHeader: false,
hasCorsHeaders: true,
hasDefaultRoute: false,
port
});
Expand Down Expand Up @@ -224,7 +225,8 @@ export class OpenAPIConverter {
port?: number
): Environment {
const newEnvironment = BuildEnvironment({
hasDefaultHeader: false,
hasContentTypeHeader: false,
hasCorsHeaders: true,
hasDefaultRoute: false,
port
});
Expand Down
20 changes: 15 additions & 5 deletions packages/commons/src/libs/schema-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,22 +212,32 @@ export const BuildCallback = (): Callback => ({ ...CallbackDefault });
export const BuildEnvironment = (
params: {
hasDefaultRoute: boolean;
hasDefaultHeader: boolean;
hasContentTypeHeader: boolean;
hasCorsHeaders: boolean;
port?: number;
} = {
hasDefaultRoute: true,
hasDefaultHeader: true
hasContentTypeHeader: true,
hasCorsHeaders: true
}
): Environment => {
const newRoute = BuildHTTPRoute();

const headers: Header[] = [];

if (params.hasContentTypeHeader) {
headers.push(BuildHeader('Content-Type', 'application/json'));
}

if (params.hasCorsHeaders) {
headers.push(...CORSHeaders);
}

return {
...EnvironmentDefault,
port: params.port ?? EnvironmentDefault.port,
routes: params.hasDefaultRoute ? [newRoute] : [],
headers: params.hasDefaultHeader
? [BuildHeader('Content-Type', 'application/json'), ...CORSHeaders]
: [],
headers,
proxyReqHeaders: [BuildHeader()],
proxyResHeaders: [BuildHeader()],
rootChildren: params.hasDefaultRoute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,8 @@ export class EnvironmentsService extends Logger {
const newEnvironment = options.environment
? options.environment
: BuildEnvironment({
hasDefaultHeader: true,
hasContentTypeHeader: true,
hasCorsHeaders: true,
hasDefaultRoute: true,
port: this.dataService.getNewEnvironmentPort()
});
Expand Down Expand Up @@ -719,7 +720,8 @@ export class EnvironmentsService extends Logger {
if (!environment) {
environment = {
...BuildEnvironment({
hasDefaultHeader: true,
hasContentTypeHeader: true,
hasCorsHeaders: true,
hasDefaultRoute: true
}),
// provide a name or the filename (UUID) will be used
Expand Down Expand Up @@ -1267,7 +1269,8 @@ export class EnvironmentsService extends Logger {
setActive: true,
environment: {
...BuildEnvironment({
hasDefaultHeader: true,
hasContentTypeHeader: true,
hasCorsHeaders: true,
hasDefaultRoute: true,
port: this.dataService.getNewEnvironmentPort()
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6736,7 +6736,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4336,7 +4336,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25928,7 +25928,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10568,7 +10568,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12632,7 +12632,20 @@
"passphrase": ""
},
"cors": true,
"headers": [],
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
}
],
"proxyReqHeaders": [{ "key": "", "value": "" }],
"proxyResHeaders": [{ "key": "", "value": "" }],
"data": [],
Expand Down
Loading

0 comments on commit 1c9dacd

Please sign in to comment.