Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the default behavior of the encoder to use urlsafe versions #1698

Merged
merged 5 commits into from
Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/graphql/schema/base_64_encoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Schema
# @api private
module Base64Encoder
def self.encode(plaintext, nonce: false)
Base64.strict_encode64(plaintext)
Base64Bp.urlsafe_encode64(plaintext, padding: false)
end

def self.decode(ciphertext, nonce: false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ def get_last_cursor(result)
assert_equal(2, get_names(result).length)
assert_equal(true, get_page_info(result)["hasNextPage"])
assert_equal(false, get_page_info(result)["hasPreviousPage"])
assert_equal("MQ==", get_page_info(result)["startCursor"])
assert_equal("Mg==", get_page_info(result)["endCursor"])
assert_equal("MQ==", get_first_cursor(result))
assert_equal("Mg==", get_last_cursor(result))
assert_equal("MQ", get_page_info(result)["startCursor"])
assert_equal("Mg", get_page_info(result)["endCursor"])
assert_equal("MQ", get_first_cursor(result))
assert_equal("Mg", get_last_cursor(result))

result = star_trek_query(query_string, "first" => 3)
assert_equal(3, get_names(result).length)
assert_equal(false, get_page_info(result)["hasNextPage"])
assert_equal(false, get_page_info(result)["hasPreviousPage"])
assert_equal("MQ==", get_page_info(result)["startCursor"])
assert_equal("Mw==", get_page_info(result)["endCursor"])
assert_equal("MQ==", get_first_cursor(result))
assert_equal("Mw==", get_last_cursor(result))
assert_equal("MQ", get_page_info(result)["startCursor"])
assert_equal("Mw", get_page_info(result)["endCursor"])
assert_equal("MQ", get_first_cursor(result))
assert_equal("Mw", get_last_cursor(result))
end

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

third_cursor = "Mw=="
third_cursor = "Mw"
result = star_trek_query(query_string, "last" => 100, "before" => third_cursor)
assert_equal(first_and_second_names, get_names(result))

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

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

it "raises when the node isn't found" do
Expand Down
18 changes: 9 additions & 9 deletions spec/integration/rails/graphql/relay/array_connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def get_page_info(result, key = "bases")
assert_equal(2, number_of_ships)
assert_equal(true, result["data"]["rebels"]["ships"]["pageInfo"]["hasNextPage"])
assert_equal(false, result["data"]["rebels"]["ships"]["pageInfo"]["hasPreviousPage"])
assert_equal("MQ==", result["data"]["rebels"]["ships"]["pageInfo"]["startCursor"])
assert_equal("Mg==", result["data"]["rebels"]["ships"]["pageInfo"]["endCursor"])
assert_equal("MQ", result["data"]["rebels"]["ships"]["pageInfo"]["startCursor"])
assert_equal("Mg", result["data"]["rebels"]["ships"]["pageInfo"]["endCursor"])

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

result = star_wars_query(query_string, "first" => 100)
assert_equal(false, result["data"]["rebels"]["ships"]["pageInfo"]["hasNextPage"])
assert_equal(false, result["data"]["rebels"]["ships"]["pageInfo"]["hasPreviousPage"])
assert_equal("MQ==", result["data"]["rebels"]["ships"]["pageInfo"]["startCursor"])
assert_equal("NQ==", result["data"]["rebels"]["ships"]["pageInfo"]["endCursor"])
assert_equal("MQ", result["data"]["rebels"]["ships"]["pageInfo"]["startCursor"])
assert_equal("NQ", result["data"]["rebels"]["ships"]["pageInfo"]["endCursor"])
end

it "provides bidirectional_pagination" do
Expand Down Expand Up @@ -166,8 +166,8 @@ def get_page_info(result, key = "bases")

assert_equal(false, result["data"]["rebels"]["ships"]["pageInfo"]["hasNextPage"])
assert_equal(false, result["data"]["rebels"]["ships"]["pageInfo"]["hasPreviousPage"])
assert_equal("MQ==", result["data"]["rebels"]["ships"]["pageInfo"]["startCursor"])
assert_equal("NQ==", result["data"]["rebels"]["ships"]["pageInfo"]["endCursor"])
assert_equal("MQ", result["data"]["rebels"]["ships"]["pageInfo"]["startCursor"])
assert_equal("NQ", result["data"]["rebels"]["ships"]["pageInfo"]["endCursor"])
assert_equal(5, result["data"]["rebels"]["ships"]["edges"].length)
end

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

third_cursor = "Mw=="
third_cursor = "Mw"
first_and_second_names = ["Yavin", "Echo Base"]
result = star_wars_query(query_string, "last" => 100, "before" => third_cursor)
assert_equal(first_and_second_names, get_names(result))
Expand Down
6 changes: 3 additions & 3 deletions spec/integration/rails/graphql/relay/base_connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ def decode(str, nonce: false); str.reverse; end
assert_equal "Person/1", conn.decode("1/nosreP")
end

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

assert_equal "UGVyc29uLzE=", conn.encode("Person/1")
assert_equal "Person/1", conn.decode("UGVyc29uLzE=")
assert_equal "UGVyc29uLzE", conn.encode("Person/1")
assert_equal "Person/1", conn.decode("UGVyc29uLzE=") # can decode with padding
end

it "handles trimmed base64" do
Expand Down
44 changes: 22 additions & 22 deletions spec/integration/rails/graphql/relay/page_info_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,63 +46,63 @@ def get_last_cursor(result)
result = star_wars_query(query_string, "first" => 2)
assert_equal(true, get_page_info(result)["hasNextPage"])
assert_equal(false, get_page_info(result)["hasPreviousPage"], "hasPreviousPage is false if 'last' is missing")
assert_equal("MQ==", get_page_info(result)["startCursor"])
assert_equal("Mg==", get_page_info(result)["endCursor"])
assert_equal("MQ", get_page_info(result)["startCursor"])
assert_equal("Mg", get_page_info(result)["endCursor"])

last_cursor = get_last_cursor(result)
result = star_wars_query(query_string, "first" => 100, "after" => last_cursor)
assert_equal(false, get_page_info(result)["hasNextPage"])
assert_equal(false, get_page_info(result)["hasPreviousPage"])
assert_equal("Mw==", get_page_info(result)["startCursor"])
assert_equal("Mw==", get_page_info(result)["endCursor"])
assert_equal("Mw", get_page_info(result)["startCursor"])
assert_equal("Mw", get_page_info(result)["endCursor"])
end

it "hasPreviousPage if there are more items" do
result = star_wars_query(query_string, "last" => 100, "before" => cursor_of_last_base)
assert_equal(false, get_page_info(result)["hasNextPage"])
assert_equal(false, get_page_info(result)["hasPreviousPage"])
assert_equal("MQ==", get_page_info(result)["startCursor"])
assert_equal("Mg==", get_page_info(result)["endCursor"])
assert_equal("MQ", get_page_info(result)["startCursor"])
assert_equal("Mg", get_page_info(result)["endCursor"])

result = star_wars_query(query_string, "last" => 1, "before" => cursor_of_last_base)
assert_equal(false, get_page_info(result)["hasNextPage"])
assert_equal(true, get_page_info(result)["hasPreviousPage"])
assert_equal("Mg==", get_page_info(result)["startCursor"])
assert_equal("Mg==", get_page_info(result)["endCursor"])
assert_equal("Mg", get_page_info(result)["startCursor"])
assert_equal("Mg", get_page_info(result)["endCursor"])
end

it "has both if first and last are present" do
result = star_wars_query(query_string, "last" => 1, "first" => 1, "before" => cursor_of_last_base)
assert_equal(true, get_page_info(result)["hasNextPage"])
assert_equal(true, get_page_info(result)["hasPreviousPage"])
assert_equal("MQ==", get_page_info(result)["startCursor"])
assert_equal("MQ==", get_page_info(result)["endCursor"])
assert_equal("MQ", get_page_info(result)["startCursor"])
assert_equal("MQ", get_page_info(result)["endCursor"])
end

it "startCursor and endCursor are the cursors of the first and last edge" do
result = star_wars_query(query_string, "first" => 2)
assert_equal(true, get_page_info(result)["hasNextPage"])
assert_equal(false, get_page_info(result)["hasPreviousPage"])
assert_equal("MQ==", get_page_info(result)["startCursor"])
assert_equal("Mg==", get_page_info(result)["endCursor"])
assert_equal("MQ==", get_first_cursor(result))
assert_equal("Mg==", get_last_cursor(result))
assert_equal("MQ", get_page_info(result)["startCursor"])
assert_equal("Mg", get_page_info(result)["endCursor"])
assert_equal("MQ", get_first_cursor(result))
assert_equal("Mg", get_last_cursor(result))

result = star_wars_query(query_string, "first" => 1, "after" => get_page_info(result)["endCursor"])
assert_equal(false, get_page_info(result)["hasNextPage"])
assert_equal(false, get_page_info(result)["hasPreviousPage"])
assert_equal("Mw==", get_page_info(result)["startCursor"])
assert_equal("Mw==", get_page_info(result)["endCursor"])
assert_equal("Mw==", get_first_cursor(result))
assert_equal("Mw==", get_last_cursor(result))
assert_equal("Mw", get_page_info(result)["startCursor"])
assert_equal("Mw", get_page_info(result)["endCursor"])
assert_equal("Mw", get_first_cursor(result))
assert_equal("Mw", get_last_cursor(result))

result = star_wars_query(query_string, "last" => 1, "before" => get_page_info(result)["endCursor"])
assert_equal(false, get_page_info(result)["hasNextPage"])
assert_equal(true, get_page_info(result)["hasPreviousPage"])
assert_equal("Mg==", get_page_info(result)["startCursor"])
assert_equal("Mg==", get_page_info(result)["endCursor"])
assert_equal("Mg==", get_first_cursor(result))
assert_equal("Mg==", get_last_cursor(result))
assert_equal("Mg", get_page_info(result)["startCursor"])
assert_equal("Mg", get_page_info(result)["endCursor"])
assert_equal("Mg", get_first_cursor(result))
assert_equal("Mg", get_last_cursor(result))
end
end

Expand Down
38 changes: 19 additions & 19 deletions spec/integration/rails/graphql/relay/relation_connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ def get_last_cursor(result)
assert_equal(2, get_names(result).length)
assert_equal(true, get_page_info(result)["hasNextPage"])
assert_equal(false, get_page_info(result)["hasPreviousPage"])
assert_equal("MQ==", get_page_info(result)["startCursor"])
assert_equal("Mg==", get_page_info(result)["endCursor"])
assert_equal("MQ==", get_first_cursor(result))
assert_equal("Mg==", get_last_cursor(result))
assert_equal("MQ", get_page_info(result)["startCursor"])
assert_equal("Mg", get_page_info(result)["endCursor"])
assert_equal("MQ", get_first_cursor(result))
assert_equal("Mg", get_last_cursor(result))

result = star_wars_query(query_string, "first" => 3)
assert_equal(3, get_names(result).length)
assert_equal(false, get_page_info(result)["hasNextPage"])
assert_equal(false, get_page_info(result)["hasPreviousPage"])
assert_equal("MQ==", get_page_info(result)["startCursor"])
assert_equal("Mw==", get_page_info(result)["endCursor"])
assert_equal("MQ==", get_first_cursor(result))
assert_equal("Mw==", get_last_cursor(result))
assert_equal("MQ", get_page_info(result)["startCursor"])
assert_equal("Mw", get_page_info(result)["endCursor"])
assert_equal("MQ", get_first_cursor(result))
assert_equal("Mw", get_last_cursor(result))
end

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

third_cursor = "Mw=="
third_cursor = "Mw"
result = star_wars_query(query_string, "last" => 100, "before" => third_cursor)
assert_equal(first_and_second_names, get_names(result))

Expand Down Expand Up @@ -528,19 +528,19 @@ def get_last_cursor(result)
assert_equal(2, get_names(result).length)
assert_equal(true, get_page_info(result)["hasNextPage"])
assert_equal(false, get_page_info(result)["hasPreviousPage"])
assert_equal("MQ==", get_page_info(result)["startCursor"])
assert_equal("Mg==", get_page_info(result)["endCursor"])
assert_equal("MQ==", get_first_cursor(result))
assert_equal("Mg==", get_last_cursor(result))
assert_equal("MQ", get_page_info(result)["startCursor"])
assert_equal("Mg", get_page_info(result)["endCursor"])
assert_equal("MQ", get_first_cursor(result))
assert_equal("Mg", get_last_cursor(result))

result = star_wars_query(query_string, "first" => 3)
assert_equal(3, get_names(result).length)
assert_equal(false, get_page_info(result)["hasNextPage"])
assert_equal(false, get_page_info(result)["hasPreviousPage"])
assert_equal("MQ==", get_page_info(result)["startCursor"])
assert_equal("Mw==", get_page_info(result)["endCursor"])
assert_equal("MQ==", get_first_cursor(result))
assert_equal("Mw==", get_last_cursor(result))
assert_equal("MQ", get_page_info(result)["startCursor"])
assert_equal("Mw", get_page_info(result)["endCursor"])
assert_equal("MQ", get_first_cursor(result))
assert_equal("Mw", get_last_cursor(result))
end

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

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

it "raises when the node isn't found" do
Expand Down