-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathExpressTests.dart
146 lines (120 loc) · 4.61 KB
/
ExpressTests.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import "package:unittest/unittest.dart";
import "../lib/express.dart";
import "dart:io";
import "InMemoryRedisClient.dart";
import "package:json_client/json_client.dart";
main(){
group("Express unit tests", (){
test("hashCode test", (){
var item = {"content":"new TODO","done":false,"order":0};
var map = new Map<String,Object>();
map["key"] = item;
expect(map["key"], equals(item), reason:"can store an object in a map");
});
test("Express: routes", (){
expect(pathMatcher("/tests", "/tests"), equals({}), reason:"matches exact path");
expect(pathMatcher("/tests/:id", "/tests/1"), equals({'id':'1'}), reason:"matches path with arg");
expect(pathMatcher("/users/:id/todos/:todoId", "/users/1/todos/2"), equals({'id':'1','todoId':'2'}), reason:"matches path with 2 args");
expect(pathMatcher("/tests/:id", "/tests"), isNull, reason:"child path does not match parent route");
expect(pathMatcher("/tests/:id", "/rests"), isNull, reason:"does not match invalid path");
expect(pathMatcher("/todos", "/todos.css"), isNull, reason:"does not match same path with .css ext");
expect(pathMatcher("/todos", "/todos.js"), isNull, reason:"does not match same path with .js ext");
});
});
group("Express Integration tests", (){
Express app;
InMemoryRedisClient redis;
JsonClient client;
setUp((){
redis = new InMemoryRedisClient();
client = new JsonClient("http://127.0.0.1:8000");
app = new Express();
app["/counter"] = (HttpContext ctx){
redis.incr("counter").then((nextIncr) =>
ctx.sendJson(nextIncr)
);
};
app
.use(new StaticFileHandler())
.get("/todos", (HttpContext ctx){
redis.keys("todo:*").then((keys) =>
redis.mget(keys).then(ctx.sendJson)
);
})
.get("/todos/:id", (HttpContext ctx){
var id = ctx.params["id"];
redis.get("todo:$id").then((todo) =>
todo != null
? ctx.sendJson(todo)
: ctx.notFound("todo $id does not exist")
);
})
.post("/todos", (HttpContext ctx){
ctx.readAsJson().then((x){
redis.incr("ids:todo").then((newId){
var todo = x;
todo["id"] = newId;
redis.set("todo:$newId", todo);
ctx.sendJson(todo);
});
});
})
.put("/todos/:id", (HttpContext ctx){
var id = ctx.params["id"];
ctx.readAsJson().then((todo){
redis.set("todo:$id", todo);
ctx.sendJson(todo);
});
})
.delete("/todos/:id", (HttpContext ctx){
redis.del("todo:${ctx.params['id']}");
ctx.send();
});
return app.listen("127.0.0.1", 8000);
});
tearDown((){
app.close();
});
test("Express: Todo App", (){
/*JSONClient does not include the @Proxy annotation, warnings will be
generated on Dart Editor */
client.todos({"content":"new TODO","done":false,"order":0}).then(expectAsync1((_){
client.todos()
.then(expectAsync1((todos){
expect(todos.length, equals(1), reason:"expected 1 added TODO");
expect(todos[0]['content'], equals("new TODO"), reason:"new TODO was not added");
expect(todos[0]['id'], equals(1), reason:"new TODO id was not set");
var id = todos[0]['id'];
todos[0]['content'] = "updated TODO";
client.put("/todos/$id", todos[0])
.then(expectAsync1((_1){
client.todos(id)
.then(expectAsync1((todo){
expect(todo['content'], equals("updated TODO"), reason:"new TODO was not updated");
client.delete("/todos/$id")
.then(expectAsync1((_2){
cb(statusCode){
expect(statusCode, equals(404), reason:"Expected 404 - deleted TODO not found");
}
var future = client.todos(id);
future.then(cb).catchError(expectAsync1((HttpClientResponse e){
cb(e.statusCode);
}));
}));
}));
}));
}));
}));
});
test("Express: test app[route] handles all verbs", (){
client.counter()
.then(expectAsync1((counter) {
expect(counter, equals(1), reason:"New counter starts at 1");
client.counter({'force-POST':true})
.then(expectAsync1((nextCounter){
expect(nextCounter, equals(2), reason:"Next counter is 2");
}));
}));
});
});
}