@@ -7,6 +7,24 @@ class Country < TestResource
77
88end
99
10+ class Pet < TestResource
11+ self . site = "http://example.com/"
12+ self . route_format = :dasherized_route
13+
14+ custom_endpoint :related_pets , on : :member , request_method : :get
15+ custom_endpoint :vip_pets , on : :collection , request_method : :get
16+
17+ end
18+
19+ class MythicBeasts < TestResource
20+ self . site = "http://example.com/"
21+ self . route_format = :camelized_route
22+
23+ custom_endpoint :related_beasts , on : :member , request_method : :get
24+ custom_endpoint :ancient_beasts , on : :collection , request_method : :get
25+
26+ end
27+
1028class CustomEndpointTest < Minitest ::Test
1129
1230 def test_collection_get
@@ -43,4 +61,72 @@ def test_collection_methods_should_not_add_methods_to_all_classes
4361 assert !Class . respond_to? ( :autocomplete ) , "adding a custom method should not add methods to all classes"
4462 end
4563
64+ def test_member_dasherized_route_format_converts_custom_endpoint
65+ stub_request ( :get , "http://example.com/pets/1/related-pets" )
66+ . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
67+ data : [
68+ { id : 1 , type : 'pets' } ,
69+ { id : 2 , type : 'pets' } ,
70+ ]
71+ } . to_json )
72+
73+ pet = Pet . new ( { id : 1 , name : 'Otto' } )
74+ pet . mark_as_persisted!
75+
76+ related_pets = pet . related_pets
77+
78+ assert_equal 2 , related_pets . length
79+ assert_equal [ 1 , 2 ] , related_pets . map ( &:id )
80+ end
81+
82+ def test_collection_dasherized_route_format_converts_custom_endpoint
83+ stub_request ( :get , "http://example.com/pets/vip-pets" )
84+ . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
85+ data : [
86+ { id : 4 , type : 'pets' } ,
87+ { id : 5 , type : 'pets' } ,
88+ { id : 6 , type : 'pets' }
89+ ]
90+ } . to_json )
91+
92+ vip_pets = Pet . vip_pets
93+
94+ assert_equal 3 , vip_pets . length
95+ assert_equal [ 4 , 5 , 6 ] , vip_pets . map ( &:id )
96+ end
97+
98+ def test_member_camelized_route_format_converts_custom_endpoint
99+ stub_request ( :get , "http://example.com/mythicBeasts/1/relatedBeasts" )
100+ . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
101+ data : [
102+ { id : 1 , type : 'mythic-beasts' } ,
103+ { id : 2 , type : 'mythic-beasts' } ,
104+ ]
105+ } . to_json )
106+
107+ dragon = MythicBeasts . new ( { id : 1 , name : 'Dragon' } )
108+ dragon . mark_as_persisted!
109+
110+ related_beasts = dragon . related_beasts
111+
112+ assert_equal 2 , related_beasts . length
113+ assert_equal [ 1 , 2 ] , related_beasts . map ( &:id )
114+ end
115+
116+ def test_collection_camelized_route_format_converts_custom_endpoint
117+ stub_request ( :get , "http://example.com/mythicBeasts/ancientBeasts" )
118+ . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
119+ data : [
120+ { id : 4 , type : 'mythic-beasts' } ,
121+ { id : 5 , type : 'mythic-beasts' } ,
122+ { id : 6 , type : 'mythic-beasts' }
123+ ]
124+ } . to_json )
125+
126+ ancient_beasts = MythicBeasts . ancient_beasts
127+
128+ assert_equal 3 , ancient_beasts . length
129+ assert_equal [ 4 , 5 , 6 ] , ancient_beasts . map ( &:id )
130+ end
131+
46132end
0 commit comments