Skip to content

Commit 18d5461

Browse files
author
Robert Mosolgo
authored
Merge pull request #1698 from gfx/urlsafe_base64
Change the default behavior of the encoder to use `urlsafe` versions
2 parents 5098895 + 001d413 commit 18d5461

File tree

6 files changed

+65
-65
lines changed

6 files changed

+65
-65
lines changed

lib/graphql/schema/base_64_encoder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Schema
77
# @api private
88
module Base64Encoder
99
def self.encode(plaintext, nonce: false)
10-
Base64.strict_encode64(plaintext)
10+
Base64Bp.urlsafe_encode64(plaintext, padding: false)
1111
end
1212

1313
def self.decode(ciphertext, nonce: false)

spec/integration/mongoid/graphql/relay/mongo_relation_connection_spec.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,19 @@ def get_last_cursor(result)
6060
assert_equal(2, get_names(result).length)
6161
assert_equal(true, get_page_info(result)["hasNextPage"])
6262
assert_equal(false, get_page_info(result)["hasPreviousPage"])
63-
assert_equal("MQ==", get_page_info(result)["startCursor"])
64-
assert_equal("Mg==", get_page_info(result)["endCursor"])
65-
assert_equal("MQ==", get_first_cursor(result))
66-
assert_equal("Mg==", get_last_cursor(result))
63+
assert_equal("MQ", get_page_info(result)["startCursor"])
64+
assert_equal("Mg", get_page_info(result)["endCursor"])
65+
assert_equal("MQ", get_first_cursor(result))
66+
assert_equal("Mg", get_last_cursor(result))
6767

6868
result = star_trek_query(query_string, "first" => 3)
6969
assert_equal(3, get_names(result).length)
7070
assert_equal(false, get_page_info(result)["hasNextPage"])
7171
assert_equal(false, get_page_info(result)["hasPreviousPage"])
72-
assert_equal("MQ==", get_page_info(result)["startCursor"])
73-
assert_equal("Mw==", get_page_info(result)["endCursor"])
74-
assert_equal("MQ==", get_first_cursor(result))
75-
assert_equal("Mw==", get_last_cursor(result))
72+
assert_equal("MQ", get_page_info(result)["startCursor"])
73+
assert_equal("Mw", get_page_info(result)["endCursor"])
74+
assert_equal("MQ", get_first_cursor(result))
75+
assert_equal("Mw", get_last_cursor(result))
7676
end
7777

7878
it 'provides custom fields on the connection type' do
@@ -268,7 +268,7 @@ def get_last_cursor(result)
268268
assert_equal(first_and_second_names, get_names(result))
269269
assert_equal(false, result["data"]["federation"]["bases"]["pageInfo"]["hasPreviousPage"], "hasPreviousPage is false when last is not specified")
270270

271-
third_cursor = "Mw=="
271+
third_cursor = "Mw"
272272
result = star_trek_query(query_string, "last" => 100, "before" => third_cursor)
273273
assert_equal(first_and_second_names, get_names(result))
274274

@@ -462,8 +462,8 @@ def get_names(result, field_name)
462462
let(:connection) { GraphQL::Relay::MongoRelationConnection.new(StarTrek::Base.where(faction_id: 1), {}) }
463463

464464
it "returns the cursor for a node in the connection" do
465-
assert_equal "MQ==", connection.cursor_from_node(StarTrek::Base.all[0])
466-
assert_equal "Mg==", connection.cursor_from_node(StarTrek::Base.all[1])
465+
assert_equal "MQ", connection.cursor_from_node(StarTrek::Base.all[0])
466+
assert_equal "Mg", connection.cursor_from_node(StarTrek::Base.all[1])
467467
end
468468

469469
it "raises when the node isn't found" do

spec/integration/rails/graphql/relay/array_connection_spec.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def get_page_info(result, key = "bases")
4343
assert_equal(2, number_of_ships)
4444
assert_equal(true, result["data"]["rebels"]["ships"]["pageInfo"]["hasNextPage"])
4545
assert_equal(false, result["data"]["rebels"]["ships"]["pageInfo"]["hasPreviousPage"])
46-
assert_equal("MQ==", result["data"]["rebels"]["ships"]["pageInfo"]["startCursor"])
47-
assert_equal("Mg==", result["data"]["rebels"]["ships"]["pageInfo"]["endCursor"])
46+
assert_equal("MQ", result["data"]["rebels"]["ships"]["pageInfo"]["startCursor"])
47+
assert_equal("Mg", result["data"]["rebels"]["ships"]["pageInfo"]["endCursor"])
4848

