Last active
April 5, 2018 10:59
-
-
Save dominykas/bfdfea6d10c1bc56b2c1fffc3074d70c to your computer and use it in GitHub Desktop.
hapi/nes empty response
This file contains hidden or 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
GET /null | |
nes null | |
wreck + json null | |
GET /null-with-json-header | |
nes null | |
wreck + json null | |
GET /null-with-text-header | |
nes null | |
wreck + json null | |
GET /empty-string | |
nes "" | |
wreck + json null | |
GET /empty-with-text-header | |
nes "" | |
wreck + json null | |
GET /204 | |
nes null | |
wreck + json null |
This file contains hidden or 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
'use strict'; | |
const Hapi = require('hapi'); | |
const Nes = require('nes'); | |
const internals = {}; | |
module.exports.start = async () => { | |
const server = new Hapi.Server(); | |
await server.register([Nes]); | |
server.route({ | |
method: 'GET', | |
path: '/null', | |
config: { | |
handler: (request, h) => { | |
return null; | |
} | |
} | |
}); | |
server.route({ | |
method: 'GET', | |
path: '/null-with-json-header', | |
config: { | |
handler: (request, h) => { | |
return h.response(null) | |
.type('application/json'); | |
} | |
} | |
}); | |
server.route({ | |
method: 'GET', | |
path: '/null-with-text-header', | |
config: { | |
handler: (request, h) => { | |
return h.response(null) | |
.type('text/plain'); | |
} | |
} | |
}); | |
server.route({ | |
method: 'GET', | |
path: '/empty-string', | |
config: { | |
handler: (request, h) => { | |
return ''; | |
} | |
} | |
}); | |
server.route({ | |
method: 'GET', | |
path: '/empty-with-json-header', | |
config: { | |
handler: (request, h) => { | |
return h.response('') | |
.type('application/json'); | |
} | |
} | |
}); | |
server.route({ | |
method: 'GET', | |
path: '/empty-with-text-header', | |
config: { | |
handler: (request, h) => { | |
return h.response('') | |
.type('text/plain'); | |
} | |
} | |
}); | |
server.route({ | |
method: 'GET', | |
path: '/204', | |
config: { | |
handler: (request, h) => { | |
return h.response().code(204); | |
} | |
} | |
}); | |
await server.start(); | |
return server; | |
}; |
This file contains hidden or 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
'use strict'; | |
const FakeServer = require('./server'); | |
const Client = require('nes').Client; | |
const Wreck = require('wreck'); | |
const internals = {}; | |
internals.test = async (client, baseUrl, path) => { | |
const [overNes, overWreckJson, overWreck] = await Promise.all([ | |
client.request({ | |
method: 'GET', | |
path | |
}), | |
Wreck.get(baseUrl + path, { json: true }), | |
Wreck.get(baseUrl + path) | |
]); | |
console.log(`GET ${path}`); | |
console.log(`nes`, JSON.stringify(overNes.payload)); | |
console.log(`wreck + json`, JSON.stringify(overWreckJson.payload)); | |
// console.log(`wreck`, JSON.stringify(overWreck.payload.toString())); | |
console.log(); | |
}; | |
internals.run = async () => { | |
const server = await FakeServer.start(); | |
const baseUrl = server.info.uri; | |
const client = new Client(baseUrl); | |
console.log(`connecting ${baseUrl}`); | |
await client.connect(); | |
await internals.test(client, baseUrl, '/null'); | |
await internals.test(client, baseUrl, '/null-with-json-header'); | |
await internals.test(client, baseUrl, '/null-with-text-header'); | |
await internals.test(client, baseUrl, '/empty-string'); | |
// await internals.test(client, baseUrl, '/empty-with-json-header'); | |
await internals.test(client, baseUrl, '/empty-with-text-header'); | |
await internals.test(client, baseUrl, '/204'); | |
console.log('disconnecting'); | |
await client.disconnect(); | |
await server.stop(); | |
}; | |
internals.run() | |
.then(() => { | |
console.log('done'); | |
}) | |
.catch((err) => { | |
console.error('failed', err); | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment