|
| 1 | +require 'spec_helper.rb' |
| 2 | +require 'proxycrawl' |
| 3 | + |
| 4 | +describe ProxyCrawl::ScreenshotsAPI do |
| 5 | + it 'raises an error if token is missing' do |
| 6 | + expect { ProxyCrawl::ScreenshotsAPI.new }.to raise_error(RuntimeError, 'Token is required') |
| 7 | + end |
| 8 | + |
| 9 | + it 'sets/reads token' do |
| 10 | + expect(ProxyCrawl::ScreenshotsAPI.new(token: 'test').token).to eql('test') |
| 11 | + end |
| 12 | + |
| 13 | + describe '#get' do |
| 14 | + before(:each) do |
| 15 | + stub_request(:get, 'https://api.proxycrawl.com/screenshots?token=test&url=http%3A%2F%2Fhttpbin.org%2Fanything%3Fparam1%3Dx%26params2%3Dy'). |
| 16 | + to_return( |
| 17 | + body: 'body', |
| 18 | + status: 200, |
| 19 | + headers: { skip_normalize: true, 'original_status' => 200, 'pc_status' => 200, 'url' => 'http://httpbin.org/anything?param1=x¶ms2=y'}) |
| 20 | + end |
| 21 | + |
| 22 | + it 'sends an get request to ProxyCrawl Screenshots API' do |
| 23 | + api = ProxyCrawl::ScreenshotsAPI.new(token: 'test') |
| 24 | + |
| 25 | + response = api.get("http://httpbin.org/anything?param1=x¶ms2=y") |
| 26 | + |
| 27 | + expect(response.status_code).to eql(200) |
| 28 | + expect(response.original_status).to eql(200) |
| 29 | + expect(response.pc_status).to eql(200) |
| 30 | + expect(response.url).to eql('http://httpbin.org/anything?param1=x¶ms2=y') |
| 31 | + expect(response.body).to eql('body') |
| 32 | + expect(response.screenshot_path).not_to be_empty |
| 33 | + end |
| 34 | + |
| 35 | + it 'accepts a valid save_to_path option' do |
| 36 | + api = ProxyCrawl::ScreenshotsAPI.new(token: 'test') |
| 37 | + |
| 38 | + response = api.get("http://httpbin.org/anything?param1=x¶ms2=y", save_to_path: save_to_path) |
| 39 | + |
| 40 | + expect(response.status_code).to eql(200) |
| 41 | + expect(response.original_status).to eql(200) |
| 42 | + expect(response.pc_status).to eql(200) |
| 43 | + expect(response.url).to eql('http://httpbin.org/anything?param1=x¶ms2=y') |
| 44 | + expect(response.body).to eql('body') |
| 45 | + expect(response.screenshot_path).to eql(File.join(Dir.tmpdir, 'test-image.jpg')) |
| 46 | + end |
| 47 | + |
| 48 | + it 'rejects an invalid save_to_path option' do |
| 49 | + api = ProxyCrawl::ScreenshotsAPI.new(token: 'test') |
| 50 | + |
| 51 | + expect { api.get("http://httpbin.org/anything?param1=x¶ms2=y", save_to_path: '~/images/image_filename.png') }.to raise_error(RuntimeError, 'Filename must end with .jpg or .jpeg') |
| 52 | + expect { api.get("http://httpbin.org/anything?param1=x¶ms2=y", save_to_path: 'image_filename.png') }.to raise_error(RuntimeError, 'Filename must end with .jpg or .jpeg') |
| 53 | + expect { api.get("http://httpbin.org/anything?param1=x¶ms2=y", save_to_path: '~/images/image_filename') }.to raise_error(RuntimeError, 'Filename must end with .jpg or .jpeg') |
| 54 | + expect { api.get("http://httpbin.org/anything?param1=x¶ms2=y", save_to_path: 'image_filename') }.to raise_error(RuntimeError, 'Filename must end with .jpg or .jpeg') |
| 55 | + end |
| 56 | + |
| 57 | + it 'accepts a block' do |
| 58 | + api = ProxyCrawl::ScreenshotsAPI.new(token: 'test') |
| 59 | + |
| 60 | + response = api.get("http://httpbin.org/anything?param1=x¶ms2=y", save_to_path: save_to_path) do |file| |
| 61 | + expect(file).to be_kind_of(File) |
| 62 | + expect(file.path).to eql(File.join(Dir.tmpdir, 'test-image.jpg')) |
| 63 | + end |
| 64 | + |
| 65 | + expect(response.status_code).to eql(200) |
| 66 | + expect(response.original_status).to eql(200) |
| 67 | + expect(response.pc_status).to eql(200) |
| 68 | + expect(response.url).to eql('http://httpbin.org/anything?param1=x¶ms2=y') |
| 69 | + expect(response.body).to eql('body') |
| 70 | + expect(response.screenshot_path).to eql(File.join(Dir.tmpdir, 'test-image.jpg')) |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + private |
| 75 | + |
| 76 | + def save_to_path |
| 77 | + File.join(Dir.tmpdir, 'test-image.jpg') |
| 78 | + end |
| 79 | +end |
0 commit comments