browshot 1.16.0 → 1.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,170 @@
1
+ require_relative 'helper'
2
+ require 'tmpdir'
3
+ require 'webrick'
4
+
5
+ class TestBrowshotArtifacts < Test::Unit::TestCase
6
+ FIXTURE_ID = '9007199254740991'
7
+ TEST_KEY = 'test key & value'
8
+ ARTIFACTS = {
9
+ 'pdf' => ['application/pdf', "%PDF-1.7\n\x00\x80\xff".b],
10
+ 'webm' => ['video/webm', "\x1a\x45\xdf\xa3\x00\x80\xff".b],
11
+ 'mp4' => ['video/mp4', "\x00\x00\x00\x10ftyp\x00\x80\xff".b],
12
+ 'gif' => ['image/gif', "GIF89a\x00\x80\xff".b]
13
+ }.freeze
14
+
15
+ context 'API 1.32 against a local server' do
16
+ setup do
17
+ ready = Queue.new
18
+ @server = WEBrick::HTTPServer.new(
19
+ :BindAddress => '127.0.0.1', :Port => 0,
20
+ :Logger => WEBrick::Log.new(File::NULL), :AccessLog => [],
21
+ :StartCallback => proc { ready << true }
22
+ )
23
+ @server.mount_proc('/') { |request, response| fixture_response(request, response) }
24
+ @server_thread = Thread.new { @server.start }
25
+ Timeout.timeout(5) { ready.pop }
26
+ @browshot = Browshot.new(TEST_KEY, false, "http://127.0.0.1:#{@server.config[:Port]}/api/v1/")
27
+ end
28
+
29
+ teardown do
30
+ @server.shutdown
31
+ @server_thread.join
32
+ end
33
+
34
+ should 'download and save all four artifact formats without changing their bytes' do
35
+ ARTIFACTS.each do |format, (_, content)|
36
+ download = format == 'pdf' ? :screenshot_pdf : :screenshot_video
37
+ save = format == 'pdf' ? :screenshot_pdf_file : :screenshot_video_file
38
+ options = { :stub_format => format, 'id' => '1' }
39
+
40
+ data = @browshot.public_send(download, FIXTURE_ID, options)
41
+ assert_equal content, data
42
+ assert_equal Encoding::BINARY, data.encoding
43
+ assert_equal '1', options['id']
44
+
45
+ Dir.mktmpdir do |directory|
46
+ path = File.join(directory, "capture.#{format}")
47
+ assert_equal path, @browshot.public_send(save, FIXTURE_ID, path, options)
48
+ assert_equal content, File.binread(path)
49
+ end
50
+ end
51
+ end
52
+
53
+ should 'accept WebM when an MP4 request falls back' do
54
+ assert_equal ARTIFACTS['webm'][1], @browshot.screenshot_video(FIXTURE_ID, :video_format => 'mp4')
55
+ end
56
+
57
+ should 'reject erroneous empty or truncated responses and incorrect content types' do
58
+ [:screenshot_pdf, :screenshot_video].each do |method|
59
+ [400, 401, 404, 503].each do |status|
60
+ assert_equal '', @browshot.public_send(method, FIXTURE_ID, :stub_status => status)
61
+ end
62
+ [:stub_bad_type, :stub_empty, :stub_truncated].each do |flag|
63
+ assert_equal '', @browshot.public_send(method, FIXTURE_ID, flag => 1)
64
+ end
65
+ end
66
+ assert_equal '', @browshot.screenshot_pdf(FIXTURE_ID, :stub_format => 'gif')
67
+ assert_equal '', @browshot.screenshot_video(FIXTURE_ID, :stub_format => 'pdf')
68
+ end
69
+
70
+ should 'reject invalid screenshot IDs and local paths' do
71
+ [0, -1, nil, '1.5', 'test', '9' * 20, "1\n"].each do |id|
72
+ assert_equal '', @browshot.screenshot_pdf(id)
73
+ assert_equal '', @browshot.screenshot_video(id)
74
+ end
75
+ assert_equal '', @browshot.screenshot_pdf_file(FIXTURE_ID, '')
76
+ assert_equal '', @browshot.screenshot_video_file(FIXTURE_ID, 1)
77
+ assert_equal '', @browshot.screenshot_video_file(FIXTURE_ID, "bad\0path")
78
+ end
79
+
80
+ should 'leave an existing file intact on a download failure and handle write errors' do
81
+ Dir.mktmpdir do |directory|
82
+ path = File.join(directory, 'existing.pdf')
83
+ File.binwrite(path, 'original')
84
+ assert_equal '', @browshot.screenshot_pdf_file(FIXTURE_ID, path, :stub_status => 404)
85
+ assert_equal 'original', File.binread(path)
86
+ assert_equal '', @browshot.screenshot_video_file(FIXTURE_ID, File.join(path, 'capture.gif'))
87
+ end
88
+ end
89
+
90
+ should 'forward all capture options through single multiple batch and crawl requests' do
91
+ options = {
92
+ :pdf => 1, :pdf_format => 'a4', :pdf_background => 1, :pdf_landscape => 0,
93
+ :pdf_one_page => 1, :pdf_margin => '10mm', :pdf_margin_top => '1cm',
94
+ :pdf_margin_bottom => '0.5in', :pdf_margin_left => '2px', :pdf_margin_right => '3px',
95
+ :video => 1, :video_format => 'mp4', :video_duration => 5, :video_quality => 24,
96
+ :video_fps => 20, :video_scale => 0.5, :device_scale => 1, :hide_ads => true
97
+ }
98
+ url = 'https://example.com/?a=hello+world&b=1'
99
+ single = @browshot.screenshot_create(url, options.merge(:instance_id => 373))
100
+ assert_capture_options single, 'screenshot/create', options
101
+ assert_equal ['373'], single['query']['instance_id']
102
+ assert_equal [url], single['query']['url']
103
+
104
+ multiple = @browshot.screenshot_multiple(options.merge(:urls => [url, 'https://example.net/'], 'instances' => [373, 377]))
105
+ assert_capture_options multiple, 'screenshot/multiple', options
106
+ assert_equal [url, 'https://example.net/'], multiple['query']['url']
107
+ assert_equal ['373', '377'], multiple['query']['instance_id']
108
+
109
+ crawl = @browshot.crawl_create(381, 'example.com', options.merge(:url => url))
110
+ assert_capture_options crawl, 'crawl/create', options
111
+ assert_equal ['381'], crawl['query']['instance_id']
112
+ assert_equal ['example.com'], crawl['query']['domain']
113
+ assert_equal [url], crawl['query']['url']
114
+
115
+ Dir.mktmpdir do |directory|
116
+ path = File.join(directory, 'urls.csv')
117
+ File.write(path, url + "\n")
118
+ batch = @browshot.batch_create(385, path, options.dup)
119
+ assert_capture_options batch, 'batch/create', options
120
+ assert_equal ['385'], batch['query']['instance_id']
121
+ assert_equal 'POST', batch['method']
122
+ assert_equal url + "\n", batch['file']
123
+ end
124
+ end
125
+ end
126
+
127
+ private
128
+
129
+ def fixture_response(request, response)
130
+ query = URI.decode_www_form(request.query_string.to_s).group_by(&:first)
131
+ query = query.transform_values { |pairs| pairs.map(&:last) }
132
+ if query['stub_status']
133
+ response.status = query['stub_status'].first.to_i
134
+ response['Content-Type'] = 'application/json'
135
+ response.body = '{"error":"test"}'
136
+ return
137
+ end
138
+
139
+ if ['/api/v1/screenshot/pdf', '/api/v1/screenshot/video'].include?(request.path)
140
+ unless query['id'] == [FIXTURE_ID] && query['key'] == [TEST_KEY] && request.request_method == 'GET'
141
+ response.status = 404
142
+ return
143
+ end
144
+
145
+ default_format = request.path.end_with?('/pdf') ? 'pdf' : 'webm'
146
+ format = query.fetch('stub_format', [default_format]).first
147
+ media_type, body = ARTIFACTS.fetch(format)
148
+ media_type = 'text/html' if query.key?('stub_bad_type')
149
+ body = ''.b if query.key?('stub_empty')
150
+ response['Content-Type'] = media_type.upcase + '; charset=binary'
151
+ if query.key?('stub_truncated')
152
+ response['Content-Length'] = (body.bytesize + 1).to_s
153
+ response['Connection'] = 'close'
154
+ end
155
+ response.body = body
156
+ else
157
+ response['Content-Type'] = 'application/json'
158
+ response.body = JSON.generate(
159
+ 'path' => request.path, 'query' => query,
160
+ 'method' => request.request_method, 'file' => request.query['file']
161
+ )
162
+ end
163
+ end
164
+
165
+ def assert_capture_options(result, path, options)
166
+ assert_equal '/api/v1/' + path, result['path']
167
+ assert_equal [TEST_KEY], result['query']['key']
168
+ options.each { |key, value| assert_equal [value.to_s], result['query'][key.to_s], key.to_s }
169
+ end
170
+ end
@@ -0,0 +1,59 @@
1
+ require_relative 'helper'
2
+ require 'tmpdir'
3
+
4
+ class TestBrowshotArtifactsLive < Test::Unit::TestCase
5
+ context 'API 1.32 with an account' do
6
+ setup do
7
+ @browshot = live_browshot
8
+ end
9
+
10
+ {
11
+ 'pdf' => ['%PDF-'.b, 0],
12
+ 'webm' => ["\x1a\x45\xdf\xa3".b, 0],
13
+ 'mp4' => ['ftyp'.b, 4],
14
+ 'gif' => ['GIF8'.b, 0]
15
+ }.each do |format, (signature, offset)|
16
+ should "download and save a live #{format.upcase} artifact" do
17
+ id = fixture_id(format)
18
+ kind = format == 'pdf' ? 'pdf' : 'video'
19
+ method = 'screenshot_' + kind
20
+ info = @browshot.screenshot_info(id, :details => 1)
21
+ assert_equal 'finished', info['status']
22
+ assert_equal 1, info[kind].to_i
23
+ assert !info[kind + '_url'].empty?
24
+
25
+ content = @browshot.public_send(method, id)
26
+ assert_equal signature, content.byteslice(offset, signature.bytesize)
27
+ Dir.mktmpdir do |directory|
28
+ path = File.join(directory, "capture.#{format}")
29
+ assert_equal path, @browshot.public_send(method + '_file', id, path)
30
+ assert_equal content, File.binread(path)
31
+ end
32
+ end
33
+ end
34
+
35
+ should 'list all sixteen native country instances at one credit' do
36
+ instances = @browshot.instance_list['shared'].each_with_object({}) { |instance, ids| ids[instance['id']] = instance }
37
+ countries = ['France', 'India', 'Finland', 'United Kingdom']
38
+ (373..388).each do |id|
39
+ assert instances.key?(id), "Missing instance #{id}"
40
+ assert_equal countries[(id - 373) / 4], instances[id]['country']
41
+ assert_equal 1, instances[id]['screenshot_cost'].to_i
42
+ end
43
+ end
44
+
45
+ should 'return retina and ad blocking metadata' do
46
+ info = @browshot.screenshot_info(fixture_id('retina'), :details => 1)
47
+ assert_equal 'finished', info['status']
48
+ assert_equal 2, info['device_scale'].to_i
49
+ assert_equal 1, info['hide_ads'].to_i
50
+ end
51
+
52
+ should 'return empty strings for unavailable or unauthorized artifacts' do
53
+ [@browshot, Browshot.new('invalid-test-key')].each do |client|
54
+ assert_equal '', client.screenshot_pdf(1)
55
+ assert_equal '', client.screenshot_video(1)
56
+ end
57
+ end
58
+ end
59
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: browshot
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.16.0
4
+ version: 1.32.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julien Sobrier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-01 00:00:00.000000000 Z
11
+ date: 2026-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -39,21 +39,21 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: bundler
42
+ name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '1.5'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '1.5'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rake
56
+ name: shoulda-context
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: shoulda
70
+ name: yard
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -81,7 +81,21 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: yard
84
+ name: test-unit
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webrick
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
101
  - - ">="
@@ -100,11 +114,18 @@ executables: []
100
114
  extensions: []
101
115
  extra_rdoc_files: []
102
116
  files:
117
+ - CHANGELOG.rdoc
103
118
  - Gemfile
119
+ - LICENSE.txt
120
+ - README.rdoc
104
121
  - Rakefile
122
+ - VERSION
123
+ - browshot.gemspec
105
124
  - lib/browshot.rb
106
125
  - test/helper.rb
107
126
  - test/test_browshot.rb
127
+ - test/test_browshot_artifacts.rb
128
+ - test/test_browshot_artifacts_live.rb
108
129
  homepage: https://browshot.com/
109
130
  licenses:
110
131
  - MIT
@@ -125,10 +146,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
146
  version: '0'
126
147
  requirements: []
127
148
  rubyforge_project:
128
- rubygems_version: 2.4.5
149
+ rubygems_version: 2.7.6.3
129
150
  signing_key:
130
151
  specification_version: 4
131
152
  summary: Take website screenshots with Browshot
132
153
  test_files:
154
+ - test/test_browshot_artifacts.rb
155
+ - test/test_browshot_artifacts_live.rb
133
156
  - test/helper.rb
134
157
  - test/test_browshot.rb