Skip to content

Commit 1c3c011

Browse files
committed
Initial import
0 parents  commit 1c3c011

File tree

7 files changed

+145
-0
lines changed

7 files changed

+145
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
tmp
3+
.DS_Store
4+

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Hello
2+
3+
An example [Hapi.js](https://hapijs.com/) app with https://github.com/hapijs/lab tests. Solution to Exercise 1 from [Exercises for Programmers](http://pragprog.com/titles/bhwb), copyright 2015 The Pragmatic Programmers.
4+
5+
## Usage
6+
7+
Run with
8+
9+
`npm start`
10+
11+
to fire the server. VIsit <http://localhost:3000/hello/Homer> to see the result
12+
13+
Replace `homer` with your name to echo the response. Use `Homer%20Simpson` to test that URL decoding for spaces works
14+
15+
## Running tests
16+
17+
Run tests with `npm test`
18+
19+
This will run tests, coverage, and a linter using the default linting settings.
20+
21+
## License
22+
23+
MIT
24+
25+
Copyright (c) 2016 Brian P. Hogan
26+
27+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
28+
29+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
30+
31+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

app.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
const Hapi = require('hapi');
3+
const Server = new Hapi.Server();
4+
const Hello = require('./lib/hello');
5+
6+
Server.connection({ port: 3000 });
7+
8+
Server.route({
9+
method: 'GET',
10+
path: '/hello/{user}',
11+
handler: function (request, reply) {
12+
13+
const result = Hello(decodeURIComponent(request.params.user));
14+
reply(result);
15+
}
16+
});
17+
18+
// don't start server if this file was required
19+
20+
if (!module.parent) {
21+
22+
Server.start((err) => {
23+
24+
if (err) {
25+
throw err;
26+
}
27+
console.log(`Server running at: ${Server.info.uri}`);
28+
});
29+
}
30+
31+
module.exports = Server;

lib/hello.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
module.exports = (name) => {
3+
4+
return 'Hello, ' + name + '!';
5+
};
6+

package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "hello",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "app.js",
6+
"scripts": {
7+
"start": "node app.js",
8+
"test": "NODE_ENV=test node node_modules/lab/bin/lab -v -L -C -D -t 85"
9+
},
10+
"author": "Brian Hogan",
11+
"license": "MIT",
12+
"dependencies": {
13+
"hapi": "^16.4.1"
14+
},
15+
"devDependencies": {
16+
"code": "^4.0.0",
17+
"lab": "^13.1.0"
18+
}
19+
}

test/integration/app_test.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
const Code = require('code');
3+
const Lab = require('lab');
4+
const lab = exports.lab = Lab.script();
5+
const Server = require('../../app.js');
6+
7+
lab.experiment('Basic HTTP Tests', () => {
8+
9+
lab.test('Greets /hello/homer} ', (done) => {
10+
11+
const options = {
12+
method: 'GET',
13+
url: '/hello/Homer'
14+
};
15+
16+
Server.inject(options, ( response ) => {
17+
18+
Code.expect(response.statusCode).to.equal(200);
19+
Code.expect(response.result).to.equal('Hello, Homer!');
20+
done();
21+
});
22+
});
23+
24+
lab.test('Greets /hello/Homer%20Simpson} ', (done) => {
25+
26+
const options = {
27+
method: 'GET',
28+
url: '/hello/Homer%20Simpson'
29+
};
30+
31+
Server.inject(options, ( response ) => {
32+
33+
Code.expect(response.statusCode).to.equal(200);
34+
Code.expect(response.result).to.equal('Hello, Homer Simpson!');
35+
done();
36+
});
37+
});
38+
39+
});

test/unit/hello_test.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
const Code = require('code');
3+
const Lab = require('lab');
4+
const lab = exports.lab = Lab.script();
5+
const Hello = require('../../lib/hello.js');
6+
7+
lab.experiment('test greetings', () => {
8+
9+
lab.test('greets with name ', (done) => {
10+
11+
Code.expect(Hello('Homer')).to.equal('Hello, Homer!');
12+
done();
13+
});
14+
15+
});

0 commit comments

Comments
 (0)