YUKI Hiroshi
null+****@clear*****
Thu Jan 17 15:23:58 JST 2013
YUKI Hiroshi 2013-01-17 15:23:58 +0900 (Thu, 17 Jan 2013) New Revision: 2d4e90776a09b69a219ec6a1f79828d24f1c93eb https://github.com/groonga/express-kotoumi/commit/2d4e90776a09b69a219ec6a1f79828d24f1c93eb Log: Request object has two properties: "params" for parameters bound to the path, "query" for parameters from query parameters Modified files: lib/builders/rest-request.js test/builders-rest-request.test.js Modified: lib/builders/rest-request.js (+16 -14) =================================================================== --- lib/builders/rest-request.js 2013-01-17 12:43:21 +0900 (e47d01f) +++ lib/builders/rest-request.js 2013-01-17 15:23:58 +0900 (0d0dabc) @@ -19,6 +19,8 @@ function searchRequestBuilder(request) { if (!params.tableName) throw new Error('no source is specified'); + var queryParams = request.query; + var queries = {}; queries.result = { source: params.tableName, @@ -31,20 +33,20 @@ function searchRequestBuilder(request) { } }; - if (typeof params.attributes == 'string') - queries.result.attributes = params.attributes.split(','); - if (typeof params.limit == 'string') - queries.result.limit = getIntegerValue(params, 'limit'); - if (typeof params.match_escalation_threshold == 'string') - queries.result.matchEscalationThreshold = getIntegerValue(params, 'match_escalation_threshold'); - if (typeof params.match_to == 'string') - queries.result.matchTo = params.match_to.split(','); - if (typeof params.offset == 'string') - queries.result.offset = getIntegerValue(params, 'offset'); - if (typeof params.query == 'string') - queries.result.query = params.query; - if (typeof params.sort_by == 'string') - queries.result.sortBy = params.sort_by.split(','); + if (typeof queryParams.attributes == 'string') + queries.result.attributes = queryParams.attributes.split(','); + if (typeof queryParams.limit == 'string') + queries.result.limit = getIntegerValue(queryParams, 'limit'); + if (typeof queryParams.match_escalation_threshold == 'string') + queries.result.matchEscalationThreshold = getIntegerValue(queryParams, 'match_escalation_threshold'); + if (typeof queryParams.match_to == 'string') + queries.result.matchTo = queryParams.match_to.split(','); + if (typeof queryParams.offset == 'string') + queries.result.offset = getIntegerValue(queryParams, 'offset'); + if (typeof queryParams.query == 'string') + queries.result.query = queryParams.query; + if (typeof queryParams.sort_by == 'string') + queries.result.sortBy = queryParams.sort_by.split(','); return { queries: queries }; } Modified: test/builders-rest-request.test.js (+63 -41) =================================================================== --- test/builders-rest-request.test.js 2013-01-17 12:43:21 +0900 (aad9512) +++ test/builders-rest-request.test.js 2013-01-17 15:23:58 +0900 (0c825e0) @@ -15,9 +15,13 @@ suite('building message from REST API request', function() { }; test('simple query', function() { - var params = { - tableName: 'test_table', - query: 'foobar' + var fakeRequest = { + params: { + tableName: 'test_table' + }, + query: { + query: 'foobar' + } }; var expectedBody = { queries: { @@ -28,19 +32,23 @@ suite('building message from REST API request', function() { } } }; - var actualBody = builders.search({ params: params }); + var actualBody = builders.search(fakeRequest); assert.equalJSON(actualBody, expectedBody); }); test('with options', function() { - var params = { - tableName: 'people', - query: 'foobar', - offset: '10', - limit: '100', - match_to: 'realname,nickname', - sort_by: '-realname,-nickname', - attributes: 'realname,nickname,age,job' + var fakeRequest = { + params: { + tableName: 'people' + }, + query: { + query: 'foobar', + offset: '10', + limit: '100', + match_to: 'realname,nickname', + sort_by: '-realname,-nickname', + attributes: 'realname,nickname,age,job' + } }; var expectedBody = { queries: { @@ -56,52 +64,66 @@ suite('building message from REST API request', function() { } } }; - var actualBody = builders.search({ params: params }); + var actualBody = builders.search(fakeRequest); assert.equalJSON(actualBody, expectedBody); }); suite('validation', function() { - function testSuccessFor(key, value, params) { - test(key + '=' + value + '(success)', function() { + function merge(base, extra) { + if (!extra || typeof extra != 'object') + return base || extra; + + var merged = Object.create(base); + Object.keys(extra).forEach(function(key) { + merged[key] = merge(base[key] || null, extra[key]); + }); + return merged; + } + + function testSuccessFor(request, baseRequest) { + request = merge(baseRequest, request); + test(JSON.stringify(request) + ' (success)', function() { assert.doesNotThrow(function() { - params = Object.create(params || {}); - params[key] = value; - builders.search({ params: params }); + builders.search(request); }); }); } - function testFailFor(key, value, params, errorMessage) { - test(key + '=' + value + '(fail)', function() { + function testFailFor(request, errorMessage, baseRequest) { + request = merge(baseRequest, request); + test(JSON.stringify(request) + ' (fail)', function() { assert.throws(function() { - params = Object.create(params || {}); - params[key] = value; - builders.search({ params: params }); + builders.search(request); }, errorMessage); }); } - var baseParams = { tableName: 'test' }; + var baseRequest = { + params: {}, + query: {} + }; + + testSuccessFor({ params: { tableName: 'foobar' } }, baseRequest); + testFailFor({}, 'no source', baseRequest); + testFailFor({ params: { tableName: '' } }, 'no source', baseRequest); - testSuccessFor('tableName', 'foobar'); - testFailFor('', '', null, 'no source'); - testFailFor('tableName', '', null, 'no source'); + baseRequest.params.tableName = 'test'; - testSuccessFor('limit', '0', baseParams); - testSuccessFor('limit', '10', baseParams); - testSuccessFor('limit', '-10', baseParams); - testFailFor('limit', '', baseParams, 'invalid integer'); - testFailFor('limit', '0.1', baseParams, 'invalid integer'); - testFailFor('limit', '-0.1', baseParams, 'invalid integer'); - testFailFor('limit', 'foobar', baseParams, 'invalid integer'); + testSuccessFor({ query: { limit: '0' } }, baseRequest); + testSuccessFor({ query: { limit: '10' } }, baseRequest); + testSuccessFor({ query: { limit: '-10' } }, baseRequest); + testFailFor({ query: { limit: '' } }, 'invalid integer', baseRequest); + testFailFor({ query: { limit: '0.1' } }, 'invalid integer', baseRequest); + testFailFor({ query: { limit: '-0.1' } }, 'invalid integer', baseRequest); + testFailFor({ query: { limit: 'foobar' } }, 'invalid integer', baseRequest); - testSuccessFor('offset', '0', baseParams); - testSuccessFor('offset', '10', baseParams); - testSuccessFor('offset', '-10', baseParams); - testFailFor('offset', '', baseParams, 'invalid integer'); - testFailFor('offset', '0.1', baseParams, 'invalid integer'); - testFailFor('offset', '-0.1', baseParams, 'invalid integer'); - testFailFor('offset', 'foobar', baseParams, 'invalid integer'); + testSuccessFor({ query: { offset: '0' } }, baseRequest); + testSuccessFor({ query: { offset: '10' } }, baseRequest); + testSuccessFor({ query: { offset: '-10' } }, baseRequest); + testFailFor({ query: { offset: '' } }, 'invalid integer', baseRequest); + testFailFor({ query: { offset: '0.1' } }, 'invalid integer', baseRequest); + testFailFor({ query: { offset: '-0.1' } }, 'invalid integer', baseRequest); + testFailFor({ query: { offset: 'foobar' } }, 'invalid integer', baseRequest); }); }); }); -------------- next part -------------- HTML����������������������������...Download