2626 let ( :response_page1 ) { double ( "response_page1" , body : response_body_page1 ) }
2727
2828 before do
29- allow ( subject ) . to receive ( :get_response ) . with ( " test_resources" ) . and_return ( response_page1 )
29+ stub_json_request ( :get , %r{ test_resources} , json ( response_body_page1 ) )
3030 end
3131
3232 context "when CBP is used" do
3333 it "is empty on the first page" do
34- subject . fetch
35-
36- expect ( subject . prev ) . to eq ( [ ] )
34+ stub_const ( "ZendeskAPI::TestResource::CBP_ACTIONS" , %w[ test_resources ] )
35+ collection = ZendeskAPI :: Collection . new ( client , ZendeskAPI :: TestResource )
36+ expect ( collection . prev ) . to eq ( [ ] )
3737 end
3838 end
3939 end
@@ -543,6 +543,34 @@ def to_proc
543543 expect ( subject . fetch ( true ) ) . to be_empty
544544 end
545545 end
546+
547+ context "when the request returns a Cursor Based Pagination type of response" do
548+ let ( :cbp_response ) do
549+ {
550+ "meta" => {
551+ "has_more" => true ,
552+ "after_cursor" => 'after_cursor' ,
553+ "before_cursor" => 'before_cursor'
554+ } ,
555+ "links" => {
556+ "next" => "next_page" ,
557+ "prev" => "previous_page"
558+ } ,
559+ "test_resources" => [ { "id" => 1 } ]
560+ }
561+ end
562+ before do
563+ stub_json_request ( :get , %r{test_resources} , json ( cbp_response ) )
564+ end
565+
566+ it "sets the next and previous pages and cursors" do
567+ subject . fetch
568+ expect ( subject . instance_variable_get ( :@next_page ) ) . to eq ( "next_page" )
569+ expect ( subject . instance_variable_get ( :@prev_page ) ) . to eq ( "previous_page" )
570+ expect ( subject . options . page . after ) . to eq ( "after_cursor" )
571+ expect ( subject . options . page . before ) . to eq ( "before_cursor" )
572+ end
573+ end
546574 end
547575
548576 context "save" do
@@ -1015,8 +1043,9 @@ def to_proc
10151043 end
10161044 end
10171045
1018- context "when fetching a collection" do
1046+ context "when fetching a collection that supports CBP " do
10191047 before do
1048+ stub_const ( "ZendeskAPI::TestResource::CBP_ACTIONS" , %w[ test_resources ] )
10201049 expect ( subject ) . to receive ( :get_response ) . with ( "test_resources" ) . and_return ( cbp_success_response )
10211050 end
10221051
@@ -1033,18 +1062,6 @@ def to_proc
10331062 end
10341063 end
10351064
1036- context "when the endpoint does not support CBP" do
1037- before do
1038- expect ( subject ) . to receive ( :get_response ) . with ( "test_resources" ) . and_raise ( ZendeskAPI ::Error ::NetworkError ) . twice
1039- end
1040-
1041- it "tries an OBP request after a failed CBP request" do
1042- subject . per_page ( 2 ) . fetch
1043- expect ( subject . instance_variable_get ( :@options ) [ "per_page" ] ) . to eq ( 2 )
1044- expect ( subject . instance_variable_get ( :@options ) [ "page" ] ) . to be_nil
1045- end
1046- end
1047-
10481065 describe "#all going through multiple pages" do
10491066 def generate_response ( index , has_more )
10501067 {
@@ -1061,6 +1078,7 @@ def generate_response(index, has_more)
10611078 }
10621079 end
10631080 before do
1081+ stub_const ( "ZendeskAPI::TestResource::CBP_ACTIONS" , %w[ test_resources ] )
10641082 stub_json_request ( :get , %r{test_resources\? page%5Bsize%5D=1} , json ( generate_response ( 1 , true ) ) )
10651083 stub_json_request ( :get , %r{test_resources.json\/ \? page%5Bafter%5D=after1&page%5Bsize%5D=1} , json ( generate_response ( 2 , true ) ) )
10661084 stub_json_request ( :get , %r{test_resources.json\/ \? page%5Bafter%5D=after2&page%5Bsize%5D=1} , json ( generate_response ( 3 , true ) ) )
@@ -1069,12 +1087,12 @@ def generate_response(index, has_more)
10691087
10701088 it "fetches all pages and yields the correct arguments" do
10711089 expect do |b |
1072- silence_logger { subject . all ( &b ) }
1090+ silence_logger { subject . per_page ( 1 ) . all ( &b ) }
10731091 end . to yield_successive_args (
1074- [ ZendeskAPI ::TestResource . new ( client , id : 1 ) , "after" => "after1" , "before" => "before0" , "size" => 100 ] ,
1075- [ ZendeskAPI ::TestResource . new ( client , id : 2 ) , "after" => "after2" , "before" => "before1" , "size" => 100 ] ,
1076- [ ZendeskAPI ::TestResource . new ( client , id : 3 ) , "after" => "after3" , "before" => "before2" , "size" => 100 ] ,
1077- [ ZendeskAPI ::TestResource . new ( client , id : 4 ) , "after" => "after4" , "before" => "before3" , "size" => 100 ]
1092+ [ ZendeskAPI ::TestResource . new ( client , id : 1 ) , "after" => "after1" , "before" => "before0" , "size" => 1 ] ,
1093+ [ ZendeskAPI ::TestResource . new ( client , id : 2 ) , "after" => "after2" , "before" => "before1" , "size" => 1 ] ,
1094+ [ ZendeskAPI ::TestResource . new ( client , id : 3 ) , "after" => "after3" , "before" => "before2" , "size" => 1 ] ,
1095+ [ ZendeskAPI ::TestResource . new ( client , id : 4 ) , "after" => "after4" , "before" => "before3" , "size" => 1 ]
10781096 )
10791097 end
10801098 end
0 commit comments