strelka 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -303,16 +303,9 @@ describe Strelka::App::Routing do
303
303
  "been set up" do
304
304
  # RSpec's "expect {}.to" construct only rescues RuntimeErrors, so we have to do
305
305
  # this ourselves.
306
- begin
306
+ expect {
307
307
  @app.get( '/userinfo/:username' ) {}
308
- rescue ScriptError => err
309
- Strelka.log.error "%p: %s" % [ err.class, err.message ]
310
- :pass
311
- rescue ::Exception => err
312
- fail "Expected to raise a ScriptError, but raised a %p instead" % [ err ]
313
- else
314
- fail "Expected to raise a ScriptError, but nothing was raised."
315
- end
308
+ }.to raise_error( NameError, /no such parameter "username"/i )
316
309
 
317
310
  end
318
311
  end
@@ -75,7 +75,6 @@ describe Strelka::HTTPRequest do
75
75
  it "can get and set notes for communication between plugins" do
76
76
  @req.notes.should be_a( Hash )
77
77
  @req.notes[:routing].should be_a( Hash )
78
- @req.notes[:routing][:route].should be_a( Hash )
79
78
  end
80
79
 
81
80
  it "can redirect the request to a different URI" do
@@ -198,6 +197,11 @@ describe Strelka::HTTPRequest do
198
197
  'mirror' => 'sequel',
199
198
  }
200
199
  end
200
+
201
+ it "treats a malformed query string as the lack of a query" do
202
+ req = @request_factory.get( '/directory/path?foo' )
203
+ req.params.should == {}
204
+ end
201
205
  end
202
206
 
203
207
 
@@ -57,6 +57,13 @@ describe Strelka::ParamValidator do
57
57
  @validator.param_names.should include( 'a_field' )
58
58
  end
59
59
 
60
+ it "revalidates parameters when new constraints are added" do
61
+ @validator.validate( 'blorp' => 'true' )
62
+ @validator[ :blorp ].should be_nil
63
+ @validator.add( :blorp, :boolean )
64
+ @validator[ :blorp ].should be_true
65
+ end
66
+
60
67
  it "ignores identical duplicate constraints to be added twice" do
61
68
  @validator.add( :a_field, :string )
62
69
  @validator.add( :a_field, :string )
@@ -117,7 +124,6 @@ describe Strelka::ParamValidator do
117
124
  end
118
125
 
119
126
  it "handles multiple values for the same parameter" do
120
- pending "issue #1"
121
127
  @validator.add( :foo, /^\d+$/, :multiple )
122
128
 
123
129
  @validator.validate( {'foo' => %w[1 2]} )
@@ -125,7 +131,6 @@ describe Strelka::ParamValidator do
125
131
  end
126
132
 
127
133
  it "always returns an Array for parameters marked as :multiple" do
128
- pending "issue #1"
129
134
  @validator.add( :foo, /^\d+$/, :multiple )
130
135
 
131
136
  @validator.validate( {'foo' => '1'} )
@@ -133,7 +138,6 @@ describe Strelka::ParamValidator do
133
138
  end
134
139
 
135
140
  it "fails to validate if one of a multiple-value parameter doesn't validate" do
136
- pending "issue #1"
137
141
  @validator.add( :foo, /^\d+$/, :multiple )
138
142
 
139
143
  @validator.validate( {'foo' => %[1 victor 8]} )
@@ -356,9 +360,9 @@ describe Strelka::ParamValidator do
356
360
 
357
361
  end # describe "hash parameters"
358
362
 
359
- describe "constraints" do
363
+ describe "builtin constraint type" do
360
364
 
361
- it "treats ArgumentErrors in builtin constraints as validation failures" do
365
+ it "treats ArgumentErrors as validation failures" do
362
366
  @validator.add( :integer )
363
367
  @validator.validate( 'integer' => 'jalopy' )
364
368
  @validator.should_not be_okay()
@@ -366,21 +370,21 @@ describe Strelka::ParamValidator do
366
370
  @validator[:integer].should be_nil()
367
371
  end
368
372
 
369
- describe "Regexp constraints" do
373
+ describe "Regexp" do
370
374
 
371
- it "returns the capture from a regexp constraint if it has only one" do
375
+ it "returns the capture if it has only one" do
372
376
  @validator.add( :treename, /(\w+)/ )
373
377
  @validator.validate( 'treename' => " ygdrassil " )
