GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Rubygem
Description: Resource-oriented open source Ruby framework for Web apps.
Homepage: http://rubywaves.com/
Clone URL: git://github.com/dyoder/waves.git
pattern matching specs updated for The New Way
automatthew (author)
Thu Aug 21 19:54:24 -0700 2008
commit  939cfa2aa52743884698d568302a99499979ac0e
tree    363c1de771912e8971351641ace51b23b6741cda
parent  07889d02b33d4b96e882c9ee963c158c20f3d229
...
6
7
8
9
 
10
11
12
13
14
15
16
17
 
 
 
 
 
 
 
18
19
20
...
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
...
6
7
8
 
9
10
11
12
13
 
 
 
 
14
15
16
17
18
19
20
21
22
23
...
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
0
@@ -6,15 +6,18 @@ describe "In a mapping's path-matcher" do
0
   describe "the HTTP method" do
0
     
0
     before do
0
- mapping.clear
0
+ mappings.clear
0
       handle( Waves::Dispatchers::NotFoundError ) { response.status = 404 }
0
     end
0
   
0
     it "is specified with a hash key" do
0
- mapping.response( :get => [ 'somewhere' ] ) { "GET method" }
0
- mapping.response( :put => [ 'somewhere' ] ) { "PUT method" }
0
- mapping.response( :post => [ 'somewhere' ] ) { "POST method" }
0
- mapping.response( :delete => [ 'somewhere' ] ) { "DELETE method" }
0
+ mappings do
0
+ on( :get => [ 'somewhere' ] ) { "GET method" }
0
+ on( :put => [ 'somewhere' ] ) { "PUT method" }
0
+ on( :post => [ 'somewhere' ] ) { "POST method" }
0
+ on( :delete => [ 'somewhere' ] ) { "DELETE method" }
0
+ end
0
+
0
     
0
       mock_request.get("/somewhere").body.should == "GET method"
0
       mock_request.put("/somewhere").body.should == "PUT method"
0
@@ -25,72 +28,102 @@ describe "In a mapping's path-matcher" do
0
   end
0
   
0
   describe "the request path pattern" do
0
-
0
- before do
0
- mapping.clear
0
- handle( Waves::Dispatchers::NotFoundError ) { response.status = 404 }
0
- end
0
-
0
- it "is an array given as the value of the HTTP method key" do
0
- mapping.response( :get => [ 'foo' ] ) { 'The Foo' }
0
- mapping.response( :get => [ 'bar', 'baz' ] ) { 'The Bar' }
0
       
0
- mock_request.get('/foo').body.should == 'The Foo'
0
- mock_request.get('/bar/baz').body.should == 'The Bar'
0
- end
0
+ before do
0
+ mappings.clear
0
+ handle( Waves::Dispatchers::NotFoundError ) { response.status = 404 }
0
+ end
0
     
0
- it "may be omitted to allow matching all paths" do
0
- mapping.response( {} ) { "pathless" }
0
+ it "is an array given as the value of the HTTP method key" do
0
+ mappings do
0
+ on( :get => [ 'foo' ] ) { 'The Foo' }
0
+ on( :get => [ 'bar', 'baz' ] ) { 'The Bar' }
0
+ end
0
+
0
+ mock_request.get('/foo').body.should == 'The Foo'
0
+ mock_request.get('/bar/baz').body.should == 'The Bar'
0
+ end
0
       
0
- mock_request.get('/bogus').body.should == 'pathless'
0
- end
0
-
0
- it "may match string literals" do
0
- mapping.response( :get => [ 'kilroy', 'was', 'here'] ) { 'Hello from Kilroy' }
0
+ it "may be omitted to allow matching all paths" do
0
+ mappings do
0
+ on( {} ) { "pathless" }
0
+ end
0
+
0
+ mock_request.get('/bogus').body.should == 'pathless'
0
+ end
0
       
0
- mock_request.get('/kilroy/was/here').body.should == 'Hello from Kilroy'
0
+ it "may use true to match all paths, while specifying a method" do
0
+ mappings do
0
+ on( :get => [ true ] ) { "pathless" }
0
+ end
0
+
0
+ mock_request.get('/bogus').body.should == 'pathless'
0
+ end
0
       
0
- # doesn't work for partial matches.
0
- mock_request.get('/kilroy/was').status.should == 404
0
- end
0
-
0
- it "may use symbols as placeholders for a default regex" do
0
- mapping.response( :get => [ :critter, :name ]) { "hi" }
0
+ it "may use true as the last element, to match all remaining components" do
0
+ mappings do
0
+ on( :get => [ "first", true ] ) { "Yes" }
0
+ end
0
+
0
+ mock_request.get('/first/second/third').body.should == "Yes"
0
+ mock_request.get('/uno/dos/tres').status.should == 404
0
+ end
0
       
