1 | var http = require("..");
|
2 | var mesh = require("mesh");
|
3 | var expect = require("expect.js");
|
4 |
|
5 | describe(__filename + "#", function() {
|
6 |
|
7 | var requests;
|
8 | var db;
|
9 |
|
10 | function request(options, next) {
|
11 | requests.push(options);
|
12 | next(null, options.data);
|
13 | }
|
14 |
|
15 | beforeEach(function() {
|
16 | requests = [];
|
17 | });
|
18 |
|
19 | it("can customize the methods", function(next) {
|
20 | var stream = mesh.open(http({
|
21 | request: request,
|
22 | method: function(operation) {
|
23 | return {
|
24 | "insert" : "a",
|
25 | "update" : "b",
|
26 | "load" : "c",
|
27 | "remove" : "d"
|
28 | }[operation.name];
|
29 | }
|
30 | }));
|
31 | stream.on("end", function() {
|
32 | expect(requests[0].method).to.be("a");
|
33 | expect(requests[1].method).to.be("b");
|
34 | expect(requests[2].method).to.be("c");
|
35 | expect(requests[3].method).to.be("d");
|
36 | next();
|
37 | });
|
38 |
|
39 | stream.write(mesh.operation("insert", { path: "/ab" }));
|
40 | stream.write(mesh.operation("update", { path: "/ab" }));
|
41 | stream.write(mesh.operation("load", { path: "/ab" }));
|
42 | stream.end(mesh.operation("remove", { path: "/ab" }));
|
43 |
|
44 | });
|
45 |
|
46 | it("can run a get request", function(next) {
|
47 | mesh.open(http({ request: request })).on("end", function() {
|
48 | expect(requests[0].method).to.be("post");
|
49 | expect(requests[0].uri).to.be("/ab");
|
50 | next();
|
51 | }).end(mesh.operation("insert", { path: "/ab" }));
|
52 | });
|
53 |
|
54 | it("can pass params in the path", function(next) {
|
55 | var stream = mesh.stream(http({ request: request })).on("end", function() {
|
56 | expect(requests[0].uri).to.be("/blarg");
|
57 | next();
|
58 | }).end(mesh.operation("insert", { data: { name: "blarg" }, path: "/:data.name" }));
|
59 | });
|
60 |
|
61 | it("can have http specific params", function(next) {
|
62 | var stream = mesh.stream(http({ request: request })).
|
63 | on("end", function() {
|
64 | expect(requests[0].uri).to.be("/blarg");
|
65 | next();
|
66 | }).end(mesh.operation("insert", { http: { data: { name: "blarg" }, path: "/:data.name" }}));
|
67 | });
|
68 |
|
69 | it("path can be a function", function(next) {
|
70 | var stream = mesh.stream(http({ request: request })).
|
71 | on("end", function() {
|
72 | expect(requests[0].uri).to.be("/blarg");
|
73 | next();
|
74 | }).end(mesh.operation("insert", { data: { name: "blarg" }, path: function(op) {
|
75 | expect(op.name).to.be("insert");
|
76 | return "/:data.name";
|
77 | }}));
|
78 | });
|
79 |
|
80 | it("method can be function", function(next) {
|
81 | var stream = mesh.stream(http({ request: request }));
|
82 | stream.on("end", function() {
|
83 | expect(requests[0].method).to.be("patch");
|
84 | next();
|
85 | });
|
86 | stream.end(mesh.operation("insert", { data: { name: "blarg" }, path:"/a", method: function(operation) {
|
87 | expect(operation.path).to.be("/a");
|
88 | return "patch";
|
89 | }}));
|
90 | });
|
91 |
|
92 | it("can use a transform function for the response", function(next) {
|
93 | var stream = mesh.stream(http({ request: request }));
|
94 | stream.on("data", function(data) {
|
95 | expect(data.data.name).to.be("blarg");
|
96 | next();
|
97 | }).end(mesh.operation("insert", {
|
98 | data: { name: "blarg" },
|
99 | path: "/a",
|
100 | transform: function(data) {
|
101 | return { data: data };
|
102 | }
|
103 | }));
|
104 | });
|
105 |
|
106 | it("can provide custom http headers", function(next) {
|
107 | var stream = mesh.stream(http({ request: request }));
|
108 |
|
109 | stream.on("end", function() {
|
110 | expect(requests[0].headers.ua).to.be("b");
|
111 | next();
|
112 | });
|
113 | stream.end(mesh.operation("insert", { data: { name: "blarg" }, path:"/a", headers: { ua: "b" } }));
|
114 | });
|
115 |
|
116 | it("headers can be a function", function(next) {
|
117 | var stream = mesh.stream(http({ request: request }));
|
118 | stream.on("end", function() {
|
119 | expect(requests[0].headers.ua).to.be("b");
|
120 | next();
|
121 | });
|
122 | stream.end(mesh.operation("insert", { data: { name: "blarg" }, path:"/a", headers: function(operation) {
|
123 | expect(operation.path).to.be("/a");
|
124 | return { ua: "b" };
|
125 | } }));
|
126 | });
|
127 |
|
128 | it("can add a query", function(next) {
|
129 | var stream = mesh.stream(http({ request: request }));
|
130 | stream.on("end", function() {
|
131 | expect(requests[0].uri).to.be("/a");
|
132 | expect(requests[0].query.ua).to.be("b");
|
133 | next();
|
134 | });
|
135 | stream.end(mesh.operation("insert", { data: { name: "blarg" }, path:"/a", query: {ua:"b"}}));
|
136 | });
|
137 |
|
138 | it("query can be a function", function(next) {
|
139 | var stream = mesh.stream(http({ request: request }));
|
140 | stream.on("end", function() {
|
141 | expect(requests[0].query.ua).to.be("b");
|
142 | next();
|
143 | });
|
144 | stream.end(mesh.operation("insert", { data: { name: "blarg" }, path:"/a", query: function(operation) {
|
145 | expect(operation.path).to.be("/a");
|
146 | return { ua: "b" };
|
147 | } }));
|
148 | });
|
149 |
|
150 | it("automatically maps the path based on the collection", function(next) {
|
151 |
|
152 | var stream = mesh.stream(http({
|
153 | request: request
|
154 | }));
|
155 |
|
156 | stream.on("end", function() {
|
157 | expect(requests[0].uri).to.be("/people");
|
158 | expect(requests[1].uri).to.be("/people/1");
|
159 | expect(requests[2].uri).to.be("/people/1");
|
160 | expect(requests[3].uri).to.be("/people");
|
161 | expect(requests[4].uri).to.be("/people/1");
|
162 | next();
|
163 | });
|
164 |
|
165 | stream.write(mesh.operation("insert", { collection: "people", data: { uid: "1" }}));
|
166 | stream.write(mesh.operation("update", { collection: "people", data: { uid: "1" }}));
|
167 | stream.write(mesh.operation("load", { collection: "people", data: { uid: "1" }}));
|
168 | stream.write(mesh.operation("load", { collection: "people", multi: true, data: { uid: "1" }}));
|
169 | stream.write(mesh.operation("remove", { collection: "people", data: { uid: "1" }}));
|
170 | stream.end();
|
171 |
|
172 | });
|
173 |
|
174 | xit("can change the idProperty", function() {
|
175 | });
|
176 |
|
177 | it("can add a prefix", function(next) {
|
178 | var stream = mesh.stream(http({
|
179 | prefix: "/api",
|
180 | request: request
|
181 | }));
|
182 | stream.on("end", function() {
|
183 | expect(requests[0].uri).to.be("/api/people");
|
184 | next();
|
185 | });
|
186 | stream.end(mesh.operation("insert", { collection: "people", data: { uid: "1" }}));
|
187 | });
|
188 |
|
189 | it("can add a prefix in the operation", function(next) {
|
190 | var stream = mesh.stream(http({
|
191 | request: request
|
192 | }));
|
193 | stream.on("end", function() {
|
194 | expect(requests[0].uri).to.be("/api/people");
|
195 | next();
|
196 | });
|
197 | stream.end(mesh.operation("insert", { prefix: "/api", collection: "people", data: { uid: "1" }}));
|
198 | });
|
199 |
|
200 | it("can upsert and insert a value", function(next) {
|
201 | var db = http({
|
202 | request: request
|
203 | });
|
204 |
|
205 | db(mesh.operation("upsert", {
|
206 | collection: "people",
|
207 | data: { name: "a" }
|
208 | })).on("end", function() {
|
209 | expect(requests[0].uri).to.be("/people");
|
210 | expect(requests[0].method).to.be("post");
|
211 | next();
|
212 | });
|
213 | });
|
214 |
|
215 | it("can upsert and update a value", function(next) {
|
216 | var db = http({
|
217 | request: request
|
218 | });
|
219 |
|
220 | db(mesh.operation("upsert", {
|
221 | collection: "people",
|
222 | data: { uid: "a" }
|
223 | })).on("end", function() {
|
224 | expect(requests[0].uri).to.be("/people/a");
|
225 | expect(requests[0].method).to.be("put");
|
226 | next();
|
227 | });
|
228 | });
|
229 |
|
230 | it("doesn't add query if obj exists", function(next) {
|
231 | var db = http({
|
232 | request: request
|
233 | });
|
234 |
|
235 | db(mesh.operation("upsert", {
|
236 | collection: "people",
|
237 | data: { uid: "a" },
|
238 | query: {},
|
239 | })).on("end", function() {
|
240 | expect(requests[0].uri).to.be("/people/a");
|
241 | expect(requests[0].method).to.be("put");
|
242 | next();
|
243 | });
|
244 | });
|
245 |
|
246 | it("passes data to body if it's a string", function(next) {
|
247 | var db = http({
|
248 | request: request
|
249 | });
|
250 |
|
251 | db(mesh.operation("upsert", {
|
252 | collection: "people",
|
253 | data: "abc",
|
254 | query: {},
|
255 | })).on("end", function() {
|
256 | expect(requests[0].uri).to.be("/people");
|
257 | expect(requests[0].data).to.be("abc");
|
258 | next();
|
259 | });
|
260 | });
|
261 | });
|