374
378
  @validator[:treename].should == 'ygdrassil'
375
379
  end
376
380
 
377
- it "returns the captures from a regexp constraint as an array if it has more than one" do
381
+ it "returns the captures as an array if it has more than one" do
378
382
  @validator.add( :stuff, /(\w+)(\S+)?/ )
379
383
  @validator.validate( 'stuff' => " the1tree(!) " )
380
384
  @validator[:stuff].should == ['the1tree', '(!)']
381
385
  end
382
386
 
383
- it "returns the captures from a regexp constraint with named captures as a Hash" do
387
+ it "returns the captures with named captures as a Hash" do
384
388
  @validator.add( :order_number, /(?<category>[[:upper:]]{3})-(?<sku>\d{12})/, :untaint )
385
389
  @validator.validate( 'order_number' => " JVV-886451300133 ".taint )
386
390
 
@@ -389,7 +393,7 @@ describe Strelka::ParamValidator do
389
393
  @validator[:order_number][:sku].should_not be_tainted()
390
394
  end
391
395
 
392
- it "returns the captures from a regexp constraint as an array " +
396
+ it "returns the captures as an array " +
393
397
  "even if an optional capture doesn't match anything" do
394
398
  @validator.add( :amount, /^([\-+])?(\d+(?:\.\d+)?)/ )
395
399
  @validator.validate( 'amount' => '2.28' )
@@ -405,7 +409,7 @@ describe Strelka::ParamValidator do
405
409
  @validator.add( :enabled, :boolean )
406
410
  end
407
411
 
408
- it "accepts the value 'true' for fields with boolean constraints" do
412
+ it "accepts the value 'true'" do
409
413
  @validator.validate( 'enabled' => 'true' )
410
414
 
411
415
  @validator.should be_okay()
@@ -414,7 +418,7 @@ describe Strelka::ParamValidator do
414
418
  @validator[:enabled].should be_true()
415
419
  end
416
420
 
417
- it "accepts the value 't' for fields with boolean constraints" do
421
+ it "accepts the value 't'" do
418
422
  @validator.validate( 'enabled' => 't' )
419
423
 
420
424
  @validator.should be_okay()
@@ -423,7 +427,7 @@ describe Strelka::ParamValidator do
423
427
  @validator[:enabled].should be_true()
424
428
  end
425
429
 
426
- it "accepts the value 'yes' for fields with boolean constraints" do
430
+ it "accepts the value 'yes'" do
427
431
  @validator.validate( 'enabled' => 'yes' )
428
432
 
429
433
  @validator.should be_okay()
@@ -432,7 +436,7 @@ describe Strelka::ParamValidator do
432
436
  @validator[:enabled].should be_true()
433
437
  end
434
438
 
435
- it "accepts the value 'y' for fields with boolean constraints" do
439
+ it "accepts the value 'y'" do
436
440
  @validator.validate( 'enabled' => 'y' )
437
441
 
438
442
  @validator.should be_okay()
@@ -441,7 +445,7 @@ describe Strelka::ParamValidator do
441
445
  @validator[:enabled].should be_true()
442
446
  end
443
447
 
444
- it "accepts the value '1' for fields with boolean constraints" do
448
+ it "accepts the value '1'" do
445
449
  @validator.validate( 'enabled' => '1' )
446
450
 
447
451
  @validator.should be_okay()
@@ -450,7 +454,7 @@ describe Strelka::ParamValidator do
450
454
  @validator[:enabled].should be_true()
451
455
  end
452
456
 
453
- it "accepts the value 'false' for fields with boolean constraints" do
457
+ it "accepts the value 'false'" do
454
458
  @validator.validate( 'enabled' => 'false' )
455
459
 
456
460
  @validator.should be_okay()
@@ -459,7 +463,7 @@ describe Strelka::ParamValidator do
459
463
  @validator[:enabled].should be_false()
460
464
  end
461
465
 
462
- it "accepts the value 'f' for fields with boolean constraints" do
466
+ it "accepts the value 'f'" do
463
467
  @validator.validate( 'enabled' => 'f' )
464
468
 
465
469
  @validator.should be_okay()
@@ -468,7 +472,7 @@ describe Strelka::ParamValidator do
468
472
  @validator[:enabled].should be_false()
469
473
  end
470
474
 