0
- mock_request.get('/smurf/papa_smurf').status.should == 200
0
+ it "may match string literals" do
0
+ mappings do
0
+ on( :get => [ 'kilroy', 'was', 'here'] ) { 'Hello from Kilroy' }
0
+ end
0
+
0
+ mock_request.get('/kilroy/was/here').body.should == 'Hello from Kilroy'
0
+
0
+ # doesn't work for partial matches.
0
+ mock_request.get('/kilroy/was').status.should == 404
0
+ end
0
       
0
- # The default regex accepts word characters, hyphens, and underscores only.
0
- mock_request.get('/smurf/moo+cow').status.should == 404
0
- end
0
-
0
- # it "may use hashes to specify placeholders with custom regexes" do
0
- # mapping.response( :get => [ :prisoner, { :prisoner_id => /9430|24601/ } ] ) { "I am Jean Valjean!" }
0
- #
0
- # mock_request.get("/prisoner/9430").body.should == "I am Jean Valjean!"
0
- # mock_request.get("/prisoner/9431").status.should == 404
0
- # end
0
-
0
- it "saves placeholder matches as params" do
0
- mapping.response( :get => [ :critter, :name ]) { "#{params['critter']}, #{params['name']}" }
0
+ it "may use symbols as placeholders for a default regex" do
0
+ mappings do
0
+ on( :get => [ :critter, :name ] ) { "hi" }
0
+ end
0
+
0
+ mock_request.get('/smurf/papa_smurf').status.should == 200
0
+
0
+ end
0
       
0
- mock_request.get('/dwarf/tyrion').body.should == "dwarf, tyrion"
0
- end
0
-
0
- it "may use regexes instead of strings or placeholders, but the matches are not saved" do
0
- # extremely bad example. Don't ever really use this for authentication. If somebody thinks up a
0
- # better use case for pattern components that throw away the matches, please fix this spec.
0
- mapping.response( :get => [ "user", /matthew|mark/, "password", /wehttam|kram/ ]) { "you were able to get logged in!!!!"}
0
+ # it "may use hashes to specify placeholders with custom regexes" do
0
+ # mapping.on( :get => [ :prisoner, { :prisoner_id => /9430|24601/ } ] ) { "I am Jean Valjean!" }
0
+ #
0
+ # mock_request.get("/prisoner/9430").body.should == "I am Jean Valjean!"
0
+ # mock_request.get("/prisoner/9431").status.should == 404
0
+ # end
0
       
0
- mock_request.get('/user/matthew/password/wehttam').status.should == 200
0
- mock_request.get('/user/matthew/password/nimda').status.should == 404
0
- end
0
-
0
- it "can apparently also use hashes to set default values" do
0
- mapping.response( :get => [ "view", { :mode => 'show' } ] ) { "mode: #{params['mode']}" }
0
+ it "saves placeholder matches as params" do
0
+ mappings do
0
+ on( :get => [ :critter, :name ]) { "#{params['critter']}, #{params['name']}" }
0
+ end
0
+
0
+ mock_request.get('/dwarf/tyrion').body.should == "dwarf, tyrion"
0
+ end
0
+
0
+ it "may use regexes instead of strings or placeholders, but the matches are not saved" do
0
+ # extremely bad example. Don't ever really use this for authentication. If somebody thinks up a
0
+ # better use case for pattern components that throw away the matches, please fix this spec.
0
+ mappings do
0
+ on( :get => [ "user", /matthew|mark/, "password", /wehttam|kram/ ]) { "you been let in"}
0
+ end
0
+
0
+ mock_request.get('/user/matthew/password/wehttam').status.should == 200
0
+ mock_request.get('/user/matthew/password/nimda').status.should == 404
0
+ end
0
+
0
+ it "can apparently also use hashes to set default values" do
0
+ mappings do
0
+ on( :get => [ "thingy", { :mode => 'show' } ] ) { "mode: #{params['mode']}" }
0
+ end
0
+
0
+ mock_request.get("/thingy").body.should == "mode: show"
0
+ mock_request.get("/thingy/edit").body.should == "mode: edit"
0
+ end
0
       
0
- mock_request.get("/view").body.should == "mode: show"
0
     end
0
-
0
- end
0
   
0
 end

Comments

    No one has commented yet.