Last active
April 12, 2018 15:21
-
-
Save sethlopezme/17e75a7bd69368bd774e23577db8997d to your computer and use it in GitHub Desktop.
Complete files for my article, Testing Hapi.js APIs. https://sethlopez.me/article/testing-hapi-js-apis
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
{ | |
"name": "testing-hapi-js-apis", | |
"version": "1.0.0", | |
"license": "MIT", | |
"scripts": { | |
"test": "ava --verbose test.js" | |
}, | |
"dependencies": { | |
"ava": "^0.16.0", | |
"hapi": "^15.1.1" | |
} | |
} |
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
const Hapi = require('hapi'); | |
const server = new Hapi.Server(); | |
server.connection({ port: 8080 }); | |
server.route({ | |
method: 'POST', | |
path: '/subscribe', | |
handler: (request, reply) => { | |
if (!request.payload.email) { | |
return reply({ | |
result: 'failure', | |
message: 'Email address is required.' | |
}).code(400); | |
} else if (!/@/.test(request.payload.email)) { | |
return reply({ | |
result: 'failure', | |
message: 'Invalid email address.' | |
}).code(400); | |
} | |
// persist the email address | |
// ... | |
return reply({ | |
result: 'success', | |
message: 'Subscription successful.' | |
}); | |
} | |
}); | |
if (!module.parent) { | |
server.start(error => { | |
process.exit(1); | |
}); | |
} | |
module.exports = server; |
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
import test from 'ava'; | |
import server from './server'; | |
const requestDefaults = { | |
method: 'POST', | |
url: '/subscribe', | |
payload: {} | |
}; | |
test('endpoint test | POST /subscribe | empty payload -> 400 Bad Request', t => { | |
const request = Object.assign({}, requestDefaults); | |
return server.inject(request) | |
.then(response => { | |
t.is(response.statusCode, 400, 'status code is 400'); | |
}); | |
}); | |
test('endpoint test | POST /subscribe | invalid email address -> 400 Bad Request', t => { | |
const request = Object.assign({}, requestDefaults, { | |
payload: { | |
email: 'a' | |
} | |
}); | |
return server.inject(request) | |
.then(response => { | |
t.is(response.statusCode, 400, 'status code is 400'); | |
}); | |
}); | |
test('endpoint test | POST /subscribe | valid email address -> 200 OK', t => { | |
const request = Object.assign({}, requestDefaults, { | |
payload: { | |
email: '[email protected]' | |
} | |
}); | |
return server.inject(request) | |
.then(response => { | |
t.is(response.statusCode, 200, 'status code is 200'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment