Skip to content

Commit cdb0d3a

Browse files
committed
Add test for serializing variable values
1 parent 52aea64 commit cdb0d3a

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

test/sanity/http/where_test.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
describe Sanity::Http::Where do
6+
let(:klass) { Sanity::Http::Where }
7+
let(:resource_class) do
8+
Class.new do
9+
def self.where_api_endpoint
10+
"query/production"
11+
end
12+
13+
def self.project_id
14+
"test-project"
15+
end
16+
17+
def self.dataset
18+
"production"
19+
end
20+
21+
def self.api_version
22+
"v1"
23+
end
24+
end
25+
end
26+
27+
describe "query parameter serialization" do
28+
describe "with GET requests" do
29+
subject { klass.new(resource_klass: resource_class, variables: variables) }
30+
31+
describe "with different variable types" do
32+
let(:variables) do
33+
{
34+
"string_var" => "hello",
35+
"array_var" => [1, 2, 3],
36+
"hash_var" => {"foo" => "bar"},
37+
"number_var" => 42,
38+
"boolean_var" => true
39+
}
40+
end
41+
42+
it "properly serializes variables in the query string" do
43+
uri = subject.send(:uri)
44+
query_params = URI.decode_www_form(uri.query).to_h
45+
46+
assert_equal "\"hello\"", query_params["$string_var"]
47+
assert_equal "[1,2,3]", query_params["$array_var"]
48+
assert_equal "{\"foo\":\"bar\"}", query_params["$hash_var"]
49+
assert_equal "42", query_params["$number_var"]
50+
assert_equal "true", query_params["$boolean_var"]
51+
end
52+
end
53+
end
54+
55+
describe "with POST requests" do
56+
subject { klass.new(resource_klass: resource_class, variables: variables, use_post: true) }
57+
58+
let(:variables) do
59+
{
60+
"string_var" => "hello",
61+
"array_var" => [1, 2, 3]
62+
}
63+
end
64+
65+
it "includes variables in the request body" do
66+
body = JSON.parse(subject.send(:request_body))
67+
assert_equal variables, body["params"]
68+
end
69+
end
70+
end
71+
end

0 commit comments

Comments
 (0)