How to properly install and test the Hapi domain and subdomain?

I am currently updating a small library that I created to parse the locale from the request object. It currently works with Express and Koa, but I'm trying to get it to work with Hapi.

For tests, I use the Mocha and Hapi method inject, as described in their docs. I also looked in my docs for setting up the properties server , but I did not find an example of setting up a domain, such as locahost.enor even subdomains, such as en.localhost.com.

I currently have my test setup installed:

var server = new Hapi.Server();
server.connection({
    //is this correct?
    uri:'localhost.en:3000',
    port: 3000
});

var handler = function(request, reply) {
    //return the parsed locale {String}
    return reply(accept(request, {
           supported: ['en']
    }).getFromDomain());
 };

server.route({
    method: 'GET',
    path: '/',
    handler: handler
});
server.start(function() {});

where injectset as follows:

server.inject({
    method: 'GET',
    url: '/',
    headers: {
        'Accept-Language': 'ja',
        'Set-Cookie': 'mycookie=test'
     }}, function(res) {
        assert.strictEqual(res.result, 'en');
        done();
});

? , ? , / request? , hostname, ?

+4
1

Hapi ( host). , request.headers.host.

(), request.info.hostname

server.route({
    method: 'GET',
    path: '/',
    handler: function(request, reply) {

        var hostname = request.info.hostname;    

        reply('Ok');
     }
});

, ( ), vhost.

server.route({
    method: 'GET',
    path: '/',
    vhost: ['en.example.com'],
    handler: function(request, reply) {

        reply('Ok');
     }
});

vhosts server.inject,

server.inject({
    method: 'GET',
    url: '/',
    headers: {
        'Set-Cookie': 'mycookie=test',
     }}, function(res) {

        Assert(res.statusCode === 404);    // 404 because not en.example.com
});

server.inject({
    method: 'GET',
    url: '/',
    headers: {
        'Set-Cookie': 'mycookie=test',
        'Host': 'en.example.com'
     }}, function(res) {

        Assert(res.statusCode === 200);    // 200 because en.example.com
});
+8

All Articles