mirror of https://gitee.com/bigwinds/arangodb
Merge branch 'devel' of https://github.com/arangodb/arangodb into vpack
This commit is contained in:
commit
c019a6ecd5
|
@ -530,7 +530,6 @@ SHELL_SERVER_ONLY = \
|
||||||
@top_srcdir@/js/server/tests/shell-database-noncluster.js \
|
@top_srcdir@/js/server/tests/shell-database-noncluster.js \
|
||||||
@top_srcdir@/js/server/tests/shell-foxx.js \
|
@top_srcdir@/js/server/tests/shell-foxx.js \
|
||||||
@top_srcdir@/js/server/tests/shell-foxx-base-middleware.js \
|
@top_srcdir@/js/server/tests/shell-foxx-base-middleware.js \
|
||||||
@top_srcdir@/js/server/tests/shell-foxx-format-middleware.js \
|
|
||||||
@top_srcdir@/js/server/tests/shell-foxx-model.js \
|
@top_srcdir@/js/server/tests/shell-foxx-model.js \
|
||||||
@top_srcdir@/js/server/tests/shell-foxx-model-events-spec.js \
|
@top_srcdir@/js/server/tests/shell-foxx-model-events-spec.js \
|
||||||
@top_srcdir@/js/server/tests/shell-foxx-preprocessor.js \
|
@top_srcdir@/js/server/tests/shell-foxx-preprocessor.js \
|
||||||
|
|
|
@ -1,124 +0,0 @@
|
||||||
'use strict';
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// @brief Foxx Format Middleware
|
|
||||||
///
|
|
||||||
/// @file
|
|
||||||
///
|
|
||||||
/// DISCLAIMER
|
|
||||||
///
|
|
||||||
/// Copyright 2013 triagens GmbH, Cologne, Germany
|
|
||||||
///
|
|
||||||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
/// you may not use this file except in compliance with the License.
|
|
||||||
/// You may obtain a copy of the License at
|
|
||||||
///
|
|
||||||
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
///
|
|
||||||
/// Unless required by applicable law or agreed to in writing, software
|
|
||||||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
/// See the License for the specific language governing permissions and
|
|
||||||
/// limitations under the License.
|
|
||||||
///
|
|
||||||
/// Copyright holder is triAGENS GmbH, Cologne, Germany
|
|
||||||
///
|
|
||||||
/// @author Lucas Dohmen
|
|
||||||
/// @author Copyright 2013, triAGENS GmbH, Cologne, Germany
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// @brief FormatMiddleware
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
function FormatMiddleware(allowedFormats, defaultFormat) {
|
|
||||||
var stringRepresentation, middleware = function (request, response, options, next) {
|
|
||||||
var parsed, determinePathAndFormat;
|
|
||||||
|
|
||||||
determinePathAndFormat = function (path, headers) {
|
|
||||||
var mimeTypes = require("org/arangodb/mimetypes").mimeTypes,
|
|
||||||
extensions = require("org/arangodb/mimetypes").extensions,
|
|
||||||
urlFormatToMime = function (urlFormat) {
|
|
||||||
var mimeType;
|
|
||||||
|
|
||||||
if (mimeTypes[urlFormat]) {
|
|
||||||
mimeType = mimeTypes[urlFormat][0];
|
|
||||||
} else {
|
|
||||||
mimeType = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return mimeType;
|
|
||||||
},
|
|
||||||
mimeToUrlFormat = function (mimeType) {
|
|
||||||
var urlFormat;
|
|
||||||
|
|
||||||
if (extensions[mimeType]) {
|
|
||||||
urlFormat = extensions[mimeType][0];
|
|
||||||
} else {
|
|
||||||
urlFormat = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return urlFormat;
|
|
||||||
},
|
|
||||||
parsed = {
|
|
||||||
contentType: headers.accept
|
|
||||||
};
|
|
||||||
|
|
||||||
path = path.split('.');
|
|
||||||
|
|
||||||
if (path.length === 1) {
|
|
||||||
parsed.path = path.join();
|
|
||||||
} else {
|
|
||||||
parsed.format = path.pop();
|
|
||||||
parsed.path = path.join('.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parsed.contentType === undefined && parsed.format === undefined) {
|
|
||||||
parsed.format = defaultFormat;
|
|
||||||
parsed.contentType = urlFormatToMime(defaultFormat);
|
|
||||||
} else if (parsed.contentType === undefined) {
|
|
||||||
parsed.contentType = urlFormatToMime(parsed.format);
|
|
||||||
} else if (parsed.format === undefined) {
|
|
||||||
parsed.format = mimeToUrlFormat(parsed.contentType);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parsed.format !== mimeToUrlFormat(parsed.contentType)) {
|
|
||||||
throw new Error("Contradiction between Accept Header and URL.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (allowedFormats.indexOf(parsed.format) < 0) {
|
|
||||||
throw new Error("Format '" + parsed.format + "' is not allowed.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return parsed;
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
parsed = determinePathAndFormat(request.path, request.headers);
|
|
||||||
request.path = parsed.path;
|
|
||||||
request.format = parsed.format;
|
|
||||||
response.contentType = parsed.contentType;
|
|
||||||
next();
|
|
||||||
} catch (e) {
|
|
||||||
response.responseCode = 406;
|
|
||||||
response.body = e;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
stringRepresentation = String(middleware)
|
|
||||||
.replace("allowedFormats", JSON.stringify(allowedFormats))
|
|
||||||
.replace("defaultFormat", JSON.stringify(defaultFormat));
|
|
||||||
|
|
||||||
return middleware;
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.FormatMiddleware = FormatMiddleware;
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
// --SECTION-- END-OF-FILE
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
/// Local Variables:
|
|
||||||
/// mode: outline-minor
|
|
||||||
/// outline-regexp: "/// @brief\\|/// @addtogroup\\|/// @page\\|// --SECTION--\\|/// @\\}\\|/\\*jslint"
|
|
||||||
/// End:
|
|
|
@ -1,148 +0,0 @@
|
||||||
/*jshint globalstrict:false, strict:false */
|
|
||||||
/*global assertEqual, assertFalse */
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// @brief tests for dump/reload
|
|
||||||
///
|
|
||||||
/// @file
|
|
||||||
///
|
|
||||||
/// DISCLAIMER
|
|
||||||
///
|
|
||||||
/// Copyright 2010-2012 triagens GmbH, Cologne, Germany
|
|
||||||
///
|
|
||||||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
/// you may not use this file except in compliance with the License.
|
|
||||||
/// You may obtain a copy of the License at
|
|
||||||
///
|
|
||||||
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
///
|
|
||||||
/// Unless required by applicable law or agreed to in writing, software
|
|
||||||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
/// See the License for the specific language governing permissions and
|
|
||||||
/// limitations under the License.
|
|
||||||
///
|
|
||||||
/// Copyright holder is triAGENS GmbH, Cologne, Germany
|
|
||||||
///
|
|
||||||
/// @author Lukas Doomen
|
|
||||||
/// @author Copyright 2012, triAGENS GmbH, Cologne, Germany
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
var jsunity = require("jsunity");
|
|
||||||
|
|
||||||
function FormatMiddlewareSpec () {
|
|
||||||
'use strict';
|
|
||||||
var Middleware, middleware, request, response, options, next;
|
|
||||||
|
|
||||||
return {
|
|
||||||
setUp: function () {
|
|
||||||
Middleware = require('org/arangodb/foxx/format_middleware').FormatMiddleware;
|
|
||||||
request = {};
|
|
||||||
response = {};
|
|
||||||
options = {};
|
|
||||||
next = function () {};
|
|
||||||
},
|
|
||||||
|
|
||||||
testChangesTheURLAccordingly: function () {
|
|
||||||
request = { path: "test/1.json", headers: {} };
|
|
||||||
middleware = new Middleware(["json"]);
|
|
||||||
middleware(request, response, options, next);
|
|
||||||
assertEqual(request.path, "test/1");
|
|
||||||
},
|
|
||||||
|
|
||||||
testRefusesFormatsThatHaveNotBeenAllowed: function () {
|
|
||||||
var nextCalled = false,
|
|
||||||
next = function () { nextCalled = true; };
|
|
||||||
middleware = new Middleware(["json"]);
|
|
||||||
request = { path: "test/1.html", headers: {} };
|
|
||||||
middleware(request, response, options, next);
|
|
||||||
assertEqual(response.responseCode, 406);
|
|
||||||
assertEqual("<" + response.body + ">", "<Error: Format 'html' is not allowed.>");
|
|
||||||
assertFalse(nextCalled);
|
|
||||||
},
|
|
||||||
|
|
||||||
testRefuseContradictingURLAndResponseType: function () {
|
|
||||||
var nextCalled = false,
|
|
||||||
next = function () { nextCalled = true; };
|
|
||||||
request = { path: "test/1.json", headers: {"accept": "text/html"} };
|
|
||||||
middleware = new Middleware(["json"]);
|
|
||||||
middleware(request, response, options, next);
|
|
||||||
assertEqual(response.responseCode, 406);
|
|
||||||
assertEqual("<" + response.body + ">", "<Error: Contradiction between Accept Header and URL.>");
|
|
||||||
assertFalse(nextCalled);
|
|
||||||
},
|
|
||||||
|
|
||||||
testRefuseMissingBothURLAndResponseTypeWhenNoDefaultWasGiven: function () {
|
|
||||||
var nextCalled = false,
|
|
||||||
next = function () { nextCalled = true; };
|
|
||||||
request = { path: "test/1", headers: {} };
|
|
||||||
middleware = new Middleware(["json"]);
|
|
||||||
middleware(request, response, options, next);
|
|
||||||
assertEqual(response.responseCode, 406);
|
|
||||||
assertEqual("<" + response.body + ">", "<Error: Format 'undefined' is not allowed.>");
|
|
||||||
assertFalse(nextCalled);
|
|
||||||
},
|
|
||||||
|
|
||||||
testFallBackToDefaultWhenMissingBothURLAndResponseType: function () {
|
|
||||||
request = { path: "test/1", headers: {} };
|
|
||||||
middleware = new Middleware(["json"], "json");
|
|
||||||
middleware(request, response, options, next);
|
|
||||||
assertEqual(request.format, "json");
|
|
||||||
assertEqual(response.contentType, "application/json");
|
|
||||||
},
|
|
||||||
|
|
||||||
// JSON
|
|
||||||
testSettingTheFormatAttributeAndResponseTypeForJsonViaURL: function () {
|
|
||||||
request = { path: "test/1.json", headers: {} };
|
|
||||||
middleware = new Middleware(["json"]);
|
|
||||||
middleware(request, response, options, next);
|
|
||||||
assertEqual(request.format, "json");
|
|
||||||
assertEqual(response.contentType, "application/json");
|
|
||||||
},
|
|
||||||
|
|
||||||
testSettingTheFormatAttributeAndResponseTypeForJsonViaAcceptHeader: function () {
|
|
||||||
request = { path: "test/1", headers: {"accept": "application/json"} };
|
|
||||||
middleware = new Middleware(["json"]);
|
|
||||||
middleware(request, response, options, next);
|
|
||||||
assertEqual(request.format, "json");
|
|
||||||
assertEqual(response.contentType, "application/json");
|
|
||||||
},
|
|
||||||
|
|
||||||
// HTML
|
|
||||||
testSettingTheFormatAttributeAndResponseTypeForHtmlViaURL: function () {
|
|
||||||
request = { path: "test/1.html", headers: {} };
|
|
||||||
middleware = new Middleware(["html"]);
|
|
||||||
middleware(request, response, options, next);
|
|
||||||
assertEqual(request.format, "html");
|
|
||||||
assertEqual(response.contentType, "text/html");
|
|
||||||
},
|
|
||||||
|
|
||||||
testSettingTheFormatAttributeAndResponseTypeForHtmlViaAcceptHeader: function () {
|
|
||||||
request = { path: "test/1", headers: {"accept": "text/html"} };
|
|
||||||
middleware = new Middleware(["html"]);
|
|
||||||
middleware(request, response, options, next);
|
|
||||||
assertEqual(request.format, "html");
|
|
||||||
assertEqual(response.contentType, "text/html");
|
|
||||||
},
|
|
||||||
|
|
||||||
// TXT
|
|
||||||
testSettingTheFormatAttributeAndResponseTypeForTxtViaURL: function () {
|
|
||||||
request = { path: "test/1.txt", headers: {} };
|
|
||||||
middleware = new Middleware(["txt"]);
|
|
||||||
middleware(request, response, options, next);
|
|
||||||
assertEqual(request.format, "txt");
|
|
||||||
assertEqual(response.contentType, "text/plain");
|
|
||||||
},
|
|
||||||
|
|
||||||
testSettingTheFormatAttributeAndResponseTypeForTxtViaAcceptHeader: function () {
|
|
||||||
request = { path: "test/1", headers: {"accept": "text/plain"} };
|
|
||||||
middleware = new Middleware(["txt"]);
|
|
||||||
middleware(request, response, options, next);
|
|
||||||
assertEqual(request.format, "txt");
|
|
||||||
assertEqual(response.contentType, "text/plain");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
jsunity.run(FormatMiddlewareSpec);
|
|
||||||
|
|
||||||
return jsunity.done();
|
|
Loading…
Reference in New Issue