11import { Database } from "../arangojs" ;
2+ import { DocumentCollection } from "../collection" ;
23import { Route } from "../route" ;
34import { expect } from "chai" ;
45
@@ -9,44 +10,226 @@ describe("Arbitrary HTTP routes", () => {
910 } ) ;
1011 describe ( "database.route" , ( ) => {
1112 it ( "returns a Route instance" , ( ) => {
12- let route = db . route ( ) ;
13+ const route = db . route ( ) ;
1314 expect ( route ) . to . be . an . instanceof ( Route ) ;
1415 } ) ;
1516 it ( "creates a route for the given path" , ( ) => {
16- let path = "/hi" ;
17- let route = db . route ( path ) ;
17+ const path = "/hi" ;
18+ const route = db . route ( path ) ;
1819 expect ( ( route as any ) . _path ) . to . equal ( path ) ;
1920 } ) ;
2021 it ( "passes the given headers to the new route" , ( ) => {
21- let route = db . route ( "/hello" , { "x-magic" : "awesome" } ) ;
22+ const route = db . route ( "/hello" , { "x-magic" : "awesome" } ) ;
2223 expect ( ( route as any ) . _headers ) . to . have . property ( "x-magic" , "awesome" ) ;
2324 } ) ;
2425 } ) ;
2526} ) ;
2627
2728describe ( "Route API" , ( ) => {
29+ const name = `testdb_${ Date . now ( ) } ` ;
30+ let db : Database ;
31+ let collection : DocumentCollection ;
32+ before ( done => {
33+ db = new Database ( {
34+ url : process . env . TEST_ARANGODB_URL || "http://localhost:8529" ,
35+ arangoVersion : Number ( process . env . ARANGO_VERSION || 30000 )
36+ } ) ;
37+ db
38+ . createDatabase ( name )
39+ . then ( ( ) => {
40+ db . useDatabase ( name ) ;
41+ collection = db . collection ( `c_${ Date . now ( ) } ` ) ;
42+ return collection . create ( ) ;
43+ } )
44+ . then ( ( ) => done ( ) )
45+ . catch ( done ) ;
46+ } ) ;
47+ after ( done => {
48+ db . useDatabase ( "_system" ) ;
49+ db
50+ . dropDatabase ( name )
51+ . then ( ( ) => done ( ) )
52+ . catch ( done ) ;
53+ } ) ;
54+ beforeEach ( done => {
55+ collection
56+ . truncate ( )
57+ . then ( ( ) => done ( ) )
58+ . catch ( done ) ;
59+ } ) ;
2860 describe ( "route.route" , ( ) => {
29- it ( "is missing tests" ) ;
61+ it ( "should concat path" , ( ) => {
62+ const route = db . route ( "/api" ) . route ( "/version" ) ;
63+ expect ( route ) . to . have . property ( "_path" , "/api/version" ) ;
64+ } ) ;
3065 } ) ;
3166 describe ( "route.get" , ( ) => {
32- it ( "is missing tests" ) ;
67+ it ( "should be executed using the route path" , done => {
68+ db
69+ . route ( "/_api/version" )
70+ . get ( )
71+ . then ( res => {
72+ expect ( res ) . to . have . property ( "body" ) ;
73+ const body = res . body ;
74+ expect ( body ) . to . have . property ( "version" ) ;
75+ expect ( body ) . to . have . property ( "server" ) ;
76+ } )
77+ . then ( ( ) => done ( ) )
78+ . catch ( done ) ;
79+ } ) ;
80+ it ( "should concat path to route path" , done => {
81+ db
82+ . route ( "/_api" )
83+ . get ( "/version" )
84+ . then ( res => {
85+ expect ( res ) . to . have . property ( "body" ) ;
86+ const body = res . body ;
87+ expect ( body ) . to . have . property ( "version" ) ;
88+ expect ( body ) . to . have . property ( "server" ) ;
89+ } )
90+ . then ( ( ) => done ( ) )
91+ . catch ( done ) ;
92+ } ) ;
93+ it ( "should passes query parameters" , done => {
94+ db
95+ . route ( "/_api" )
96+ . get ( "/version" , { details : true } )
97+ . then ( res => {
98+ expect ( res ) . to . have . property ( "body" ) ;
99+ const body = res . body ;
100+ expect ( body ) . to . have . property ( "version" ) ;
101+ expect ( body ) . to . have . property ( "server" ) ;
102+ expect ( body ) . to . have . property ( "details" ) ;
103+ } )
104+ . then ( ( ) => done ( ) )
105+ . catch ( done ) ;
106+ } ) ;
33107 } ) ;
34108 describe ( "route.post" , ( ) => {
35- it ( "is missing tests" ) ;
109+ it ( "should passes body" , done => {
110+ db
111+ . route ( `/_api/document/${ collection . name } ` )
112+ . post ( { foo : "bar" } )
113+ . then ( res => {
114+ expect ( res ) . to . have . property ( "body" ) ;
115+ expect ( res . body ) . to . have . property ( "_id" ) ;
116+ expect ( res . body ) . to . have . property ( "_key" ) ;
117+ expect ( res . body ) . to . have . property ( "_rev" ) ;
118+ } )
119+ . then ( ( ) => done ( ) )
120+ . catch ( done ) ;
121+ } ) ;
36122 } ) ;
37123 describe ( "route.put" , ( ) => {
38- it ( "is missing tests" ) ;
124+ let documentHandle : String ;
125+ beforeEach ( done => {
126+ collection
127+ . save ( { foo : "bar" } )
128+ . then ( doc => {
129+ documentHandle = doc . _id ;
130+ done ( ) ;
131+ } )
132+ . catch ( done ) ;
133+ } ) ;
134+ it ( "should passes body" , done => {
135+ db
136+ . route ( `/_api/document/${ documentHandle } ` )
137+ . put ( { hello : "world" } )
138+ . then ( res => {
139+ expect ( res ) . to . have . property ( "body" ) ;
140+ expect ( res . body ) . to . have . property ( "_id" ) ;
141+ expect ( res . body ) . to . have . property ( "_key" ) ;
142+ expect ( res . body ) . to . have . property ( "_rev" ) ;
143+ } )
144+ . then ( ( ) => done ( ) )
145+ . catch ( done ) ;
146+ } ) ;
39147 } ) ;
40148 describe ( "route.patch" , ( ) => {
41- it ( "is missing tests" ) ;
149+ let documentHandle : String ;
150+ beforeEach ( done => {
151+ collection
152+ . save ( { foo : "bar" } )
153+ . then ( doc => {
154+ documentHandle = doc . _id ;
155+ done ( ) ;
156+ } )
157+ . catch ( done ) ;
158+ } ) ;
159+ it ( "should passes body" , done => {
160+ db
161+ . route ( `/_api/document/${ documentHandle } ` )
162+ . patch ( { hello : "world" } )
163+ . then ( res => {
164+ expect ( res ) . to . have . property ( "body" ) ;
165+ expect ( res . body ) . to . have . property ( "_id" ) ;
166+ expect ( res . body ) . to . have . property ( "_key" ) ;
167+ expect ( res . body ) . to . have . property ( "_rev" ) ;
168+ } )
169+ . then ( ( ) => done ( ) )
170+ . catch ( done ) ;
171+ } ) ;
42172 } ) ;
43173 describe ( "route.delete" , ( ) => {
44- it ( "is missing tests" ) ;
174+ let documentHandle : String ;
175+ beforeEach ( done => {
176+ collection
177+ . save ( { foo : "bar" } )
178+ . then ( doc => {
179+ documentHandle = doc . _id ;
180+ done ( ) ;
181+ } )
182+ . catch ( done ) ;
183+ } ) ;
184+ it ( "should be executed using the route path" , done => {
185+ db
186+ . route ( `/_api/document/${ documentHandle } ` )
187+ . delete ( )
188+ . then ( res => {
189+ expect ( res ) . to . have . property ( "body" ) ;
190+ expect ( res . body ) . to . have . property ( "_id" ) ;
191+ expect ( res . body ) . to . have . property ( "_key" ) ;
192+ expect ( res . body ) . to . have . property ( "_rev" ) ;
193+ } )
194+ . then ( ( ) => done ( ) )
195+ . catch ( done ) ;
196+ } ) ;
45197 } ) ;
46198 describe ( "route.head" , ( ) => {
47- it ( "is missing tests" ) ;
199+ let documentHandle : String ;
200+ beforeEach ( done => {
201+ collection
202+ . save ( { foo : "bar" } )
203+ . then ( doc => {
204+ documentHandle = doc . _id ;
205+ done ( ) ;
206+ } )
207+ . catch ( done ) ;
208+ } ) ;
209+ it ( "should be executed using the route path" , done => {
210+ db
211+ . route ( `/_api/document/${ documentHandle } ` )
212+ . head ( )
213+ . then ( res => {
214+ expect ( res ) . to . have . property ( "statusCode" , 200 ) ;
215+ } )
216+ . then ( ( ) => done ( ) )
217+ . catch ( done ) ;
218+ } ) ;
48219 } ) ;
49220 describe ( "route.request" , ( ) => {
50- it ( "is missing tests" ) ;
221+ it ( "should be executed using the route path" , done => {
222+ db
223+ . route ( "/_api/version" )
224+ . request ( "get" )
225+ . then ( res => {
226+ expect ( res ) . to . have . property ( "body" ) ;
227+ const body = res . body ;
228+ expect ( body ) . to . have . property ( "version" ) ;
229+ expect ( body ) . to . have . property ( "server" ) ;
230+ } )
231+ . then ( ( ) => done ( ) )
232+ . catch ( done ) ;
233+ } ) ;
51234 } ) ;
52235} ) ;
0 commit comments