4949
result = star_wars_query(query_string, "first" => 3)
5050
number_of_ships = get_names(result).length
@@ -55,14 +55,14 @@ def get_page_info(result, key = "bases")
5555
result = star_wars_query(query_string, "first" => 2)
5656
assert_equal(true, result["data"]["rebels"]["ships"]["pageInfo"]["hasNextPage"])
5757
assert_equal(false, result["data"]["rebels"]["ships"]["pageInfo"]["hasPreviousPage"])
58-
assert_equal("MQ==", result["data"]["rebels"]["ships"]["pageInfo"]["startCursor"])
59-
assert_equal("Mg==", result["data"]["rebels"]["ships"]["pageInfo"]["endCursor"])
58+
assert_equal("MQ", result["data"]["rebels"]["ships"]["pageInfo"]["startCursor"])
59+
assert_equal("Mg", result["data"]["rebels"]["ships"]["pageInfo"]["endCursor"])
6060

6161
result = star_wars_query(query_string, "first" => 100)
6262
assert_equal(false, result["data"]["rebels"]["ships"]["pageInfo"]["hasNextPage"])
6363
assert_equal(false, result["data"]["rebels"]["ships"]["pageInfo"]["hasPreviousPage"])
64-
assert_equal("MQ==", result["data"]["rebels"]["ships"]["pageInfo"]["startCursor"])
65-
assert_equal("NQ==", result["data"]["rebels"]["ships"]["pageInfo"]["endCursor"])
64+
assert_equal("MQ", result["data"]["rebels"]["ships"]["pageInfo"]["startCursor"])
65+
assert_equal("NQ", result["data"]["rebels"]["ships"]["pageInfo"]["endCursor"])
6666
end
6767

6868
it "provides bidirectional_pagination" do
@@ -166,8 +166,8 @@ def get_page_info(result, key = "bases")
166166

167167
assert_equal(false, result["data"]["rebels"]["ships"]["pageInfo"]["hasNextPage"])
168168
assert_equal(false, result["data"]["rebels"]["ships"]["pageInfo"]["hasPreviousPage"])
169-
assert_equal("MQ==", result["data"]["rebels"]["ships"]["pageInfo"]["startCursor"])
170-
assert_equal("NQ==", result["data"]["rebels"]["ships"]["pageInfo"]["endCursor"])
169+
assert_equal("MQ", result["data"]["rebels"]["ships"]["pageInfo"]["startCursor"])
170+
assert_equal("NQ", result["data"]["rebels"]["ships"]["pageInfo"]["endCursor"])
171171
assert_equal(5, result["data"]["rebels"]["ships"]["edges"].length)
172172
end
173173

@@ -217,7 +217,7 @@ def get_names(result)
217217
assert_equal(["Yavin", "Echo Base"], get_names(result))
218218
assert_equal(false, get_page_info(result)["hasPreviousPage"], "hasPreviousPage is false when last is not specified")
219219

220-
third_cursor = "Mw=="
220+
third_cursor = "Mw"
221221
first_and_second_names = ["Yavin", "Echo Base"]
222222
result = star_wars_query(query_string, "last" => 100, "before" => third_cursor)
223223
assert_equal(first_and_second_names, get_names(result))

spec/integration/rails/graphql/relay/base_connection_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ def decode(str, nonce: false); str.reverse; end
7777
assert_equal "Person/1", conn.decode("1/nosreP")
7878
end
7979

80-
it "defaults to base64" do
80+
it "defaults to base64 (urlsafe_base64 without padding)" do
8181
conn = GraphQL::Relay::BaseConnection.new([], {}, context: nil)
8282

83-
assert_equal "UGVyc29uLzE=", conn.encode("Person/1")
84-
assert_equal "Person/1", conn.decode("UGVyc29uLzE=")
83+
assert_equal "UGVyc29uLzE", conn.encode("Person/1")
84+
assert_equal "Person/1", conn.decode("UGVyc29uLzE=") # can decode with padding
8585
end
8686

8787
it "handles trimmed base64" do

spec/integration/rails/graphql/relay/page_info_spec.rb

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,63 +46,63 @@ def get_last_cursor(result)
4646
result = star_wars_query(query_string, "first" => 2)
4747
assert_equal(true, get_page_info(result)["hasNextPage"])
4848
assert_equal(false, get_page_info(result)["hasPreviousPage"], "hasPreviousPage is false if 'last' is missing")
49-
assert_equal("MQ==", get_page_info(result)["startCursor"])
50-
assert_equal("Mg==", get_page_info(result)["endCursor"])
49+
assert_equal("MQ", get_page_info(result)["startCursor"])
50+
assert_equal("Mg", get_page_info(result)["endCursor"])
5151

