Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"name": "simple",
"version": "0.1.0",
"dependencies": {},
"dependencies": {
"https": "^1.0.0",
"express": "^4.17.1",},
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-uglify": "~0.2.7",
Expand Down
4 changes: 4 additions & 0 deletions src/client/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mounted() {
if (window.location.protocol === 'http:') {
window.location.href = window.location.href.replace('http:', 'https:');
}
3 changes: 3 additions & 0 deletions src/server/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const serverConfig = {
sslCertPath: '/path/to/ssl/cert.pem',
sslKeyPath: '/path/to/ssl/key.pem',
5 changes: 5 additions & 0 deletions src/server/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const options = {
cert: fs.readFileSync(serverConfig.sslCertPath),
key: fs.readFileSync(serverConfig.sslKeyPath)
};
const server = https.createServer(options, app).listen(serverConfig.port, () => {
28 changes: 28 additions & 0 deletions tests/AppMounted.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { shallowMount } from '@vue/test-utils';
import App from '../src/client/App.vue';

describe('App Mounted', () => {
let originalLocation;

beforeAll(() => {
originalLocation = window.location;
delete window.location;
window.location = { href: 'http://example.com', protocol: 'http:' };
});

afterAll(() => {
window.location = originalLocation;
});

test('should redirect to HTTPS if protocol is HTTP', () => {
shallowMount(App);
expect(window.location.href).toBe('https://example.com');
});

test('should not redirect if protocol is already HTTPS', () => {
window.location.protocol = 'https:';
window.location.href = 'https://example.com';
shallowMount(App);
expect(window.location.href).toBe('https://example.com');
});
});
18 changes: 18 additions & 0 deletions tests/config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const serverConfig = require('../src/server/config');

describe('Server Configuration', () => {
test('should have sslCertPath and sslKeyPath defined', () => {
expect(serverConfig.sslCertPath).toBe('/path/to/ssl/cert.pem');
expect(serverConfig.sslKeyPath).toBe('/path/to/ssl/key.pem');
});

test('should throw error if sslCertPath or sslKeyPath is missing', () => {
const originalConfig = { ...serverConfig };
delete serverConfig.sslCertPath;
expect(() => require('../src/server/config')).toThrow();
serverConfig.sslCertPath = originalConfig.sslCertPath;
delete serverConfig.sslKeyPath;
expect(() => require('../src/server/config')).toThrow();
serverConfig.sslKeyPath = originalConfig.sslKeyPath;
});
});
30 changes: 30 additions & 0 deletions tests/server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const fs = require('fs');
const https = require('https');
const app = require('../src/server/app');
const serverConfig = require('../src/server/config');

jest.mock('fs');

describe('HTTPS Server Setup', () => {
beforeEach(() => {
fs.readFileSync.mockImplementation((path) => {
if (path === serverConfig.sslCertPath) return 'fake-cert';
if (path === serverConfig.sslKeyPath) return 'fake-key';
throw new Error('File not found');
});
});

test('should create an HTTPS server with correct options', () => {
const createServerSpy = jest.spyOn(https, 'createServer');
require('../src/server/server');
expect(createServerSpy).toHaveBeenCalledWith(expect.objectContaining({
cert: 'fake-cert',
key: 'fake-key'
}), app);
});

test('should throw error if SSL files are not found', () => {
fs.readFileSync.mockImplementation(() => { throw new Error('File not found'); });
expect(() => require('../src/server/server')).toThrow('File not found');
});
});