SHIMODA Hiroshi
null+****@clear*****
Wed Aug 15 16:25:53 JST 2012
SHIMODA Hiroshi 2012-08-15 16:25:53 +0900 (Wed, 15 Aug 2012) New Revision: 48d764550b47a16809719a8a49a63bedfaa7b330 https://github.com/groonga/gcs/commit/48d764550b47a16809719a8a49a63bedfaa7b330 Log: Detect base domain for endpoint automatically Modified files: lib/api/2011-02-01/configuration.js lib/server.js test/api-configuration.test.js test/test-utils.js Modified: lib/api/2011-02-01/configuration.js (+14 -7) =================================================================== --- lib/api/2011-02-01/configuration.js 2012-08-15 16:55:47 +0900 (91f87d4) +++ lib/api/2011-02-01/configuration.js 2012-08-15 16:25:53 +0900 (40999b1) @@ -24,16 +24,23 @@ function createCommonErrorResponse(errorCode, error) { var handlers = Object.create(null); +var defaultHttpPort = 80; +var defaultBaseHost = '127.0.0.1.xip.io'; + +function getBaseHostAndPort(config, request) { + var host = config.baseHost || + request.headers.http_x_forwarded_host || + request.headers.host || + defaultBaseHost + ':' + config.port; + var port = defaultHttpPort; -function getBaseHostAndPort(config) { - var host = config.baseHost; - var port = config.port; var portMatching = host.match(/:(\d+)$/); if (portMatching) { host = host.replace(portMatching[0], ''); port = parseInt(portMatching[1]); } - if (port == 80) + + if (port == defaultHttpPort) return host; else return host + ':' + port; @@ -89,7 +96,7 @@ handlers.CreateDomain = function(context, request, response, config) { response.contentType('application/xml'); response.send(createCreateDomainResponse({ domain: domain, - hostAndPort: getBaseHostAndPort(config), + hostAndPort: getBaseHostAndPort(config, request), created: true })); } catch (error) { @@ -119,7 +126,7 @@ handlers.DeleteDomain = function(context, request, response, config) { response.contentType('application/xml'); response.send(createDeleteDomainResponse({ domain: domain, - hostAndPort: getBaseHostAndPort(config), + hostAndPort: getBaseHostAndPort(config, request), deleted: true })); } catch (error) { @@ -172,7 +179,7 @@ handlers.DescribeDomains = function(context, request, response, config) { response.contentType('application/xml'); response.send(createDescribeDomainsResponse({ domains: domains, - hostAndPort: getBaseHostAndPort(config) + hostAndPort: getBaseHostAndPort(config, request) })); } catch (error) { var body = createCommonErrorResponse('InternalFailure', Modified: lib/server.js (+0 -3) =================================================================== --- lib/server.js 2012-08-15 16:55:47 +0900 (073f061) +++ lib/server.js 2012-08-15 16:25:53 +0900 (a61a341) @@ -8,9 +8,6 @@ exports.createServer = function (config) { if (!config.databasePath) config.databasePath = CLI.databasePath; if (!config.privilege) config.privilege = CLI.privilege; if (!config.port) config.port = CLI.port; - if (!config.baseHost) config.baseHost = CLI.defaultBaseHost; - if (!config.configurationHost) - config.configurationHost = CLI.defaultConfigurationHost; var context = config.context || new nroonga.Context(config.databasePath); var application = express.createServer(); Modified: test/api-configuration.test.js (+115 -0) =================================================================== --- test/api-configuration.test.js 2012-08-15 16:55:47 +0900 (6d0b17e) +++ test/api-configuration.test.js 2012-08-15 16:25:53 +0900 (35bfdde) @@ -359,6 +359,121 @@ suite('Configuration API', function() { }); }); + suite('auto detection of the base hostname and port', function() { + var temporaryDatabase; + var context; + var server; + + setup(function() { + temporaryDatabase = utils.createTemporaryDatabase(); + context = temporaryDatabase.get(); + server = utils.setupServer(context); + }); + + teardown(function() { + server.close(); + temporaryDatabase.teardown(); + temporaryDatabase = undefined; + }); + + function assertBaseHost(baseHost, response) { + response = toParsedResponse(response); + assert.deepEqual(response.pattern, + { statusCode: 200, + body: PATTERN_CreateDomainResponse }); + var domain = new Domain('companies', context); + var status = response.body.CreateDomainResponse.CreateDomainResult.DomainStatus; + assert.deepEqual( + { documentsEndpoint: status.DocService.Endpoint, + searchEndpoint: status.SearchService.Endpoint }, + { documentsEndpoint: domain.getDocumentsEndpoint(baseHost), + searchEndpoint: domain.getSearchEndpoint(baseHost) } + ); + } + + test('specified by server option', function(done) { + var baseHost = 'by.server.option'; + server.close(); + server = utils.setupServer(context, { baseHost: baseHost }); + utils + .get('/?DomainName=companies&Action=CreateDomain&Version=2011-02-01') + .next(function(response) { + assertBaseHost(baseHost, response); + done(); + }) + .error(function(error) { + done(error); + }); + }); + + test('specified by Host header', function(done) { + var baseHost = 'by.host.header'; + utils + .get('/?DomainName=companies&Action=CreateDomain&Version=2011-02-01', { + 'Host': baseHost + }) + .next(function(response) { + assertBaseHost(baseHost, response); + done(); + }) + .error(function(error) { + done(error); + }); + }); + + test('specified by HTTP_X_FORWARDED_HOST header', function(done) { + var baseHost = 'by.forwarded.host.header'; + utils + .get('/?DomainName=companies&Action=CreateDomain&Version=2011-02-01', { + 'HTTP_X_FORWARDED_HOST': baseHost + }) + .next(function(response) { + assertBaseHost(baseHost, response); + done(); + }) + .error(function(error) { + done(error); + }); + }); + + test('HTTP_X_FORWARDED_HOST and Host header', function(done) { + var baseHost = 'by.host.header'; + var baseHostForwarded = 'by.forwarded.host.header'; + utils + .get('/?DomainName=companies&Action=CreateDomain&Version=2011-02-01', { + 'Host': baseHost, + 'HTTP_X_FORWARDED_HOST': baseHostForwarded + }) + .next(function(response) { + assertBaseHost(baseHostForwarded, response); + done(); + }) + .error(function(error) { + done(error); + }); + }); + + test('HTTP_X_FORWARDED_HOST, Host header, and server option', function(done) { + var baseHostByOption = 'by.server.option'; + server.close(); + server = utils.setupServer(context, { baseHost: baseHostByOption }); + var baseHost = 'by.host.header'; + var baseHostForwarded = 'by.forwarded.host.header'; + utils + .get('/?DomainName=companies&Action=CreateDomain&Version=2011-02-01', { + 'Host': baseHost, + 'HTTP_X_FORWARDED_HOST': baseHostForwarded + }) + .next(function(response) { + assertBaseHost(baseHostByOption, response); + done(); + }) + .error(function(error) { + done(error); + }); + }); + }); + test('Get, Action=DeleteDomain', function(done) { var domain; utils Modified: test/test-utils.js (+8 -7) =================================================================== --- test/test-utils.js 2012-08-15 16:55:47 +0900 (57220f3) +++ test/test-utils.js 2012-08-15 16:25:53 +0900 (a75993d) @@ -18,13 +18,14 @@ exports.testBaseHost = testBaseHost; exports.testConfigurationHost = testConfigurationHost; exports.testPort = testPort; -function setupServer(context) { - var server = gcsServer.createServer({ - context: context, - port: testPort, - baseHost: exports.testBaseHost + ':80', - configurationHost: exports.testConfigurationHost + ':80' - }); +function setupServer(context, extraOptions) { + var options = { context: context, + port: testPort }; + if (extraOptions) + Object.keys(extraOptions).forEach(function(key) { + options[key] = extraOptions[key]; + }); + var server = gcsServer.createServer(options); server.listen(testPort); return server; } -------------- next part -------------- HTML����������������������������... 下載