|
2 | 2 | describe '#fetch_all' do |
3 | 3 | context 'when one page of items is returned' do |
4 | 4 | before do |
5 | | - stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll") |
6 | | - .to_return_json(body: { "items": ["one", "two", "three"] }) |
| 5 | + stub_fetch_all("https://#{host}/xrpc/com.example.service.fetchAll", [ |
| 6 | + { "items": ["one", "two", "three"] } |
| 7 | + ]) |
7 | 8 | end |
8 | 9 |
|
9 | 10 | it 'should make one request to the given endpoint' do |
10 | 11 | subject.fetch_all('com.example.service.fetchAll', field: 'items') |
11 | | - |
12 | | - WebMock.should have_requested(:get, "https://#{host}/xrpc/com.example.service.fetchAll").once |
| 12 | + verify_fetch_all |
13 | 13 | end |
14 | 14 |
|
15 | 15 | it 'should return the parsed items' do |
|
20 | 20 |
|
21 | 21 | context 'when more than one page of items is returned' do |
22 | 22 | before do |
23 | | - stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll") |
24 | | - .to_return_json(body: { "items": ["one", "two", "three"], "cursor": "ccc111" }) |
25 | | - |
26 | | - stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll?cursor=ccc111") |
27 | | - .to_return_json(body: { "items": ["four", "five"] }) |
| 23 | + stub_fetch_all("https://#{host}/xrpc/com.example.service.fetchAll", [ |
| 24 | + { "items": ["one", "two", "three"] }, |
| 25 | + { "items": ["four", "five"] }, |
| 26 | + ]) |
28 | 27 | end |
29 | 28 |
|
30 | 29 | it 'should make multiple requests, passing the last cursor' do |
31 | 30 | subject.fetch_all('com.example.service.fetchAll', field: 'items') |
32 | | - |
33 | | - WebMock.should have_requested(:get, "https://#{host}/xrpc/com.example.service.fetchAll").once |
34 | | - WebMock.should have_requested(:get, "https://#{host}/xrpc/com.example.service.fetchAll?cursor=ccc111").once |
| 31 | + verify_fetch_all |
35 | 32 | end |
36 | 33 |
|
37 | 34 | it 'should return all the parsed items collected from the responses' do |
|
42 | 39 |
|
43 | 40 | context 'when params are passed' do |
44 | 41 | before do |
45 | | - stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll?type=post") |
46 | | - .to_return_json(body: { "items": ["one", "two", "three"], "cursor": "ccc222" }) |
47 | | - |
48 | | - stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll?type=post&cursor=ccc222") |
49 | | - .to_return_json(body: { "items": ["four", "five"] }) |
| 42 | + stub_fetch_all("https://#{host}/xrpc/com.example.service.fetchAll?type=post", [ |
| 43 | + { "items": ["one", "two", "three"] }, |
| 44 | + { "items": ["four", "five"] }, |
| 45 | + ]) |
50 | 46 | end |
51 | 47 |
|
52 | 48 | it 'should add the params to the url' do |
53 | 49 | subject.fetch_all('com.example.service.fetchAll', { type: 'post' }, field: 'items') |
54 | | - |
55 | | - WebMock.should have_requested(:get, |
56 | | - "https://#{host}/xrpc/com.example.service.fetchAll?type=post").once |
57 | | - WebMock.should have_requested(:get, |
58 | | - "https://#{host}/xrpc/com.example.service.fetchAll?type=post&cursor=ccc222").once |
| 50 | + verify_fetch_all |
59 | 51 | end |
60 | 52 | end |
61 | 53 |
|
62 | 54 | context 'when params are an explicit nil' do |
63 | 55 | before do |
64 | | - stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll") |
65 | | - .to_return_json(body: { "items": ["one", "two", "three"], "cursor": "ccc222" }) |
66 | | - |
67 | | - stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll?cursor=ccc222") |
68 | | - .to_return_json(body: { "items": ["four", "five"] }) |
| 56 | + stub_fetch_all("https://#{host}/xrpc/com.example.service.fetchAll", [ |
| 57 | + { "items": ["one", "two", "three"] }, |
| 58 | + { "items": ["four", "five"] }, |
| 59 | + ]) |
69 | 60 | end |
70 | 61 |
|
71 | 62 | it 'should not add anything to the url' do |
72 | 63 | subject.fetch_all('com.example.service.fetchAll', nil, field: 'items') |
73 | | - |
74 | | - WebMock.should have_requested(:get, "https://#{host}/xrpc/com.example.service.fetchAll").once |
75 | | - WebMock.should have_requested(:get, "https://#{host}/xrpc/com.example.service.fetchAll?cursor=ccc222").once |
| 64 | + verify_fetch_all |
76 | 65 | end |
77 | 66 | end |
78 | 67 |
|
|
97 | 86 |
|
98 | 87 | context 'when break condition is passed' do |
99 | 88 | before do |
100 | | - stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll") |
101 | | - .to_return_json(body: { "items": ["one", "two", "three"], "cursor": "page1" }) |
102 | | - |
103 | | - stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll?cursor=page1") |
104 | | - .to_return_json(body: { "items": ["four", "five"], "cursor": "page2" }) |
105 | | - |
106 | | - stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll?cursor=page2") |
107 | | - .to_return_json(body: { "items": ["six"] }) |
| 89 | + stub_fetch_all("https://#{host}/xrpc/com.example.service.fetchAll", [ |
| 90 | + { "items": ["one", "two", "three"] }, |
| 91 | + { "items": ["four", "five"] }, |
| 92 | + { "items": ["six"] }, |
| 93 | + ]) |
108 | 94 | end |
109 | 95 |
|
110 | 96 | it 'should stop when a matching item is found' do |
111 | 97 | subject.fetch_all('com.example.service.fetchAll', field: 'items', break_when: ->(x) { x =~ /u/ }) |
112 | 98 |
|
113 | | - WebMock.should have_requested(:get, "https://#{host}/xrpc/com.example.service.fetchAll").once |
114 | | - WebMock.should have_requested(:get, "https://#{host}/xrpc/com.example.service.fetchAll?cursor=page1").once |
115 | | - WebMock.should_not have_requested(:get, "https://#{host}/xrpc/com.example.service.fetchAll?cursor=page2") |
| 99 | + WebMock.should have_requested(:get, @stubbed_urls[0]).once |
| 100 | + WebMock.should have_requested(:get, @stubbed_urls[1]).once |
| 101 | + WebMock.should_not have_requested(:get, @stubbed_urls[2]) |
116 | 102 | end |
117 | 103 |
|
118 | 104 | it 'should filter out matching items from the response' do |
|
0 commit comments