5252
last_cursor = get_last_cursor(result)
5353
result = star_wars_query(query_string, "first" => 100, "after" => last_cursor)
5454
assert_equal(false, get_page_info(result)["hasNextPage"])
5555
assert_equal(false, get_page_info(result)["hasPreviousPage"])
56-
assert_equal("Mw==", get_page_info(result)["startCursor"])
57-
assert_equal("Mw==", get_page_info(result)["endCursor"])
56+
assert_equal("Mw", get_page_info(result)["startCursor"])
57+
assert_equal("Mw", get_page_info(result)["endCursor"])
5858
end
5959

6060
it "hasPreviousPage if there are more items" do
6161
result = star_wars_query(query_string, "last" => 100, "before" => cursor_of_last_base)
6262
assert_equal(false, get_page_info(result)["hasNextPage"])
6363
assert_equal(false, get_page_info(result)["hasPreviousPage"])
64-
assert_equal("MQ==", get_page_info(result)["startCursor"])
65-
assert_equal("Mg==", get_page_info(result)["endCursor"])
64+
assert_equal("MQ", get_page_info(result)["startCursor"])
65+
assert_equal("Mg", get_page_info(result)["endCursor"])
6666

6767
result = star_wars_query(query_string, "last" => 1, "before" => cursor_of_last_base)
6868
assert_equal(false, get_page_info(result)["hasNextPage"])
6969
assert_equal(true, get_page_info(result)["hasPreviousPage"])
70-
assert_equal("Mg==", get_page_info(result)["startCursor"])
71-
assert_equal("Mg==", get_page_info(result)["endCursor"])
70+
assert_equal("Mg", get_page_info(result)["startCursor"])
71+
assert_equal("Mg", get_page_info(result)["endCursor"])
7272
end
7373

7474
it "has both if first and last are present" do
7575
result = star_wars_query(query_string, "last" => 1, "first" => 1, "before" => cursor_of_last_base)
7676
assert_equal(true, get_page_info(result)["hasNextPage"])
7777
assert_equal(true, get_page_info(result)["hasPreviousPage"])
78-
assert_equal("MQ==", get_page_info(result)["startCursor"])
79-
assert_equal("MQ==", get_page_info(result)["endCursor"])
78+
assert_equal("MQ", get_page_info(result)["startCursor"])
79+
assert_equal("MQ", get_page_info(result)["endCursor"])
8080
end
8181

8282
it "startCursor and endCursor are the cursors of the first and last edge" do
8383
result = star_wars_query(query_string, "first" => 2)
8484
assert_equal(true, get_page_info(result)["hasNextPage"])
8585
assert_equal(false, get_page_info(result)["hasPreviousPage"])
86-
assert_equal("MQ==", get_page_info(result)["startCursor"])
87-
assert_equal("Mg==", get_page_info(result)["endCursor"])
88-
assert_equal("MQ==", get_first_cursor(result))
89-
assert_equal("Mg==", get_last_cursor(result))
86+
assert_equal("MQ", get_page_info(result)["startCursor"])
87+
assert_equal("Mg", get_page_info(result)["endCursor"])
88+
assert_equal("MQ", get_first_cursor(result))
89+
assert_equal("Mg", get_last_cursor(result))
9090

9191
result = star_wars_query(query_string, "first" => 1, "after" => get_page_info(result)["endCursor"])
9292
assert_equal(false, get_page_info(result)["hasNextPage"])
9393
assert_equal(false, get_page_info(result)["hasPreviousPage"])
94-
assert_equal("Mw==", get_page_info(result)["startCursor"])
95-
assert_equal("Mw==", get_page_info(result)["endCursor"])
96-
assert_equal("Mw==", get_first_cursor(result))
97-
assert_equal("Mw==", get_last_cursor(result))
94+
assert_equal("Mw", get_page_info(result)["startCursor"])
95+
assert_equal("Mw", get_page_info(result)["endCursor"])
96+
assert_equal("Mw", get_first_cursor(result))
97+
assert_equal("Mw", get_last_cursor(result))
9898

9999
result = star_wars_query(query_string, "last" => 1, "before" => get_page_info(result)["endCursor"])
100100
assert_equal(false, get_page_info(result)["hasNextPage"])
101101
assert_equal(true, get_page_info(result)["hasPreviousPage"])
102-
assert_equal("Mg==", get_page_info(result)["startCursor"])
103-
assert_equal("Mg==", get_page_info(result)["endCursor"])
104-
assert_equal("Mg==", get_first_cursor(result))
105-
assert_equal("Mg==", get_last_cursor(result))
102+
assert_equal("Mg", get_page_info(result)["startCursor"])
103+
assert_equal("Mg", get_page_info(result)["endCursor"])
104+
assert_equal("Mg", get_first_cursor(result))
105+
assert_equal("Mg", get_last_cursor(result))
106106
end
107107
end
108108

