-
Notifications
You must be signed in to change notification settings - Fork 820
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support https for mock util plugin (#13700)
* feat: allow secure connection for local testing feat: add https option to mock api * chore: lockfile * chore: extract api * fix: resolve test errors and add a new test * fix: api interfaces * fix: improvements on https configuration * fix: lint
- Loading branch information
Showing
11 changed files
with
150 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/amplify-util-mock/src/__tests__/get-https-config.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { getHttpsConfig } from '../utils/get-https-config'; | ||
|
||
describe('getHttpsConfig', () => { | ||
let context; | ||
|
||
beforeEach(() => { | ||
context = { | ||
input: { | ||
argv: [], | ||
}, | ||
print: { | ||
error: jest.fn(), | ||
}, | ||
}; | ||
}); | ||
|
||
it('returns paths when --https option is followed by key and cert paths', () => { | ||
context.input.argv = ['--https', '/path/to/key', '/path/to/cert']; | ||
|
||
const config = getHttpsConfig(context); | ||
|
||
expect(config).toEqual({ | ||
sslKeyPath: '/path/to/key', | ||
sslCertPath: '/path/to/cert', | ||
}); | ||
}); | ||
|
||
it('returns null and prints error when --https option is not followed by key and cert paths', () => { | ||
context.input.argv = ['--https']; | ||
|
||
const config = getHttpsConfig(context); | ||
|
||
expect(config).toEqual(null); | ||
expect(context.print.error).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export function getHttpsConfig(context): { sslKeyPath: string; sslCertPath: string } | null { | ||
if (!context.input || !context.input.argv) { | ||
return null; | ||
} | ||
|
||
const argv = context.input.argv; | ||
const httpsIndex = argv.indexOf('--https'); | ||
|
||
if (httpsIndex !== -1) { | ||
if (httpsIndex < argv.length - 2) { | ||
const keyPath = argv[httpsIndex + 1]; | ||
const certPath = argv[httpsIndex + 2]; | ||
if (typeof keyPath === 'string' && typeof certPath === 'string') { | ||
return { sslKeyPath: keyPath, sslCertPath: certPath }; | ||
} else { | ||
context.print.error('\nThe provided paths for the SSL key and certificate are not valid.\n'); | ||
context.print.error('Please ensure you have entered the correct paths.\n'); | ||
} | ||
} else { | ||
context.print.error('\nThe --https option must be followed by the path to the SSL key and the path to the SSL certificate.\n'); | ||
context.print.error('Example: amplify mock api --https /path/to/key /path/to/cert\n'); | ||
context.print.error('In order to generate a key and certificate, you can use openssl:\n'); | ||
context.print.error('openssl req -nodes -new -x509 -keyout server.key -out server.cert\n'); | ||
context.print.error('Then, run the command again with the paths to the generated key and certificate.\n'); | ||
} | ||
} | ||
|
||
return null; | ||
} |