[Groonga-commit] groonga/gcs [master] Normalize responses more intelligently

Back to archive index

YUKI Hiroshi null+****@clear*****
Thu Nov 15 10:29:10 JST 2012


YUKI Hiroshi	2012-11-15 10:29:10 +0900 (Thu, 15 Nov 2012)

  New Revision: a32e4d563fbb4ed65b59a715610921823596dfc8
  https://github.com/groonga/gcs/commit/a32e4d563fbb4ed65b59a715610921823596dfc8

  Log:
    Normalize responses more intelligently

  Modified files:
    test/api-configuration.test.js
    tools/run-scenarios
    tools/scenario-runner.js

  Modified: test/api-configuration.test.js (+2 -2)
===================================================================
--- test/api-configuration.test.js    2012-11-15 10:12:51 +0900 (719e669)
+++ test/api-configuration.test.js    2012-11-15 10:29:10 +0900 (8b9512b)
@@ -1386,8 +1386,8 @@ suite('Configuration API', function() {
                 expected = fs.readFileSync(expected).toString();
                 expected = new ScenarioResponse(expected);
                 var actual = new ScenarioResponse(request.response);
-                assert.deepEqual(actual.bodyNormalizedJSON,
-                                 expected.bodyNormalizedJSON);
+                assert.deepEqual(actual.normalizedBody,
+                                 expected.normalizedBody);
               });
               done();
             } catch(error) {

  Modified: tools/run-scenarios (+1 -0)
===================================================================
--- tools/run-scenarios    2012-11-15 10:12:51 +0900 (312f510)
+++ tools/run-scenarios    2012-11-15 10:29:10 +0900 (81f9c1b)
@@ -8,6 +8,7 @@
 
 var CLI = require(__dirname + '/../lib/command-line').CommandLineInterface;
 var ScenariosRunner = require(__dirname + '/scenario-runner').ScenariosRunner;
+var ScenarioResponse = require(__dirname + '/scenario-runner').Response;
 var fs = require('fs');
 var path = require('path');
 var mkdirp = require('mkdirp');

  Modified: tools/scenario-runner.js (+40 -19)
===================================================================
--- tools/scenario-runner.js    2012-11-15 10:12:51 +0900 (3ec2452)
+++ tools/scenario-runner.js    2012-11-15 10:29:10 +0900 (822dd81)
@@ -155,14 +155,23 @@ ScenariosRunner.toSafeName =
 
 function Response(source) {
   source = source.split('\r\n\r\n');
-  this.headers = source[0];
-  this.body = source.slice(1).join('\r\n\r\n');
+  this.rawHeaders = source[0];
+  this.rawBody = source.slice(1).join('\r\n\r\n');
 }
 Response.prototype = {
-  get bodyRawJSON() {
-    if (!this._bodyRawJSON)
-      this._bodyRawJSON = this._XMLStringToJSON(this.body);
-    return this._bodyRawJSON;
+  get body() {
+    if (!this._body) {
+      if (this.rawBody.indexOf('<?xml') == 0) {
+        this._body = this._XMLStringToJSON(this.rawBody);
+      } else {
+        try {
+          this._body = JSON.parse(this.rawBody);
+        } catch(error) {
+          this._body = this.rawBody;
+        }
+      }
+    }
+    return this._body;
   },
   _XMLStringToJSON: function(xml) {
     var parser = new xml2js.Parser({
@@ -181,30 +190,42 @@ Response.prototype = {
     return json;
   },
 
-  get bodySortedJSON() {
-    if (!this._bodySortedJSON)
-      this._bodySortedJSON = this._toSortedJSON(this.bodyRawJSON);
-    return this._bodySortedJSON;
+  get sortedBody() {
+    if (!this._sortedBody) {
+      if (this.body && typeof this.body == 'object')
+        this._sortedBody = this._toSortedJSON(this.body, false);
+      else
+        this._sortedBody = this.body;
+    }
+    return this._sortedBody;
   },
-  _toSortedJSON: function(fragment) {
+  _toSortedJSON: function(fragment, doSort) {
     switch (typeof fragment) {
       default:
         return fragment;
       case 'object':
         var format = {};
-        Object.keys(fragment).sort().forEach(function(key) {
+        var keys = Object.keys(fragment);
+        if (!doSort) keys.sort();
+        keys.forEach(function(key) {
           if (!fragment.hasOwnProperty(key))
             return;
-          format[key] = this._toSortedJSON(fragment[key]);
+          var doSort = this._orderedContainers.indexOf(key) < 0;
+          format[key] = this._toSortedJSON(fragment[key], doSort);
         }, this);
         return format;
     }
   },
-
-  get bodyNormalizedJSON() {
-    if (!this._bodyNormalizedJSON)
-      this._bodyNormalizedJSON = this._normalize(this.bodySortedJSON);
-    return this._bodyNormalizedJSON;
+  _orderedContainers: [
+    'DomainStatusList',
+    'IndexFields',
+    'FieldNames'
+  ],
+
+  get normalizedBody() {
+    if (!this._normalizedBody)
+      this._normalizedBody = this._normalize(this.sortedBody);
+    return this._normalizedBody;
   },
   _normalize: function(fragment) {
     switch (typeof fragment) {
@@ -212,7 +233,7 @@ Response.prototype = {
         return fragment;
       case 'object':
         var format = {};
-        Object.keys(fragment).sort().forEach(function(key) {
+        Object.keys(fragment).forEach(function(key) {
           if (!fragment.hasOwnProperty(key))
             return;
 
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index