-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Reloadly/develop
Linting support
- Loading branch information
Showing
11 changed files
with
688 additions
and
592 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"rules": { | ||
"semi": ["error", "always"], | ||
"quotes": ["error", "double"] | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,3 +5,8 @@ | |
# coverage | ||
/.nyc_output | ||
/coverage | ||
|
||
src | ||
tsconfig.json | ||
.eslintrc | ||
.prettierrc |
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,5 @@ | ||
{ | ||
"printWidth": 80, | ||
"trailingComma": "all", | ||
"singleQuote": true | ||
} |
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 |
---|---|---|
@@ -1,43 +1,41 @@ | ||
import {HttpFirewallOptions} from "../types"; | ||
import {StrictHttpFirewall} from "../strict.http.firewall"; | ||
import express from "express"; | ||
import request from "supertest"; | ||
import { HttpFirewallOptions } from '../types'; | ||
import { StrictHttpFirewall } from '../strict.http.firewall'; | ||
import express from 'express'; | ||
import request from 'supertest'; | ||
|
||
describe('HttpStrictFirewall test suite', () => { | ||
describe('Integration Tests: .firewall()', () => { | ||
test('Should reject disallowed Http method', async () => { | ||
const app = express(); | ||
const options: HttpFirewallOptions = { | ||
allowedHttpMethods: ['POST', 'GET'], | ||
}; | ||
app.use(new StrictHttpFirewall(options).firewall); | ||
const res = await request(app) | ||
.head('/') | ||
.set('Content-Type', 'application/json'); | ||
expect(res.statusCode).toBe(403); | ||
}); | ||
|
||
describe('Integration Tests: .firewall()', () => { | ||
test('Should reject disallowed Http method', async () => { | ||
const app = express(); | ||
const options: HttpFirewallOptions = { | ||
allowedHttpMethods: ['POST', 'GET'], | ||
} | ||
app.use(new StrictHttpFirewall(options).firewall) | ||
const res = await request(app) | ||
.head('/') | ||
.set('Content-Type', 'application/json') | ||
expect(res.statusCode).toBe(403); | ||
}); | ||
|
||
test('Should allow configured Http method', async () => { | ||
// Arrange | ||
const app = express(); | ||
const options: HttpFirewallOptions = { | ||
allowedHttpMethods: ['POST', 'GET'], | ||
} | ||
app.use(new StrictHttpFirewall(options).firewall) | ||
app.get('/', (req, res) => { | ||
// You're working with an express req and res now. | ||
res.status(200).send(); | ||
}); | ||
test('Should allow configured Http method', async () => { | ||
// Arrange | ||
const app = express(); | ||
const options: HttpFirewallOptions = { | ||
allowedHttpMethods: ['POST', 'GET'], | ||
}; | ||
app.use(new StrictHttpFirewall(options).firewall); | ||
app.get('/', (req, res) => { | ||
// You're working with an express req and res now. | ||
res.status(200).send(); | ||
}); | ||
|
||
// Act | ||
const res = await request(app) | ||
.get('/') | ||
.set('Content-Type', 'application/json') | ||
// Act | ||
const res = await request(app) | ||
.get('/') | ||
.set('Content-Type', 'application/json'); | ||
|
||
// Assert | ||
expect(res.statusCode).toBe(200); | ||
}); | ||
// Assert | ||
expect(res.statusCode).toBe(200); | ||
}); | ||
|
||
}); | ||
}); |
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 |
---|---|---|
@@ -1,34 +1,32 @@ | ||
import express, {Express, Request, Response} from 'express'; | ||
import {HttpFirewallOptions, Predicate} from "../types"; | ||
import {StrictHttpFirewall} from "../index"; | ||
import express, { Express, Request, Response } from 'express'; | ||
import { HttpFirewallOptions, Predicate } from '../types'; | ||
import { StrictHttpFirewall } from '../index'; | ||
|
||
const app: Express = express(); | ||
const port = 5428; | ||
|
||
// This must be added first, before adding any routes | ||
app.use(new StrictHttpFirewall(firewallOptions()).firewall) | ||
app.use(new StrictHttpFirewall(firewallOptions()).firewall); | ||
|
||
// Or, to simply use the firewall with default rules: | ||
//app.use(httpFirewall) | ||
|
||
app.get('/', (req: Request, res: Response) => { | ||
res.send('Http Firewall Demo running'); | ||
res.send('Http Firewall Demo running'); | ||
}); | ||
|
||
app.listen(port, () => { | ||
console.log(`⚡️[server]: Server is running at http://localhost:${port}`); | ||
console.log(`⚡️[server]: Server is running at http://localhost:${port}`); | ||
}); | ||
|
||
|
||
function firewallOptions(): HttpFirewallOptions { | ||
|
||
// Allows traffic from specific hosts only | ||
const allowedHostnamesPredicate = | ||
Predicate.of<string>(h => h.endsWith('example.com')).or( | ||
Predicate.of<string>(h => h === "localhost")); | ||
|
||
return { | ||
allowedHostnames: allowedHostnamesPredicate, | ||
allowedHttpMethods: ['POST', 'GET'], | ||
}; | ||
// Allows traffic from specific hosts only | ||
const allowedHostnamesPredicate = Predicate.of<string>((h) => | ||
h.endsWith('example.com'), | ||
).or(Predicate.of<string>((h) => h === 'localhost')); | ||
|
||
return { | ||
allowedHostnames: allowedHostnamesPredicate, | ||
allowedHttpMethods: ['POST', 'GET'], | ||
}; | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from './strict.http.firewall' | ||
export * from './strict.http.firewall'; |
Oops, something went wrong.