├── .eslintrc ├── .gitignore ├── LICENSE.txt ├── OSSMETADATA ├── README.md ├── authors.txt ├── gulpfile.js ├── package.json ├── src ├── index.js └── requestToContext.js └── test ├── dataSourceRoute.js ├── index.js └── requestToContext.js /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": false, 4 | "node": true, 5 | "es6": false 6 | }, 7 | "rules": { 8 | // possible errors 9 | "comma-dangle": [ 2 ], 10 | "no-cond-assign": [ 2 ], 11 | "no-console": [ 1 ], 12 | "no-constant-condition": [ 2 ], 13 | "no-control-regex": [ 2 ], 14 | "no-debugger": [ 2 ], 15 | "no-dupe-args": [ 2 ], 16 | "no-dupe-keys": [ 2 ], 17 | "no-duplicate-case": [ 2 ], 18 | "no-empty": [ 2 ], 19 | "no-empty-class": [ 2 ], 20 | "no-ex-assign": [ 2 ], 21 | "no-extra-boolean-cast": [ 2 ], 22 | "no-extra-semi": [ 2 ], 23 | "no-func-assign": [ 2 ], 24 | // this is for variable hoisting, not necessary if we use block scoped declarations 25 | // "no-inner-declarations": [ 2, "both" ], 26 | "no-invalid-regexp": [ 2 ], 27 | "no-irregular-whitespace": [ 2 ], 28 | "no-negated-in-lhs": [ 2 ], 29 | // when IE8 dies 30 | "no-reserved-keys": [ 0 ], 31 | "no-regex-spaces": [ 2 ], 32 | "no-sparse-arrays": [ 2 ], 33 | "no-unreachable": [ 2 ], 34 | "use-isnan": [ 2 ], 35 | // should we enforce valid documentation comments? 36 | // i.e., if you do documentation, do it right 37 | // "valid-jsoc": [ 2 ], 38 | "valid-typeof": [ 2 ], 39 | 40 | // best practices 41 | "block-scoped-var": [ 2 ], 42 | // warning for now until we get them fixed 43 | "consistent-return": [ 2 ], 44 | "curly": [ 2 ], 45 | "default-case": [ 2 ], 46 | "dot-notation": [ 2, { "allowKeywords": true } ], 47 | "eqeqeq": [ 2 ], 48 | "guard-for-in": [ 2 ], 49 | "no-alert": [ 2 ], 50 | "no-caller": [ 2 ], 51 | "no-div-regex": [ 2 ], 52 | "no-eq-null": [ 2 ], 53 | "no-eval": [ 2 ], 54 | "no-extend-native": [ 2 ], 55 | "no-extra-bind": [ 2 ], 56 | "no-fallthrough": [ 2 ], 57 | "no-floating-decimal": [ 2 ], 58 | "no-implied-eval": [ 2 ], 59 | "no-iterator": [ 2 ], 60 | "no-labels": [ 2 ], 61 | "no-lone-blocks": [ 2 ], 62 | "no-loop-func": [ 2 ], 63 | "no-multi-spaces": [ 0 ], 64 | "no-native-reassign": [ 2 ], 65 | "no-new": [ 0 ], 66 | "no-new-func": [ 2 ], 67 | "no-new-wrappers": [ 2 ], 68 | "no-octal": [ 2 ], 69 | "no-octal-escape": [ 2 ], 70 | "no-param-reassign": [ 2 ], 71 | "no-proto": [ 2 ], 72 | "no-process-env": [ 2 ], 73 | "no-redeclare": [ 2 ], 74 | "no-return-assign": [ 2 ], 75 | "no-script-url": [ 2 ], 76 | "no-self-compare": [ 2 ], 77 | "no-sequences": [ 2 ], 78 | "no-throw-literal": [ 2 ], 79 | "no-unused-expressions": [ 2 ], 80 | 81 | "no-warning-comments": [ 1 ], 82 | "no-with": [ 2 ], 83 | "radix": [ 2 ], 84 | "wrap-iife": [ 2 ], 85 | "yoda": [ 0 ], 86 | 87 | // strict mode 88 | "strict": [ 2, "global" ], 89 | 90 | // variables 91 | "no-catch-shadow": [ 2 ], 92 | "no-delete-var": [ 2 ], 93 | "no-shadow": [ 2 ], 94 | "no-shadow-restricted-names": [ 2 ], 95 | "no-undef": [ 2 ], 96 | "no-undef-init": [ 2 ], 97 | "no-undefined": [ 2 ], 98 | "no-unused-vars": [ 2, { "vars": "all", "args": "none" } ], 99 | "no-use-before-define": [ 2, "nofunc" ], 100 | 101 | // node.js 102 | "handle-callback-err": [ 2, "^.*(e|E)rr" ], 103 | "no-mixed-requires": [ 2 ], 104 | "no-new-require": [ 2 ], 105 | "no-path-concat": [ 2 ], 106 | "no-process-exit": [ 0 ], 107 | 108 | // ES6 109 | "generator-star-spacing": [ 2, "after" ], 110 | 111 | // stylistic 112 | /* COMMENTING THESE OUT UNTIL WE GET CONCENSUS ON THEM 113 | "camelcase": [ 2, { "properties": "always" } ], 114 | "eol-last": [ 2 ], 115 | "key-spacing": [ 0 ], 116 | "no-lonely-if": [ 2 ], 117 | "no-array-constructor": [ 2 ], 118 | "no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ], 119 | "no-nested-ternary": [ 2 ], 120 | "no-new-object": [ 2 ], 121 | "no-underscore-dangle": [ 0 ], 122 | "no-trailing-spaces": [ 2 ], 123 | "no-wrap-func": [ 2 ], 124 | "semi": [ 2, "always" ], 125 | "space-after-keywords": [ 2, "always" ], 126 | "space-before-blocks": [ 2, "always" ], 127 | "space-before-function-paren": [ 2, { "anonymous": "never", "named": "never" } ], 128 | "space-infix-ops": [ 2 ], 129 | "space-return-throw-case": [ 2 ], 130 | "space-unary-ops": [ 2, { "words": true, "nonwords": false } ], 131 | "spaced-line-comment": [ 2, "always", { "exceptions": [ "-", "+" ] } ], 132 | "quotes": [ 2, "single", "avoid-escape" ], 133 | "wrap-regex": [ 2 ] 134 | */ 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | falkor_node.iml 3 | node_modules 4 | data -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2012 Netflix, Inc. 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /OSSMETADATA: -------------------------------------------------------------------------------- 1 | osslifecycle=active 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # falcor-express 2 | Server middleware for falcor-express 3 | 4 | Working usage example of the basic repro in netflix/falcor-express-demo 5 | 6 | ## Usage 7 | Minimalistic example 8 | 9 | ```javascript 10 | var FalcorServer = require('falcor-express'); 11 | var bodyParser = require('body-parser'); 12 | var express = require('express'); 13 | var app = express(); 14 | 15 | var TestRouter = ; 16 | 17 | app.use(bodyParser.text({ type: 'text/*' })) 18 | app.use('/model.json', FalcorServer.dataSourceRoute(function(req, res) { 19 | return new TestRouter(); 20 | })); 21 | 22 | app.use(express.static('.')); 23 | 24 | var server = app.listen(9090, function(err) { 25 | if (err) { 26 | console.error(err); 27 | return; 28 | } 29 | console.log("navigate to http://localhost:9090") 30 | }); 31 | 32 | ``` 33 | 34 | Example of using a static model (for development purposes only) 35 | 36 | ```javascript 37 | var falcor = require('falcor'); 38 | var FalcorServer = require('falcor-express'); 39 | var bodyParser = require('body-parser'); 40 | var express = require('express'); 41 | var app = express(); 42 | 43 | var model = new falcor.Model({ 44 | cache: { 45 | todos: [ 46 | { 47 | name: 'get milk from corner store', 48 | done: false 49 | }, 50 | { 51 | name: 'withdraw money from ATM', 52 | done: true 53 | } 54 | ] 55 | } 56 | }); 57 | 58 | app.use(bodyParser.text({ type: 'text/*' })) 59 | app.use('/model.json', FalcorServer.dataSourceRoute(function(req, res) { 60 | return model.asDataSource(); 61 | })); 62 | 63 | app.use(express.static('.')); 64 | 65 | var server = app.listen(9090, function(err) { 66 | if (err) { 67 | console.error(err); 68 | return; 69 | } 70 | console.log("navigate to http://localhost:9090") 71 | }); 72 | ``` 73 | 74 | ## Development 75 | Before contributing, please run the linter and the tests to be sure there are no issues. 76 | ``` 77 | npm run lint 78 | ``` 79 | and 80 | ``` 81 | npm run test 82 | ``` 83 | -------------------------------------------------------------------------------- /authors.txt: -------------------------------------------------------------------------------- 1 | Jafar Husain 2 | Paul Taylor 3 | Michael Paulson 4 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | var gulp = require('gulp'); 2 | var mocha = require('gulp-mocha'); 3 | var eslint = require('gulp-eslint'); 4 | 5 | gulp.task('test', function (cb) { 6 | gulp.src(['./test/index.js']) 7 | .pipe(mocha()) 8 | .on('end', cb); 9 | }); 10 | 11 | gulp.task('lint', [ 12 | 'lint-src', 13 | 'lint-test' 14 | ]); 15 | 16 | gulp.task('lint-src', function () { 17 | return gulp.src('src/*.js') 18 | .pipe(eslint({ 19 | globals: { 20 | 'require': false, 21 | 'module': false 22 | }, 23 | reset: true, 24 | useEslintrc: true, 25 | })) 26 | .pipe(eslint.format()); 27 | }); 28 | 29 | gulp.task('lint-test', function () { 30 | return gulp.src('test/*.js') 31 | .pipe(eslint({ 32 | globals: { 33 | 'require': false, 34 | 'module': false, 35 | 'it': false, 36 | 'describe': false 37 | }, 38 | reset: true, 39 | rules: { 40 | 'max-len': [2, 200], 41 | 'no-unused-expressions': 0 42 | }, 43 | useEslintrc: true, 44 | })) 45 | .pipe(eslint.format()); 46 | }); 47 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "falcor-express", 3 | "version": "0.1.4", 4 | "description": "A falcor server for express.", 5 | "main": "src/index.js", 6 | "homepage": "https://github.com/Netflix/falcor-express", 7 | "author": { 8 | "name": "Netflix", 9 | "url": "https://github.com/Netflix/falcor-express/authors.txt" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/Netflix/falcor-express.git" 14 | }, 15 | "licenses": [ 16 | { 17 | "type": "Apache License, Version 2.0", 18 | "url": "http://www.apache.org/licenses/LICENSE-2.0.html" 19 | } 20 | ], 21 | "scripts": { 22 | "lint": "node node_modules/gulp/bin/gulp.js lint", 23 | "test": "node node_modules/gulp/bin/gulp.js test" 24 | }, 25 | "dependencies": { 26 | "body-parser": "^1.12.4", 27 | "express": "^4.11.1", 28 | "rx": "~2.3.24" 29 | }, 30 | "devDependencies": { 31 | "chai": "^3.2.0", 32 | "eslint": "^0.21.0", 33 | "falcor-router": "0.2.9", 34 | "gulp": "^3.9.0", 35 | "gulp-eslint": "^0.15.0", 36 | "gulp-mocha": "^2.1.3", 37 | "sinon": "^1.15.4" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var requestToContext = require("./requestToContext"); 3 | var FalcorEndpoint = module.exports = {}; 4 | 5 | FalcorEndpoint.expressMiddleware = function(getDataSource) { 6 | console.warn("expressMiddleware is deprecated, use dataSourceRoute instead"); 7 | this.dataSourceRoute(getDataSource); 8 | }; 9 | 10 | FalcorEndpoint.dataSourceRoute = function(getDataSource) { 11 | return function(req, res, next) { 12 | var obs; 13 | var dataSource = getDataSource(req, res); 14 | var context = requestToContext(req); 15 | // probably this should be sanity check function? 16 | if (Object.keys(context).length === 0) { 17 | return res.status(500).send("Request not supported"); 18 | } 19 | if (typeof context.method === "undefined" || context.method.length === 0) { 20 | return res.status(500).send("No query method provided"); 21 | } 22 | if (typeof dataSource[context.method] === "undefined") { 23 | return res.status(500).send("Data source does not implement the requested method"); 24 | } 25 | 26 | if (context.method === "set") { 27 | obs = dataSource[context.method](context.jsonGraph); 28 | } else if (context.method === "call") { 29 | obs = dataSource[context.method](context.callPath, context.arguments, context.pathSuffixes, context.paths); 30 | } else { 31 | obs = dataSource[context.method]([].concat(context.paths)); 32 | } 33 | 34 | obs.subscribe(function(jsonGraphEnvelope) { 35 | res.status(200).json(jsonGraphEnvelope); 36 | }, function(err) { 37 | if (err instanceof Error) { 38 | return next(err); 39 | } 40 | res.status(500).send(err); 41 | }); 42 | }; 43 | }; 44 | -------------------------------------------------------------------------------- /src/requestToContext.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var url = require("url"); 4 | 5 | var parseArgs = { 6 | "jsonGraph": true, 7 | "callPath": true, 8 | "arguments": true, 9 | "pathSuffixes": true, 10 | "paths": true 11 | }; 12 | 13 | module.exports = function requestToContext(req) { 14 | var queryMap = req.method === "POST" ? req.body : url.parse(req.url, true).query; 15 | 16 | var context = {}; 17 | if (queryMap) { 18 | Object.keys(queryMap).forEach(function(key) { 19 | var arg = queryMap[key]; 20 | 21 | if (parseArgs[key] && arg) { 22 | context[key] = JSON.parse(arg); 23 | } else { 24 | context[key] = arg; 25 | } 26 | }); 27 | } 28 | 29 | return context; 30 | }; 31 | -------------------------------------------------------------------------------- /test/dataSourceRoute.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var chai = require('chai'); 4 | var expect = chai.expect; 5 | var dataSourceRoute = require('../src/index.js').dataSourceRoute; 6 | var Router = require('falcor-router'); 7 | var sinon = require('sinon'); 8 | 9 | describe('dataSourceRoute', function () { 10 | describe('Can interact with data correctly', function () { 11 | it('Calls get method', function (done) { 12 | // Fake GET request 13 | var fakeReq = { 14 | method: 'get', 15 | url: 'http://localhost:9090/?paths=[[%22genrelist%22,{%22from%22:0,%22to%22:5},%22titles%22,{%22from%22:0,%22to%22:5},%22name%22]]&method=get' 16 | }; 17 | 18 | // Stubbed response object 19 | var fakeRes = { 20 | status: sinon.stub().returnsThis(), 21 | json: sinon.stub().returnsThis() 22 | }; 23 | 24 | // Set up basic router and immediately invoke the returned funciton 25 | // with fake req and res objects to check on stubbed functions 26 | dataSourceRoute(function (req, res) { 27 | return new Router([{ 28 | route: 'route', 29 | get: function (pathSet) { 30 | return null; 31 | } 32 | }]); 33 | })(fakeReq, fakeRes); 34 | 35 | expect(fakeRes.status.calledOnce).to.equal(true); 36 | expect(fakeRes.status.args).to.deep.equal([[200]]); 37 | 38 | expect(fakeRes.json.calledOnce).to.equal(true); 39 | // Check that the res.send was called with a jsonGraph object 40 | var sentValue = fakeRes.json.args[0][0]; 41 | expect(typeof(sentValue)).to.equal('object'); 42 | expect(!!sentValue.jsonGraph).to.equal(true); 43 | 44 | done(); 45 | }); 46 | 47 | it('Calls set method', function (done) { 48 | // Fake POST request 49 | var fakeReq = { 50 | method: 'POST', 51 | body: { 52 | jsonGraph: '{"genrelist":{"0":{"titles":{"0":{"name":"jon"}}}},"paths":[["genrelist",0,"titles",0,"name"]]}', 53 | method: 'set' 54 | } 55 | }; 56 | 57 | // Stubbed response object 58 | var fakeRes = { 59 | status: sinon.stub().returnsThis(), 60 | json: sinon.stub().returnsThis() 61 | }; 62 | 63 | // Set up basic router and immediately invoke the returned funciton 64 | // with fake req and res objects to check on stubbed functions 65 | dataSourceRoute(function (req, res) { 66 | return new Router([{ 67 | route: 'route', 68 | set: function (pathSet) { 69 | return null; 70 | } 71 | }]); 72 | })(fakeReq, fakeRes); 73 | 74 | expect(fakeRes.status.calledOnce).to.equal(true); 75 | expect(fakeRes.status.args).to.deep.equal([[200]]); 76 | 77 | expect(fakeRes.json.calledOnce).to.equal(true); 78 | // Check that the res.send was called with a jsonGraph object 79 | var sentValue = fakeRes.json.args[0][0]; 80 | expect(typeof(sentValue)).to.equal('object'); 81 | expect(!!sentValue.jsonGraph).to.equal(true); 82 | 83 | done(); 84 | }); 85 | }); 86 | }); 87 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('./requestToContext'); 4 | require('./dataSourceRoute'); 5 | -------------------------------------------------------------------------------- /test/requestToContext.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var chai = require('chai'); 4 | var expect = chai.expect; 5 | var requestToContext = require('../src/requestToContext'); 6 | 7 | describe('requestToContext', function () { 8 | describe('Converts the request to the correct context', function () { 9 | it('For get requests', function (done) { 10 | var result = requestToContext({ 11 | method: 'get', 12 | url: 'http://localhost:9090/?paths=[[%22genrelist%22,{%22from%22:0,%22to%22:5},%22titles%22,{%22from%22:0,%22to%22:5},%22name%22]]&method=get' 13 | }); 14 | 15 | expect(result).to.deep.equal({ 16 | paths: [ 17 | [ 18 | 'genrelist', { 19 | from: 0, 20 | to: 5 21 | }, 22 | 'titles', { 23 | from: 0, 24 | to: 5 25 | }, 26 | 'name' 27 | ] 28 | ], 29 | method: 'get' 30 | }); 31 | 32 | done(); 33 | }); 34 | 35 | it('For post requests', function (done) { 36 | var result = requestToContext({ 37 | method: 'POST', 38 | body: { 39 | jsonGraph: '{"jsonGraph":{"genrelist":{"0":{"titles":{"0":{"name":"jon"}}}},"paths":[["genrelist",0,"titles",0,"name"]]}}', 40 | method: 'set' 41 | } 42 | }); 43 | 44 | expect(result).to.deep.equal({ 45 | jsonGraph: { 46 | jsonGraph: { 47 | "genrelist": { 48 | "0": { 49 | "titles": { 50 | "0": { 51 | "name": "jon" 52 | } 53 | } 54 | } 55 | }, 56 | "paths": [ 57 | ["genrelist", 0, "titles", 0, "name"] 58 | ] 59 | } 60 | }, 61 | method: 'set' 62 | }); 63 | 64 | done(); 65 | }); 66 | }); 67 | }); 68 | --------------------------------------------------------------------------------