471
- it "accepts the value 'no' for fields with boolean constraints" do
475
+ it "accepts the value 'no'" do
472
476
  @validator.validate( 'enabled' => 'no' )
473
477
 
474
478
  @validator.should be_okay()
@@ -477,7 +481,7 @@ describe Strelka::ParamValidator do
477
481
  @validator[:enabled].should be_false()
478
482
  end
479
483
 
480
- it "accepts the value 'n' for fields with boolean constraints" do
484
+ it "accepts the value 'n'" do
481
485
  @validator.validate( 'enabled' => 'n' )
482
486
 
483
487
  @validator.should be_okay()
@@ -486,7 +490,7 @@ describe Strelka::ParamValidator do
486
490
  @validator[:enabled].should be_false()
487
491
  end
488
492
 
489
- it "accepts the value '0' for fields with boolean constraints" do
493
+ it "accepts the value '0'" do
490
494
  @validator.validate( 'enabled' => '0' )
491
495
 
492
496
  @validator.should be_okay()
@@ -495,7 +499,7 @@ describe Strelka::ParamValidator do
495
499
  @validator[:enabled].should be_false()
496
500
  end
497
501
 
498
- it "rejects non-boolean parameters for fields with boolean constraints" do
502
+ it "rejects non-boolean parameters" do
499
503
  @validator.validate( 'enabled' => 'peanut' )
500
504
 
501
505
  @validator.should_not be_okay()
@@ -508,7 +512,7 @@ describe Strelka::ParamValidator do
508
512
 
509
513
  describe ":integer constraints" do
510
514
 
511
- it "accepts simple integers for fields with integer constraints" do
515
+ it "accepts simple integers" do
512
516
  @validator.add( :count, :integer )
513
517
  @validator.validate( 'count' => '11' )
514
518
 
@@ -518,7 +522,7 @@ describe Strelka::ParamValidator do
518
522
  @validator[:count].should == 11
519
523
  end
520
524
 
521
- it "accepts '0' for fields with integer constraints" do
525
+ it "accepts '0'" do
522
526
  @validator.add( :count, :integer )
523
527
  @validator.validate( 'count' => '0' )
524
528
 
@@ -528,7 +532,7 @@ describe Strelka::ParamValidator do
528
532
  @validator[:count].should == 0
529
533
  end
530
534
 
531
- it "accepts negative integers for fields with integer constraints" do
535
+ it "accepts negative integers" do
532
536
  @validator.add( :count, :integer )
533
537
  @validator.validate( 'count' => '-407' )
534
538
 
@@ -538,7 +542,7 @@ describe Strelka::ParamValidator do
538
542
  @validator[:count].should == -407
539
543
  end
540
544
 
541
- it "rejects non-integers for fields with integer constraints" do
545
+ it "rejects non-integers" do
542
546
  @validator.add( :count, :integer )
543
547
  @validator.validate( 'count' => '11.1' )
544
548
 
@@ -548,7 +552,7 @@ describe Strelka::ParamValidator do
548
552
  @validator[:count].should be_nil()
549
553
  end
550
554
 
551
- it "rejects integer values with other cruft in them for fields with integer constraints" do
555
+ it "rejects integer values with other cruft in them" do
552
556
  @validator.add( :count, :integer )
553
557
  @validator.validate( 'count' => '88licks' )
554
558
 
@@ -566,7 +570,7 @@ describe Strelka::ParamValidator do
566
570
  @validator.add( :amount, :float )
567
571
  end
568
572
 
569
- it "accepts simple floats for fields with float constraints" do
573
+ it "accepts simple floats" do
570
574
  @validator.validate( 'amount' => '3.14' )
571
575
 
572
576
  @validator.should be_okay()
@@ -575,7 +579,7 @@ describe Strelka::ParamValidator do
575
579
  @validator[:amount].should == 3.14
576
580
  end
577
581
 
578
- it "accepts negative floats for fields with float constraints" do
582
+ it "accepts negative floats" do
579
583
  @validator.validate( 'amount' => '-3.14' )
580
584
 
581
585
  @validator.should be_okay()
@@ -584,7 +588,7 @@ describe Strelka::ParamValidator do
584
588
  @validator[:amount].should == -3.14
585
589
  end
586
590
 
587
- it "accepts positive floats for fields with float constraints" do
591
+ it "accepts positive floats" do
588
592
  @validator.validate( 'amount' => '+3.14' )
