Skip to content

Commit 41c16e4

Browse files
committed
Add optional end-to-end upload test harness
1 parent 45a4744 commit 41c16e4

File tree

12 files changed

+194
-24
lines changed

12 files changed

+194
-24
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.jpg binary
2+

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ env.sh
1616
.env
1717
vendor/bundle/
1818
.docker-cache/
19+
.cache/rubocop_cache/04e06e0faf5ad652d8bcbcfd85bac5f6c32e711e/3031a80880d8a984708138f0d003f77c4bad2648

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,14 @@ The script forwards environment variables such as `TEST_NODE_PARITY` or credenti
515515
TEST_NODE_PARITY=1 ./scripts/test-in-docker.sh
516516
```
517517

518+
To exercise the optional end-to-end upload against a real Transloadit account, provide `TRANSLOADIT_KEY` and `TRANSLOADIT_SECRET` (via environment variables or `.env`) and set `RUBY_SDK_E2E=1`:
519+
520+
```bash
521+
RUBY_SDK_E2E=1 ./scripts/test-in-docker.sh bundle exec ruby -Itest test/integration/test_e2e_upload.rb
522+
```
523+
524+
The test uploads `chameleon.jpg`, resizes it, and asserts on the live assembly results. It respects `TRANSLOADIT_HOST` and `TRANSLOADIT_REGION` overrides when present.
525+
518526
### Releasing on RubyGems
519527

520528
Let's say you wanted to release version `3.1.0`, here are the steps:

chameleon.jpg

8.88 MB
Loading

lib/transloadit.rb

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Transloadit
3131
attr_accessor :duration
3232

3333
attr_accessor :max_size
34+
attr_accessor :service
3435

3536
#
3637
# Creates a new instance of the Transloadit API.
@@ -48,6 +49,7 @@ def initialize(options = {})
4849
self.secret = options[:secret]
4950
self.duration = options[:duration] || 5 * 60
5051
self.max_size = options[:max_size]
52+
self.service = _normalize_service(options[:service]) || _service_from_region(options[:region])
5153

5254
_ensure_key_provided
5355
end
@@ -108,7 +110,7 @@ def bill(month = Date.today.month, year = Date.today.year)
108110
month = format "%02d", month
109111
path = "bill/#{year}-#{month}"
110112

111-
Transloadit::Request.new(path, secret).get({auth: to_hash})
113+
Transloadit::Request.new(request_url_for(path), secret).get({auth: to_hash})
112114
end
113115

114116
#
@@ -135,6 +137,22 @@ def to_json
135137
MultiJson.dump(to_hash)
136138
end
137139

140+
#
141+
# Resolves an absolute URL for API requests, honoring custom service overrides.
142+
#
143+
# @param [String] path the request path or URL
144+
# @return [String] the resolved URL or original path if already absolute
145+
#
146+
def request_url_for(path)
147+
return path if service.nil? || service.empty?
148+
149+
uri = URI.parse(path.to_s)
150+
return path if uri.host
151+
152+
base = service.end_with?("/") ? service : "#{service}/"
153+
URI.join(base, path.to_s.sub(%r{\A/}, "")).to_s
154+
end
155+
138156
# @param workspace [String] Workspace slug
139157
# @param template [String] Template slug or template ID
140158
# @param input [String] Input value that is provided as `${fields.input}` in the template
@@ -185,6 +203,30 @@ def signed_smart_cdn_url(
185203

186204
private
187205

206+
#
207+
# Normalizes service URLs, returning nil for blank values.
208+
#
209+
def _normalize_service(value)
210+
return nil if value.nil?
211+
212+
str = value.to_s.strip
213+
return nil if str.empty?
214+
215+
str
216+
end
217+
218+
#
219+
# Builds a service URL from a region hint, if one is provided.
220+
#
221+
def _service_from_region(region)
222+
return nil if region.nil?
223+
224+
str = region.to_s.strip
225+
return nil if str.empty?
226+
227+
"https://api2-#{str}.transloadit.com"
228+
end
229+
188230
#
189231
# Raises an ArgumentError if no {#key} has been assigned.
190232
#

lib/transloadit/api_model.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def _do_request(path, params = nil, method = "get", extra_params = nil)
6868
params = {params: params} if ["post", "put", "delete"].include? method
6969
params.merge!(extra_params) unless extra_params.nil?
7070
end
71-
Transloadit::Request.new(path, transloadit.secret).public_send(method, params)
71+
request_url = transloadit.request_url_for(path)
72+
Transloadit::Request.new(request_url, transloadit.secret).public_send(method, params)
7273
end
7374
end

lib/transloadit/response.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def inspect
5252
#
5353
def extend!(mod)
5454
extend(mod)
55+
5556
self
5657
end
5758

scripts/test-in-docker.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,19 @@ if [[ -f .env ]]; then
7373
DOCKER_ARGS+=(--env-file "$PWD/.env")
7474
fi
7575

76+
PASSTHROUGH_ENV_VARS=(
77+
TRANSLOADIT_KEY
78+
TRANSLOADIT_SECRET
79+
TRANSLOADIT_HOST
80+
TRANSLOADIT_REGION
81+
TRANSLOADIT_TEMPLATE_ID
82+
RUBY_SDK_E2E
83+
)
84+
85+
for var in "${PASSTHROUGH_ENV_VARS[@]}"; do
86+
if [[ -n "${!var:-}" ]]; then
87+
DOCKER_ARGS+=(-e "$var=${!var}")
88+
fi
89+
done
90+
7691
exec docker run "${DOCKER_ARGS[@]}" "$IMAGE_NAME" bash -lc "$RUN_CMD"
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
require "test_helper"
2+
3+
describe "Transloadit end-to-end upload" do
4+
before do
5+
skip "Set RUBY_SDK_E2E=1 to run live upload tests" unless e2e_enabled?
6+
7+
@key = ENV["TRANSLOADIT_KEY"]
8+
@secret = ENV["TRANSLOADIT_SECRET"]
9+
skip "TRANSLOADIT_KEY and TRANSLOADIT_SECRET must be set to run live upload tests" if blank?(@key) || blank?(@secret)
10+
11+
@service = resolve_service
12+
13+
@fixture_path = File.expand_path("../../chameleon.jpg", __dir__)
14+
skip "chameleon.jpg fixture missing; run tests from the repository root" unless File.file?(@fixture_path)
15+
end
16+
17+
it "uploads and processes the chameleon image" do
18+
options = {
19+
key: @key,
20+
secret: @secret
21+
}
22+
options[:service] = @service if @service
23+
24+
transloadit = Transloadit.new(options)
25+
26+
resize_step = transloadit.step(
27+
"resize",
28+
"/image/resize",
29+
use: ":original",
30+
width: 128,
31+
height: 128,
32+
resize_strategy: "fit",
33+
format: "png"
34+
)
35+
36+
response = File.open(@fixture_path, "rb") do |upload|
37+
transloadit.assembly.create!(
38+
upload,
39+
wait: true,
40+
steps: resize_step
41+
)
42+
end
43+
44+
response.reload_until_finished!(tries: 120) unless response.finished?
45+
46+
_(response.completed?).must_equal true, "Assembly did not complete successfully: #{response.body.inspect}"
47+
48+
uploads = response["uploads"] || []
49+
refute_empty uploads, "Expected uploads in the assembly response"
50+
51+
upload_info = uploads.first
52+
basename = upload_info["basename"]
53+
_(basename).must_equal File.basename(@fixture_path, ".*") if basename
54+
55+
filename = upload_info["name"]
56+
_(filename).must_equal File.basename(@fixture_path) if filename
57+
58+
results = (response["results"] || {})["resize"] || []
59+
refute_empty results, "Expected resize results in assembly response"
60+
61+
first_result = results.first
62+
ssl_url = first_result["ssl_url"]
63+
refute_nil ssl_url, "Missing ssl_url in resize result: #{first_result.inspect}"
64+
_(ssl_url).must_match(/\Ahttps:\/\//)
65+
66+
meta = first_result["meta"] || {}
67+
width = integer_if_present(meta["width"])
68+
height = integer_if_present(meta["height"])
69+
refute_nil width, "Missing width metadata: #{meta.inspect}"
70+
refute_nil height, "Missing height metadata: #{meta.inspect}"
71+
assert width.positive? && width <= 128, "Unexpected width #{width.inspect}"
72+
assert height.positive? && height <= 128, "Unexpected height #{height.inspect}"
73+
end
74+
75+
private
76+
77+
def e2e_enabled?
78+
flag = ENV["RUBY_SDK_E2E"]
79+
return false if blank?(flag)
80+
81+
%w[1 true yes on].include?(flag.to_s.strip.downcase)
82+
end
83+
84+
def resolve_service
85+
host = ENV["TRANSLOADIT_HOST"].to_s.strip
86+
return host unless host.empty?
87+
88+
region = ENV["TRANSLOADIT_REGION"].to_s.strip
89+
return nil if region.empty?
90+
91+
"https://api2-#{region}.transloadit.com"
92+
end
93+
94+
def integer_if_present(value)
95+
return nil if blank?(value)
96+
97+
value.to_i
98+
end
99+
100+
def blank?(value)
101+
value.respond_to?(:empty?) ? value.empty? : !value
102+
end
103+
end

test/test_helper.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,3 @@ def run_transloadit_sig(payload, key:, secret:, algorithm: nil)
7272
end
7373

7474
Minitest::Test.include(TransloaditCliHelpers)
75-

0 commit comments

Comments
 (0)