/*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 + ">", ""); 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 + ">", ""); 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 + ">", ""); 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();