Skip to content

Commit 5836a93

Browse files
committed
Normalize symbols in request payloads
1 parent 4d6111d commit 5836a93

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

lib/preflight_qa/client.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def build_request(http_class, uri, body)
5353
request["Authorization"] = "Bearer #{config.api_key}"
5454
request["Content-Type"] = "application/json"
5555
request["Accept"] = "application/json"
56-
request.body = JSON.generate(body) unless body.nil?
56+
request.body = JSON.generate(normalize_payload(body)) unless body.nil?
5757
request
5858
end
5959

@@ -100,5 +100,24 @@ def error_class_for(status)
100100
else RequestError
101101
end
102102
end
103+
104+
def normalize_payload(value)
105+
case value
106+
when Array
107+
value.map { |item| normalize_payload(item) }
108+
when Hash
109+
normalize_payload_hash(value)
110+
when Symbol
111+
value.to_s
112+
else
113+
value
114+
end
115+
end
116+
117+
def normalize_payload_hash(value)
118+
value.each_with_object({}) do |(key, nested_value), normalized|
119+
normalized[key.to_s] = normalize_payload(nested_value)
120+
end
121+
end
103122
end
104123
end

spec/preflight_qa/client_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@
2727
end
2828
end
2929

30+
it "converts symbols to strings throughout the JSON payload" do
31+
client.post(
32+
"/screenshots",
33+
body: {
34+
apps: {
35+
apple_mail_mac: { modes: [:light, :dark] }
36+
},
37+
locale: :"en-GB"
38+
}
39+
)
40+
41+
expect(http).to have_received(:request) do |request|
42+
expect(request.body).to eq(
43+
'{"apps":{"apple_mail_mac":{"modes":["light","dark"]}},"locale":"en-GB"}'
44+
)
45+
end
46+
end
47+
3048
it "raises a configuration error without an api key" do
3149
config.api_key = nil
3250

0 commit comments

Comments
 (0)