589
593
 
590
594
  @validator.should be_okay()
@@ -593,7 +597,7 @@ describe Strelka::ParamValidator do
593
597
  @validator[:amount].should == 3.14
594
598
  end
595
599
 
596
- it "accepts floats that begin with '.' for fields with float constraints" do
600
+ it "accepts floats that begin with '.'" do
597
601
  @validator.validate( 'amount' => '.1418' )
598
602
 
599
603
  @validator.should be_okay()
@@ -602,7 +606,7 @@ describe Strelka::ParamValidator do
602
606
  @validator[:amount].should == 0.1418
603
607
  end
604
608
 
605
- it "accepts negative floats that begin with '.' for fields with float constraints" do
609
+ it "accepts negative floats that begin with '.'" do
606
610
  @validator.validate( 'amount' => '-.171' )
607
611
 
608
612
  @validator.should be_okay()
@@ -611,7 +615,7 @@ describe Strelka::ParamValidator do
611
615
  @validator[:amount].should == -0.171
612
616
  end
613
617
 
614
- it "accepts positive floats that begin with '.' for fields with float constraints" do
618
+ it "accepts positive floats that begin with '.'" do
615
619
  @validator.validate( 'amount' => '+.86668001' )
616
620
 
617
621
  @validator.should be_okay()
@@ -620,7 +624,7 @@ describe Strelka::ParamValidator do
620
624
  @validator[:amount].should == 0.86668001
621
625
  end
622
626
 
623
- it "accepts floats in exponential notation for fields with float constraints" do
627
+ it "accepts floats in exponential notation" do
624
628
  @validator.validate( 'amount' => '1756e-5' )
625
629
 
626
630
  @validator.should be_okay()
@@ -629,7 +633,7 @@ describe Strelka::ParamValidator do
629
633
  @validator[:amount].should == 1756e-5
630
634
  end
631
635
 
632
- it "accepts negative floats in exponential notation for fields with float constraints" do
636
+ it "accepts negative floats in exponential notation" do
633
637
  @validator.validate( 'amount' => '-28e8' )
634
638
 
635
639
  @validator.should be_okay()
@@ -638,8 +642,7 @@ describe Strelka::ParamValidator do
638
642
  @validator[:amount].should == -28e8
639
643
  end
640
644
 
641
- it "accepts floats that start with '.' in exponential notation for fields with float " +
642
- "constraints" do
645
+ it "accepts floats that start with '.' in exponential notation" do
643
646
  @validator.validate( 'amount' => '.5552e-10' )
644
647
 
645
648
  @validator.should be_okay()
@@ -648,8 +651,7 @@ describe Strelka::ParamValidator do
648
651
  @validator[:amount].should == 0.5552e-10
649
652
  end
650
653
 
651
- it "accepts negative floats that start with '.' in exponential notation for fields with " +
652
- "float constraints" do
654
+ it "accepts negative floats that start with '.' in exponential notation" do
653
655
  @validator.validate( 'amount' => '-.288088e18' )
654
656
 
655
657
  @validator.should be_okay()
@@ -658,7 +660,7 @@ describe Strelka::ParamValidator do
658
660
  @validator[:amount].should == -0.288088e18
659
661
  end
660
662
 
661
- it "accepts integers for fields with float constraints" do
663
+ it "accepts integers" do
662
664
  @validator.validate( 'amount' => '288' )
663
665
 
664
666
  @validator.should be_okay()
@@ -667,7 +669,7 @@ describe Strelka::ParamValidator do
667
669
  @validator[:amount].should == 288.0
668
670
  end
669
671
 
670
- it "accepts negative integers for fields with float constraints" do
672
+ it "accepts negative integers" do
671
673
  @validator.validate( 'amount' => '-1606' )
672
674
 
673
675
  @validator.should be_okay()
@@ -676,7 +678,7 @@ describe Strelka::ParamValidator do
676
678
  @validator[:amount].should == -1606.0
677
679
  end
678
680
 
679
- it "accepts positive integers for fields with float constraints" do
681
+ it "accepts positive integers" do
680
682
  @validator.validate( 'amount' => '2600' )
681
683
 
682
684
  @validator.should be_okay()
@@ -693,7 +695,7 @@ describe Strelka::ParamValidator do
693
695
  @validator.add( :expires, :date )
694
696
  end
695
697
 
