-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.rs
519 lines (445 loc) · 12 KB
/
url.rs
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
use rhai::{packages::Package, Engine, EvalAltResult};
use rhai_url::UrlPackage;
use url::Url;
/**
* Tests Url()
*/
#[test]
fn test_constructors() {
let mut engine = Engine::new();
let package = UrlPackage::new();
package.register_into_engine(&mut engine);
// it should be an error on empty url
assert!(
engine.eval::<Url>(r#"Url("")"#).is_err(),
"it should be an error on empty url"
);
// it should be ok on a valid url
assert!(
engine.eval::<Url>(r#"Url("http://test.dev/")"#).is_ok(),
"it should be ok on a valid url"
);
// it should be ok on a valid IPv4 url
assert!(
engine.eval::<Url>(r#"Url("http://127.0.0.1/")"#).is_ok(),
"it should be ok on a valid IPv4 url"
);
// it should be ok on a valid IPv6 url
assert!(
engine.eval::<Url>(r#"Url("http://[::1]/")"#).is_ok(),
"it should be ok on a valid IPv6 url"
);
}
/**
* Tests .href, .to_string(), .to_debug()
*/
#[test]
fn test_href_to_string_to_debug() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let package = UrlPackage::new();
package.register_into_engine(&mut engine);
assert_eq!(
engine.eval::<String>(r#"Url("http://test.dev/").href"#)?,
"http://test.dev/",
"it should return the absolute url"
);
assert_eq!(
engine.eval::<String>(r#"Url("http://test.dev/").to_string()"#)?,
"http://test.dev/",
"it should return the absolute url"
);
assert_eq!(
engine.eval::<String>(r#"Url("http://test.dev/").to_debug()"#)?,
"http://test.dev/",
"it should return the absolute url"
);
Ok(())
}
/**
* Tests scheme getter and setter
*/
#[test]
fn test_scheme_getter_setter() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let package = UrlPackage::new();
package.register_into_engine(&mut engine);
assert_eq!(
engine.eval::<String>(r#"Url("http://test.dev").scheme"#)?,
"http",
"it should return 'http'"
);
assert_eq!(
engine.eval::<String>(r#"Url("custom-scheme://test.dev/").scheme"#)?,
"custom-scheme",
"it should return 'custom-scheme'"
);
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/");
url.scheme = "https";
url.scheme
"#
)?,
"https",
"it should set the scheme to 'https'"
);
// NOTE: it might be confusing, however this is actually the implementation from url::Url
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/");
url.scheme = "custom-scheme";
url.scheme
"#
)?,
"http",
"it should refuse to set a 'custom-scheme' when the current scheme is 'http'"
);
Ok(())
}
/**
* Tests domai getter
*/
#[test]
fn test_domain_getter_as_domain_has_no_setter() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let package = UrlPackage::new();
package.register_into_engine(&mut engine);
assert_eq!(
engine.eval::<String>(r#"Url("http://test.dev").domain"#)?,
"test.dev",
"it should return a the domain"
);
assert_eq!(
engine.eval::<String>(r#"Url("http://127.0.0.1").domain"#)?,
"",
"it should return an empty string when the domain is an IPv4"
);
assert_eq!(
engine.eval::<String>(r#"Url("http://test.dev:1234").domain"#)?,
"test.dev",
"it should not include the port"
);
Ok(())
}
/**
* Tests path getter and setter
*/
#[test]
fn test_path_getter_setter() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let package = UrlPackage::new();
package.register_into_engine(&mut engine);
assert_eq!(
engine.eval::<String>(r#"Url("http://test.dev").path"#)?,
"/",
"it should return a forward slash on url without end slash"
);
assert_eq!(
engine.eval::<String>(r#"Url("http://test.dev/").path"#)?,
"/",
"it should return a forward slash on url with no path"
);
assert_eq!(
engine.eval::<String>(r#"Url("http://test.dev/test_path").path"#)?,
"/test_path",
"it should return the path with a leading forward slash"
);
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/test_path");
url.path = "new-path";
url.path
"#
)?,
"/new-path",
"it should return the newly assigned path"
);
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/test_path");
url.path = "";
url.path
"#
)?,
"/",
"it should return the newly assigned path empty path as a forward slash"
);
Ok(())
}
/**
* Tests query getter and setter
*/
#[test]
fn test_query_getter_setter() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let package = UrlPackage::new();
package.register_into_engine(&mut engine);
assert_eq!(
engine.eval::<String>(r#"Url("http://test.dev").query"#)?,
"",
"it should return an empty string when there is no query string"
);
assert_eq!(
engine.eval::<String>(r#"Url("http://test.dev?").query"#)?,
"",
"it should return an empty string when there is no query string but '?' is present"
);
assert_eq!(
engine.eval::<String>(r#"Url("http://test.dev?test_query").query"#)?,
"test_query",
"it should return the query without the leading '?'"
);
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/path");
url.query = "q=test";
url.query
"#
)?,
"q=test",
"it should return the newly assigned query"
);
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/test_query");
url.query = "";
url.query
"#
)?,
"",
"it should return the newly assigned empty query"
);
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/path");
url.query = "";
url.href
"#
)?,
"http://test.dev/path",
"it should not add the '?' when the new query is empty"
);
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/path");
url.query = "q=test";
url.href
"#
)?,
"http://test.dev/path?q=test",
"it should only set the query string"
);
Ok(())
}
/**
* Tests query getter and setter
*/
#[test]
fn test_query_clear_delete_remove() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let package = UrlPackage::new();
package.register_into_engine(&mut engine);
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/?query");
url.query_clear();
url.query
"#
)?,
"",
"it should clear the query"
);
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/?query");
url.query_clear();
url.href
"#
)?,
"http://test.dev/",
"it should remove the '?'"
);
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/?query");
url.query_delete();
url.query
"#
)?,
"",
"it should delete the query when no argument is passed"
);
// query_remove is an alias of query_delete
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/?query");
url.query_remove();
url.query
"#
)?,
"",
"it should delete the query when no argument is passed"
);
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/?q=test&b=preserve");
url.query_delete("q");
url.query
"#
)?,
"b=preserve",
"it should only delete the specified query key"
);
Ok(())
}
/**
* Tests fragment getter and setter
*/
#[test]
fn test_fragment_getter_setter() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let package = UrlPackage::new();
package.register_into_engine(&mut engine);
assert_eq!(
engine.eval::<String>(r#"Url("http://test.dev").fragment"#)?,
"",
"it should return an empty string when there is no fragment string"
);
assert_eq!(
engine.eval::<String>(r#"Url("http://test.dev#").fragment"#)?,
"",
"it should return an empty string when there is no fragment string but '#' is present"
);
assert_eq!(
engine.eval::<String>(r#"Url("http://test.dev#test_fragment").fragment"#)?,
"test_fragment",
"it should return the fragment without the leading '#'"
);
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/path");
url.fragment = "test";
url.fragment
"#
)?,
"test",
"it should return the newly assigned fragment"
);
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/");
url.fragment = "";
url.fragment
"#
)?,
"",
"it should return the newly assigned empty fragment"
);
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/path");
url.fragment = "";
url.href
"#
)?,
"http://test.dev/path",
"it should not add the '#' when the new fragment is empty"
);
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/path");
url.fragment = "test";
url.href
"#
)?,
"http://test.dev/path#test",
"it should only set the fragment string"
);
Ok(())
}
/**
* Tests hash getter and setter, hash and fragment should behave identically
*/
#[test]
fn test_hash_getter_setter_as_fragment_alias() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
let package = UrlPackage::new();
package.register_into_engine(&mut engine);
assert_eq!(
engine.eval::<String>(r#"Url("http://test.dev").hash"#)?,
"",
"it should return an empty string when there is no fragment string"
);
assert_eq!(
engine.eval::<String>(r#"Url("http://test.dev#").hash"#)?,
"",
"it should return an empty string when there is no fragment string but '#' is present"
);
assert_eq!(
engine.eval::<String>(r#"Url("http://test.dev#test_fragment").hash"#)?,
"test_fragment",
"it should return the fragment without the leading '#'"
);
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/path");
url.hash = "test";
url.hash
"#
)?,
"test",
"it should return the newly assigned fragment"
);
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/");
url.hash = "";
url.hash
"#
)?,
"",
"it should return the newly assigned empty fragment"
);
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/path");
url.hash = "";
url.href
"#
)?,
"http://test.dev/path",
"it should not add the '#' when the new fragment is empty"
);
assert_eq!(
engine.eval::<String>(
r#"
let url = Url("http://test.dev/path");
url.hash = "test";
url.href
"#
)?,
"http://test.dev/path#test",
"it should only set the fragment string"
);
Ok(())
}