1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 module test_commands_http
is test
17 import doc
::commands
::test_commands_catalog
18 import doc
::commands
::commands_http
20 class TestCommandsHttp
21 super TestCommandsCatalog
24 # Http parser to create Http requests
25 var http_parser
= new HttpRequestParser
27 # Create a new and initialized http request from the `url` string
28 fun new_request
(url
: String, route_pattern
: nullable String): HttpRequest do
29 if route_pattern
== null then route_pattern
= "/:id"
30 var route
= new Route(route_pattern
, new TestDummyAction)
31 var req
= http_parser
.parse_http_request
("GET {url} HTTP/1.0")
33 req
.uri_params
= route
.parse_params
(req
.uri
)
39 fun test_cmd_http_entity
is test
do
40 var req
= new_request
("/test_prog::Character")
41 var cmd
= new CmdEntity(test_model
, test_filter
)
42 var res
= cmd
.http_init
(req
)
43 assert res
isa CmdSuccess
44 assert cmd
.mentity
.as(not null).full_name
== "test_prog::Character"
47 fun test_cmd_http_entity_not_found
is test
do
48 var req
= new_request
("/Characterzzz")
49 var cmd
= new CmdEntity(test_model
, test_filter
)
50 var res
= cmd
.http_init
(req
)
51 assert res
isa ErrorMEntityNotFound
52 assert res
.suggestions
.first
.full_name
== "test_prog::Character"
55 fun test_cmd_http_entity_conflict
is test
do
56 var req
= new_request
("/+")
57 var cmd
= new CmdEntity(test_model
, test_filter
)
58 var res
= cmd
.http_init
(req
)
59 assert res
isa ErrorMEntityConflict
60 assert res
.conflicts
.length
== 2
65 fun test_cmd_http_comment
is test
do
66 var req
= new_request
("/test_prog::Character")
67 var cmd
= new CmdComment(test_model
, test_filter
)
68 var res
= cmd
.http_init
(req
)
69 assert res
isa CmdSuccess
70 assert cmd
.mdoc
!= null
73 fun test_cmd_http_comment_no_mdoc
is test
do
74 var req
= new_request
("/test_prog::Character?fallback=false")
75 var cmd
= new CmdComment(test_model
, test_filter
)
76 var res
= cmd
.http_init
(req
)
77 assert res
isa WarningNoMDoc
82 fun test_cmd_http_link
is test
do
83 var req
= new_request
("/test_prog::Character")
84 var cmd
= new CmdEntityLink(test_model
)
85 var res
= cmd
.http_init
(req
)
86 assert res
isa CmdSuccess
87 assert cmd
.text
== "Character"
88 assert cmd
.title
== "Characters can be played by both the human or the machine."
91 fun test_cmd_http_link_with_text
is test
do
92 var req
= new_request
("/test_prog::Character?text=foo")
93 var cmd
= new CmdEntityLink(test_model
)
94 var res
= cmd
.http_init
(req
)
95 assert res
isa CmdSuccess
96 assert cmd
.text
== "foo"
97 assert cmd
.title
== "Characters can be played by both the human or the machine."
100 fun test_cmd_http_link_with_title
is test
do
101 var req
= new_request
("/test_prog::Character?title=bar")
102 var cmd
= new CmdEntityLink(test_model
)
103 var res
= cmd
.http_init
(req
)
104 assert res
isa CmdSuccess
105 assert cmd
.text
== "Character"
106 assert cmd
.title
== "bar"
109 fun test_cmd_http_link_with_text_and_title
is test
do
110 var req
= new_request
("/test_prog::Character?text=foo&title=bar")
111 var cmd
= new CmdEntityLink(test_model
)
112 var res
= cmd
.http_init
(req
)
113 assert res
isa CmdSuccess
114 assert cmd
.text
== "foo"
115 assert cmd
.title
== "bar"
120 fun test_cmd_http_parents
is test
do
121 var req
= new_request
("/test_prog::Warrior")
122 var cmd
= new CmdParents(test_model
, test_main
, test_filter
)
123 var res
= cmd
.http_init
(req
)
124 assert res
isa CmdSuccess
125 assert cmd
.results
.as(not null).length
== 1
128 fun test_cmd_http_ancestors
is test
do
129 var req
= new_request
("/test_prog::Warrior")
130 var cmd
= new CmdAncestors(test_model
, test_main
, test_filter
)
131 var res
= cmd
.http_init
(req
)
132 assert res
isa CmdSuccess
133 assert cmd
.results
.as(not null).length
== 2
136 fun test_cmd_http_ancestorsi_without_parents
is test
do
137 var req
= new_request
("/test_prog::Warrior?parents=false")
138 var cmd
= new CmdAncestors(test_model
, test_main
, test_filter
)
139 var res
= cmd
.http_init
(req
)
140 assert res
isa CmdSuccess
141 assert cmd
.results
.as(not null).length
== 1
144 fun test_cmd_http_children
is test
do
145 var req
= new_request
("/test_prog::Career")
146 var cmd
= new CmdChildren(test_model
, test_main
, test_filter
)
147 var res
= cmd
.http_init
(req
)
148 assert res
isa CmdSuccess
149 assert cmd
.results
.as(not null).length
== 3
152 fun test_cmd_http_descendants
is test
do
153 var req
= new_request
("/test_prog::Career?children=false")
154 var cmd
= new CmdDescendants(test_model
, test_main
, test_filter
)
155 var res
= cmd
.http_init
(req
)
156 assert res
isa CmdSuccess
157 assert cmd
.results
.as(not null).length
== 0
162 fun test_cmd_http_search
is test
do
163 var req
= new_request
("/?q=Carer&l=1")
164 var cmd
= new CmdSearch(test_model
, test_filter
)
165 var res
= cmd
.http_init
(req
)
166 assert res
isa CmdSuccess
167 assert cmd
.results
.as(not null).first
.full_name
== "test_prog::Career"
168 assert cmd
.results
.as(not null).length
== 1
171 fun test_cmd_http_search_no_query
is test
do
172 var req
= new_request
("/")
173 var cmd
= new CmdSearch(test_model
, test_filter
)
174 var res
= cmd
.http_init
(req
)
175 assert res
isa ErrorNoQuery
180 fun test_cmd_http_features
is test
do
181 var req
= new_request
("/test_prog::Character?l=10")
182 var cmd
= new CmdFeatures(test_model
, test_filter
)
183 var res
= cmd
.http_init
(req
)
184 assert res
isa CmdSuccess
185 assert cmd
.results
.as(not null).length
== 10
188 fun test_cmd_http_features_no_features
is test
do
189 var req
= new_request
("/test_prog$Career$strength_bonus?l=10")
190 var cmd
= new CmdFeatures(test_model
, test_filter
)
191 var res
= cmd
.http_init
(req
)
192 assert res
isa WarningNoFeatures
197 fun test_cmd_http_lin
is test
do
198 var req
= new_request
("/init?l=10")
199 var cmd
= new CmdLinearization(test_model
, test_main
, test_filter
)
200 var res
= cmd
.http_init
(req
)
201 assert res
isa CmdSuccess
202 assert cmd
.results
.as(not null).length
== 10
205 fun test_cmd_http_lin_no_lin
is test
do
206 var req
= new_request
("/test_prog?l=10")
207 var cmd
= new CmdLinearization(test_model
, test_main
, test_filter
)
208 var res
= cmd
.http_init
(req
)
209 assert res
isa WarningNoLinearization
214 fun test_cmd_http_code
is test
do
215 var req
= new_request
("/test_prog::Career")
216 var cmd
= new CmdEntityCode(test_model
, test_builder
, test_filter
)
217 var res
= cmd
.http_init
(req
)
218 assert res
isa CmdSuccess
219 assert cmd
.node
isa AStdClassdef
220 assert cmd
.format
== "raw"
223 fun test_cmd_http_code_format
is test
do
224 var req
= new_request
("/test_prog::Career?format=html")
225 var cmd
= new CmdEntityCode(test_model
, test_builder
, test_filter
)
226 var res
= cmd
.http_init
(req
)
227 assert res
isa CmdSuccess
228 assert cmd
.node
isa AStdClassdef
229 assert cmd
.format
== "html"
232 fun test_cmd_http_code_no_code
is test
do
233 var req
= new_request
("/test_prog")
234 var cmd
= new CmdEntityCode(test_model
, test_builder
, test_filter
)
235 var res
= cmd
.http_init
(req
)
236 assert res
isa WarningNoCode
241 fun test_cmd_http_results
is test
do
242 var req
= new_request
("/?kind=modules&l=2")
243 var cmd
= new CmdModelEntities(test_model
, test_filter
, kind
= "modules")
244 var res
= cmd
.http_init
(req
)
245 assert res
isa CmdSuccess
246 assert cmd
.results
.as(not null).length
== 2
249 fun test_cmd_http_results_random
is test
do
250 var req
= new_request
("/?kind=packages&l=1")
251 var cmd
= new CmdRandomEntities(test_model
, test_filter
, kind
= "packages")
252 var res
= cmd
.http_init
(req
)
253 assert res
isa CmdSuccess
254 assert cmd
.results
.as(not null).length
== 1
259 fun test_cmd_http_uml
is test
do
260 var req
= new_request
("/test_prog::Character?format=svg")
261 var cmd
= new CmdUML(test_model
, test_main
, test_filter
)
262 var res
= cmd
.http_init
(req
)
263 assert res
isa CmdSuccess
264 assert cmd
.uml
!= null
265 assert cmd
.format
== "svg"
268 fun test_cmd_http_uml_not_found
is test
do
269 var req
= new_request
("/strength_bonus")
270 var cmd
= new CmdUML(test_model
, test_main
, test_filter
)
271 var res
= cmd
.http_init
(req
)
272 assert res
isa WarningNoUML
273 assert cmd
.format
== "dot"
274 assert cmd
.uml
== null
277 fun test_cmd_http_inh_graph
is test
do
278 var req
= new_request
("/test_prog::Character?pdepth=1&cdepth=1")
279 var cmd
= new CmdInheritanceGraph(test_model
, test_main
, test_filter
)
280 var res
= cmd
.http_init
(req
)
281 assert res
isa CmdSuccess
282 assert cmd
.graph
!= null
283 assert cmd
.pdepth
== 1
284 assert cmd
.cdepth
== 1
289 fun test_cmd_http_catalog_search
is test
do
290 var req
= new_request
("/?q=testprog&l=1")
291 var cmd
= new CmdCatalogSearch(test_model
, test_catalog
, test_filter
)
292 var res
= cmd
.http_init
(req
)
293 assert res
isa CmdSuccess
294 assert cmd
.results
.as(not null).first
.full_name
== "test_prog"
295 assert cmd
.results
.as(not null).first
isa MPackage
296 assert cmd
.results
.as(not null).length
== 1
299 fun test_cmd_http_catalog_tag
is test
do
300 var req
= new_request
("/test", "/:tid")
301 var cmd
= new CmdCatalogTag(test_model
, test_catalog
, test_filter
)
302 var res
= cmd
.http_init
(req
)
303 assert res
isa CmdSuccess
304 assert cmd
.tag
== "test"
305 assert cmd
.results
.as(not null).length
== 1
308 fun test_cmd_http_catalog_person
is test
do
309 var req
= new_request
("/Alexandre%20Terrasa", "/:pid")
310 var cmd
= new CmdCatalogPerson(test_model
, test_catalog
, test_filter
)
311 var res
= cmd
.http_init
(req
)
312 assert res
isa CmdSuccess
313 assert cmd
.person
.as(not null).name
== "Alexandre Terrasa"
317 # Dummy action that does nothing
319 # Used to build the test route / http request.
320 private class TestDummyAction
325 # FIXME linerarization
326 redef fun http_init
(req
) do return super