696
- it "accepts dates for fields with date constraints" do
698
+ it "accepts dates" do
697
699
  @validator.validate( 'expires' => '2008-11-18' )
698
700
 
699
701
  @validator.should be_okay()
@@ -702,7 +704,7 @@ describe Strelka::ParamValidator do
702
704
  @validator[:expires].should == Date.parse( '2008-11-18' )
703
705
  end
704
706
 
705
- it "rejects non-dates for fields with date constraints" do
707
+ it "rejects non-dates" do
706
708
  @validator.validate( 'expires' => 'Mexico' )
707
709
 
708
710
  @validator.should_not be_okay()
@@ -780,7 +782,7 @@ describe Strelka::ParamValidator do
780
782
  end
781
783
 
782
784
  VALID_URIS.each do |uri_string|
783
- it "accepts #{uri_string} for fields with URI constraints" do
785
+ it "accepts #{uri_string}" do
784
786
  @validator.validate( 'homepage' => uri_string )
785
787
 
786
788
  @validator.should be_okay()
@@ -792,7 +794,7 @@ describe Strelka::ParamValidator do
792
794
  end
793
795
 
794
796
  INVALID_URIS.each do |uri_string|
795
- it "rejects #{uri_string} for fields with URI constraints" do
797
+ it "rejects #{uri_string}" do
796
798
  @validator.validate( 'homepage' => uri_string )
797
799
 
798
800
  @validator.should_not be_okay()
@@ -820,7 +822,7 @@ describe Strelka::ParamValidator do
820
822
  'j:random@rubyhacquer.com',
821
823
  ]
822
824
 
823
- it "accepts simple RFC822 addresses for fields with email constraints" do
825
+ it "accepts simple RFC822 addresses" do
824
826
  @validator.add( :email )
825
827
  @validator.validate( 'email' => 'jrandom@hacker.ie' )
826
828
 
@@ -830,7 +832,7 @@ describe Strelka::ParamValidator do
830
832
  @validator[:email].should == 'jrandom@hacker.ie'
831
833
  end
832
834
 
833
- it "accepts hyphenated domains in RFC822 addresses for fields with email constraints" do
835
+ it "accepts hyphenated domains in RFC822 addresses" do
834
836
  @validator.add( :email )
835
837
  @validator.validate( 'email' => 'jrandom@just-another-hacquer.fr' )
836
838
 
@@ -841,7 +843,7 @@ describe Strelka::ParamValidator do
841
843
  end
842
844
 
843
845
  COMPLEX_ADDRESSES.each do |addy|
844
- it "accepts #{addy} for fields with email constraints" do
846
+ it "accepts #{addy}" do
845
847
  @validator.add( :mail, :email )
846
848
  @validator.validate( 'mail' => addy )
847
849
 
@@ -853,7 +855,7 @@ describe Strelka::ParamValidator do
853
855
  end
854
856
 
855
857
  BOGUS_ADDRESSES.each do |addy|
856
- it "rejects #{addy} for fields with email constraints" do
858
+ it "rejects #{addy}" do
857
859
  @validator.add( :mail, :email )
858
860
  @validator.validate( 'mail' => addy )
859
861
 
@@ -876,7 +878,7 @@ describe Strelka::ParamValidator do
876
878
  'indus«tree».com',
877
879
  ]
878
880
 
879
- it "accepts simple hostnames for fields with hostname constraints" do
881
+ it "accepts simple hostnames" do
880
882
  @validator.add( :host, :hostname )
881
883
  @validator.validate( 'host' => 'deveiate.org' )
882
884
 
@@ -886,7 +888,7 @@ describe Strelka::ParamValidator do
886
888
  @validator[:host].should == 'deveiate.org'
887
889
  end
888
890
 
889
- it "accepts hyphenated hostnames for fields with hostname constraints" do
891
+ it "accepts hyphenated hostnames" do
890
892
  @validator.add( :hostname )
891
893
  @validator.validate( 'hostname' => 'your-characters-can-fly.kr' )
892
894
 
@@ -910,9 +912,9 @@ describe Strelka::ParamValidator do
910
912
 
911
913
  end
912
914
 
913
- describe ":alpha constraints" do
915
+ describe ":alpha constraint" do
914
916
 
915
- it "accepts alpha characters for fields with alpha constraints" do
917
+ it "accepts alpha characters" do
916
918
  @validator.add( :alpha )