spec/integration/rails/graphql/relay/relation_connection_spec.rb

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,19 @@ def get_last_cursor(result)
5151
assert_equal(2, get_names(result).length)
5252
assert_equal(true, get_page_info(result)["hasNextPage"])
5353
assert_equal(false, get_page_info(result)["hasPreviousPage"])
54-
assert_equal("MQ==", get_page_info(result)["startCursor"])
55-
assert_equal("Mg==", get_page_info(result)["endCursor"])
56-
assert_equal("MQ==", get_first_cursor(result))
57-
assert_equal("Mg==", get_last_cursor(result))
54+
assert_equal("MQ", get_page_info(result)["startCursor"])
55+
assert_equal("Mg", get_page_info(result)["endCursor"])
56+
assert_equal("MQ", get_first_cursor(result))
57+
assert_equal("Mg", get_last_cursor(result))
5858

5959
result = star_wars_query(query_string, "first" => 3)
6060
assert_equal(3, get_names(result).length)
6161
assert_equal(false, get_page_info(result)["hasNextPage"])
6262
assert_equal(false, get_page_info(result)["hasPreviousPage"])
63-
assert_equal("MQ==", get_page_info(result)["startCursor"])
64-
assert_equal("Mw==", get_page_info(result)["endCursor"])
65-
assert_equal("MQ==", get_first_cursor(result))
66-
assert_equal("Mw==", get_last_cursor(result))
63+
assert_equal("MQ", get_page_info(result)["startCursor"])
64+
assert_equal("Mw", get_page_info(result)["endCursor"])
65+
assert_equal("MQ", get_first_cursor(result))
66+
assert_equal("Mw", get_last_cursor(result))
6767
end
6868

6969
it 'provides custom fields on the connection type' do
@@ -288,7 +288,7 @@ def get_last_cursor(result)
288288
assert_equal(first_and_second_names, get_names(result))
289289
assert_equal(false, result["data"]["empire"]["bases"]["pageInfo"]["hasPreviousPage"], "hasPreviousPage is false when last is not specified")
290290

291-
third_cursor = "Mw=="
291+
third_cursor = "Mw"
292292
result = star_wars_query(query_string, "last" => 100, "before" => third_cursor)
293293
assert_equal(first_and_second_names, get_names(result))
294294

@@ -528,19 +528,19 @@ def get_last_cursor(result)
528528
assert_equal(2, get_names(result).length)
529529
assert_equal(true, get_page_info(result)["hasNextPage"])
530530
assert_equal(false, get_page_info(result)["hasPreviousPage"])
531-
assert_equal("MQ==", get_page_info(result)["startCursor"])
532-
assert_equal("Mg==", get_page_info(result)["endCursor"])
533-
assert_equal("MQ==", get_first_cursor(result))
534-
assert_equal("Mg==", get_last_cursor(result))
531+
assert_equal("MQ", get_page_info(result)["startCursor"])
532+
assert_equal("Mg", get_page_info(result)["endCursor"])
533+
assert_equal("MQ", get_first_cursor(result))
534+
assert_equal("Mg", get_last_cursor(result))
535535

536536
result = star_wars_query(query_string, "first" => 3)
537537
assert_equal(3, get_names(result).length)
538538
assert_equal(false, get_page_info(result)["hasNextPage"])
539539
assert_equal(false, get_page_info(result)["hasPreviousPage"])
540-
assert_equal("MQ==", get_page_info(result)["startCursor"])
541-
assert_equal("Mw==", get_page_info(result)["endCursor"])
542-
assert_equal("MQ==", get_first_cursor(result))
543-
assert_equal("Mw==", get_last_cursor(result))
540+
assert_equal("MQ", get_page_info(result)["startCursor"])
541+
assert_equal("Mw", get_page_info(result)["endCursor"])
542+
assert_equal("MQ", get_first_cursor(result))
543+
assert_equal("Mw", get_last_cursor(result))
544544
end
545545

546546
it 'provides custom fields on the connection type' do
@@ -635,8 +635,8 @@ def get_last_cursor(result)
635635
let(:connection) { GraphQL::Relay::RelationConnection.new(StarWars::Base.where(faction_id: 1), {}) }
636636

637637
it "returns the cursor for a node in the connection" do
638-
assert_equal "MQ==", connection.cursor_from_node(StarWars::Base.all[0])
639-
assert_equal "Mg==", connection.cursor_from_node(StarWars::Base.all[1])
638+
assert_equal "MQ", connection.cursor_from_node(StarWars::Base.all[0])
639+
assert_equal "Mg", connection.cursor_from_node(StarWars::Base.all[1])
640640
end
641641

642642
it "raises when the node isn't found" do

0 commit comments

Comments
 (0)