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

Fix encoding of + in parameters for GET urls #1653

Merged
merged 4 commits into from
Mar 2, 2021
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: 2 additions & 0 deletions Sources/Apollo/GraphQLGETTransformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public struct GraphQLGETTransformer {
}

components.queryItems = queryItems
components.percentEncodedQuery =
components.percentEncodedQuery?.replacingOccurrences(of: "+", with: "%2B")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this needs to actually be replaced in the query items rather than in the percent encoded query for it to essentially get re-percent-encoded so it's not read by the server as a raw string.


return components.url
}
Expand Down
44 changes: 44 additions & 0 deletions Tests/ApolloTests/GETTransformerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,50 @@ class GETTransformerTests: XCTestCase {

XCTAssertTrue(queryString)
}

func testEncodingQueryWithParameterWithPlusSignEncoded() throws {
let operation = HeroNameQuery(episode: .empire)

let extensions: GraphQLMap = [
"testParam": "+Test+Test"
]

let body: GraphQLMap = [
"query": operation.queryDocument,
"variables": operation.variables,
"extensions": extensions
]

let transformer = GraphQLGETTransformer(body: body, url: self.url)

let url = transformer.createGetURL()

let expected = "http://localhost:8080/graphql?extensions=%7B%22testParam%22:%22%2BTest%2BTest%22%7D&query=query%20HeroName($episode:%20Episode)%20%7B%0A%20%20hero(episode:%20$episode)%20%7B%0A%20%20%20%20__typename%0A%20%20%20%20name%0A%20%20%7D%0A%7D&variables=%7B%22episode%22:%22EMPIRE%22%7D"

XCTAssertEqual(url?.absoluteString, expected)
}

func testEncodingQueryWithParameterWithAmpersandEncoded() throws {
let operation = HeroNameQuery(episode: .empire)

let extensions: GraphQLMap = [
"testParam": "Test&Test"
]

let body: GraphQLMap = [
"query": operation.queryDocument,
"variables": operation.variables,
"extensions": extensions
]

let transformer = GraphQLGETTransformer(body: body, url: self.url)

let url = transformer.createGetURL()

let expected = "http://localhost:8080/graphql?extensions=%7B%22testParam%22:%22Test%26Test%22%7D&query=query%20HeroName($episode:%20Episode)%20%7B%0A%20%20hero(episode:%20$episode)%20%7B%0A%20%20%20%20__typename%0A%20%20%20%20name%0A%20%20%7D%0A%7D&variables=%7B%22episode%22:%22EMPIRE%22%7D"

XCTAssertEqual(url?.absoluteString, expected)
}

func testEncodingQueryWith2DWOQueryParameter() throws {
let operation = HeroNameQuery(episode: .empire)
Expand Down