917
919
  @validator.validate( 'alpha' => 'abelincoln' )
918
920
 
@@ -922,7 +924,7 @@ describe Strelka::ParamValidator do
922
924
  @validator[:alpha].should == 'abelincoln'
923
925
  end
924
926
 
925
- it "rejects non-alpha characters for fields with alpha constraints" do
927
+ it "rejects non-alpha characters" do
926
928
  @validator.add( :alpha )
927
929
  @validator.validate( 'alpha' => 'duck45' )
928
930
 
@@ -934,7 +936,7 @@ describe Strelka::ParamValidator do
934
936
 
935
937
  end
936
938
 
937
- describe ":uuid constraints" do
939
+ describe ":uuid constraint" do
938
940
 
939
941
  it "accepts valid UUIDs without regard to case" do
940
942
  @validator.add( :uuid )
@@ -958,10 +960,9 @@ describe Strelka::ParamValidator do
958
960
 
959
961
  end
960
962
 
963
+ describe ":alphanumeric constraint" do
961
964
 
962
- describe ":alphanumeric constraints" do
963
-
964
- it "accepts alphanumeric characters for fields with alphanumeric constraints" do
965
+ it "accepts alphanumeric characters" do
965
966
  @validator.add( :username, :alphanumeric )
966
967
  @validator.validate( 'username' => 'zombieabe11' )
967
968
 
@@ -971,7 +972,7 @@ describe Strelka::ParamValidator do
971
972
  @validator[:username].should == 'zombieabe11'
972
973
  end
973
974
 
974
- it "rejects non-alphanumeric characters for fields with alphanumeric constraints" do
975
+ it "rejects non-alphanumeric characters" do
975
976
  @validator.add( :username, :alphanumeric )
976
977
  @validator.validate( 'username' => 'duck!ling' )
977
978
 
@@ -983,9 +984,9 @@ describe Strelka::ParamValidator do
983
984
 
984
985
  end
985
986
 
986
- describe ":printable constraints" do
987
+ describe ":printable constraint" do
987
988
 
988
- it "accepts printable characters for fields with 'printable' constraints" do
989
+ it "accepts printable characters" do
989
990
  test_content = <<-EOF
990
991
  I saw you with some kind of medical apparatus strapped to your
991
992
  spine. It was all glass and metal, a great crystaline hypodermic
@@ -999,7 +1000,7 @@ describe Strelka::ParamValidator do
999
1000
  @validator[:prologue].should == test_content
1000
1001
  end
1001
1002
 
1002
- it "rejects non-printable characters for fields with 'printable' constraints" do
1003
+ it "rejects non-printable characters" do
1003
1004
  @validator.add( :prologue, :printable )
1004
1005
  @validator.validate( 'prologue' => %{\0Something cold\0} )
1005
1006
 
@@ -1013,7 +1014,7 @@ describe Strelka::ParamValidator do
1013
1014
 
1014
1015
  describe ":word constraints" do
1015
1016
 
1016
- it "accepts any word characters for fields with 'word' constraints" do
1017
+ it "accepts any word characters" do
1017
1018
  @validator.add( :vocab_word, :word )
1018
1019
  @validator.validate( 'vocab_word' => "Собака" )
1019
1020
 
@@ -1023,7 +1024,7 @@ describe Strelka::ParamValidator do
1023
1024
  @validator[:vocab_word].should == "Собака"
1024
1025
  end
1025
1026
 
1026
- it "rejects non-word characters for fields with 'word' constraints" do
1027
+ it "rejects non-word characters" do
1027
1028
  @validator.add( :vocab_word, :word )
1028
1029
  @validator.validate( 'vocab_word' => "Собака!" )
1029
1030
 
@@ -1037,7 +1038,7 @@ describe Strelka::ParamValidator do
1037
1038
 
1038
1039
  describe "Proc constraints" do
1039
1040
 
1040
- it "accepts parameters for fields with Proc constraints if the Proc returns a true value" do
1041
+ it "accepts parameters if the Proc returns a true value" do
1041
1042
  test_date = '2007-07-17'
1042
1043
 
1043
1044
  @validator.add( :creation_date ) do |input|
@@ -1051,7 +1052,7 @@ describe Strelka::ParamValidator do
1051
1052
  @validator[:creation_date].should == Date.parse( test_date )
1052
1053
  end
1053
1054
 
1054
- it "rejects parameters for fields with Proc constraints if the Proc returns a false value" do
1055
+ it "rejects parameters if the Proc returns a false value" do
1055
1056
  @validator.add( :creation_date ) do |input|
1056
1057
  Date.parse( input ) rescue nil
1057
1058
  end
@@ -1119,6 +1120,232 @@ describe Strelka::ParamValidator do
1119
1120
 
1120
1121
  end
1121
1122
 
1123
+ describe ":json constraint" do
1124
+
1125
+ # Some tests derived from the json-smart feature test matrix by Uriel Chemouni:
1126
+ # http://code.google.com/p/json-smart/wiki/FeaturesTests
1127
+ # Used under the terms of the Apache license
1128
+
1129
+ it "accepts an empty object" do
1130
+ json = %Q<{}>
1131
+ @validator.add( :object, :json )
1132
+ @validator.validate( 'object' => json )
1133
+
1134
+ @validator.should be_okay()
1135
+ @validator[:object].should == json
1136
+ end
1137
+
1138
+ it "accepts a simple object string value" do
1139
+ json = %Q<{ "v":"1"}>
1140
+ @validator.add( :object, :json )
1141
+ @validator.validate( 'object' => json )
1142
+
1143
+ @validator.should be_okay()
1144
+ @validator[:object].should == json
1145
+ end
1146
+
1147
+ it "accepts whitespace in a value" do
1148
+ json = %Q<{\t"v":"1"\r\n}>
1149
+ @validator.add( :object, :json )
1150
+ @validator.validate( 'object' => json )
1151
+
1152
+ @validator.should be_okay()
1153
+ @validator[:object].should == json
1154
+ end
1155
+
1156
+ it "accepts a simple object integer value" do
1157
+ json = %Q<{ "v":1}>
1158
+ @validator.add( :object, :json )
1159
+ @validator.validate( 'object' => json )
1160
+
1161
+ @validator.should be_okay()
1162
+ @validator[:object].should == json
1163
+ end
1164
+
1165
+ it "accepts a single quote in a string value" do
1166
+ json = %Q<{ "v":"ab'c"}>
1167
+ @validator.add( :object, :json )
1168
+ @validator.validate( 'object' => json )
1169
+
1170
+ @validator.should be_okay()
1171
+ @validator[:object].should == json
1172
+ end
1173
+
1174
+ it "accepts a exponented float value" do
1175
+ json = %Q<{ "PI":3.141E-10}>
1176
+ @validator.add( :object, :json )
1177
+ @validator.validate( 'object' => json )
1178
+
1179
+ @validator.should be_okay()
1180
+ @validator[:object].should == json
1181
+ end
1182
+
1183
+ it "accepts a lowercase exponented value" do
1184
+ json = %Q<{ "PI":3.141e-10}>
1185
+ @validator.add( :object, :json )
1186
+ @validator.validate( 'object' => json )
1187
+
1188
+ @validator.should be_okay()
1189
+ @validator[:object].should == json
1190
+ end
1191
+
1192
+ it "accepts a long number value" do
1193
+ json = %Q<{ "v":12345123456789}>
1194
+ @validator.add( :object, :json )
1195
+ @validator.validate( 'object' => json )
1196
+
1197
+ @validator.should be_okay()
1198
+ @validator[:object].should == json
1199
+ end
1200
+
1201
+ it "accepts a Bigint value" do
1202
+ json = %Q<{ "v":123456789123456789123456789}>
1203
+ @validator.add( :object, :json )
1204
+ @validator.validate( 'object' => json )
1205
+
1206
+ @validator.should be_okay()
1207
+ @validator[:object].should == json
1208
+ end
1209
+
1210
+ it "accepts a simple digit array value" do
1211
+ json = %Q<[1,2,3,4]>
1212
+ @validator.add( :object, :json )
1213
+ @validator.validate( 'object' => json )
1214
+
1215
+ @validator.should be_okay()
1216
+ @validator[:object].should == json
1217
+ end
1218
+
1219
+ it "accepts a simple string array value" do
1220
+ json = %Q<[ "1","2","3","4"]>
1221
+ @validator.add( :object, :json )
1222
+ @validator.validate( 'object' => json )
1223
+
1224
+ @validator.should be_okay()
1225
+ @validator[:object].should == json
1226
+ end
1227
+
1228
+ it "accepts an array of empty values" do
1229
+ json = %Q<[ { }, { },[]]>
1230
+ @validator.add( :object, :json )
1231
+ @validator.validate( 'object' => json )
1232
+
1233
+ @validator.should be_okay()
1234
+ @validator[:object].should == json
1235
+ end
1236
+
1237
+ it "accepts lowercase Unicode text values" do
1238
+ json = %Q<{ "v":"\\u2000\\u20ff"}>
1239
+ @validator.add( :object, :json )
1240
+ @validator.validate( 'object' => json )
1241
+
1242
+ @validator.should be_okay()
1243
+ @validator[:object].should == json
1244
+ end
1245
+
1246
+ it "accepts a uppercase Unicode value" do
1247
+ json = %Q<{ "v":"\\u2000\\u20FF"}>
1248
+ @validator.add( :object, :json )
1249
+ @validator.validate( 'object' => json )
1250
+
1251
+ @validator.should be_okay()
1252
+ @validator[:object].should == json
1253
+ end
1254
+
1255
+ it "accepts an escaped backslash value" do
1256
+ json = %Q<{ "a":"\\\\\\\\main\\\\accounts"}>
1257
+ @validator.add( :object, :json )
1258
+ @validator.validate( 'object' => json )
1259
+
1260
+ @validator.should be_okay()
1261
+ @validator[:object].should == json
1262
+ end
1263
+
1264
+ it "accepts null values" do
1265
+ json = %Q<{ "a":null}>
1266
+ @validator.add( :object, :json )
1267
+ @validator.validate( 'object' => json )
1268
+
1269
+ @validator.should be_okay()
1270
+ @validator[:object].should == json
1271
+ end
1272
+
1273
+ it "accepts a boolean true value" do
1274
+ json = %Q<{ "a":true}>
1275
+ @validator.add( :object, :json )
1276
+ @validator.validate( 'object' => json )
1277
+
1278
+ @validator.should be_okay()
1279
+ @validator[:object].should == json
1280
+ end
1281
+
1282
+ it "accepts a boolean value with whitespace" do
1283
+ json = %Q<{ "a" : true }>
1284
+ @validator.add( :object, :json )
1285
+ @validator.validate( 'object' => json )
1286
+
1287
+ @validator.should be_okay()
1288
+ @validator[:object].should == json
1289
+ end
1290
+
1291
+ it "accepts a double-precision floating value" do
1292
+ json = %Q<{ "v":1.7976931348623157E308}>
1293
+ @validator.add( :object, :json )
1294
+ @validator.validate( 'object' => json )
1295
+
1296
+ @validator.should be_okay()
1297
+ @validator[:object].should == json
1298
+ end
1299
+
1300
+
1301
+
1302
+ it "rejects a single brace" do
1303
+ json = '{'
1304
+ @validator.add( :object, :json )
1305
+ @validator.validate( 'object' => json )
1306
+
1307
+ @validator.should_not be_okay()
1308
+ @validator.should have_errors()
1309
+
1310
+ @validator[:object].should be_nil()
1311
+ end
1312
+
1313
+ it "rejects a truncated Object value" do
1314
+ json = %Q<{'X':'s>
1315
+ @validator.add( :object, :json )
1316
+ @validator.validate( 'object' => json )
1317
+
1318
+ @validator.should_not be_okay()
1319
+ @validator.should have_errors()
1320
+
1321
+ @validator[:object].should be_nil()
1322
+ end
1323
+
1324
+ it "rejects a truncated String value" do
1325
+ json = %Q<{'X>
1326
+ @validator.add( :object, :json )
1327
+ @validator.validate( 'object' => json )
1328
+
1329
+ @validator.should_not be_okay()
1330
+ @validator.should have_errors()
1331
+
1332
+ @validator[:object].should be_nil()
1333
+ end
1334
+
1335
+ it "rejects unescaped control characters in a String value" do
1336
+ json = %Q<["this\nis\nbroken\0"]>
1337
+ @validator.add( :object, :json )
1338
+ @validator.validate( 'object' => json )
1339
+
1340
+ @validator.should_not be_okay()
1341
+ @validator.should have_errors()
1342
+
1343
+ @validator[:object].should be_nil()
1344
+ end
1345
+ end
1346
+
1347
+
1348
+
1122
1349
  end # describe "constraints"
1123
1350
 
1124
1351
  end