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

Send GET request parameters as query params [SDK-3324] #678

Merged
merged 2 commits into from
Apr 27, 2022
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
19 changes: 14 additions & 5 deletions Auth0/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,20 @@ public struct Request<T, E: Auth0APIError>: Requestable {
var request: URLRequest {
let request = NSMutableURLRequest(url: url)
request.httpMethod = method
if !parameters.isEmpty, let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) {
request.httpBody = httpBody
#if DEBUG
URLProtocol.setProperty(parameters, forKey: parameterPropertyKey, in: request)
#endif
if !parameters.isEmpty {
if method.caseInsensitiveCompare("GET") == .orderedSame {
var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true)
var queryItems = urlComponents?.queryItems ?? []
let newQueryItems = parameters.map { URLQueryItem(name: $0.key, value: String(describing: $0.value)) }
queryItems.append(contentsOf: newQueryItems)
urlComponents?.queryItems = queryItems
request.url = urlComponents?.url ?? url
} else if let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) {
request.httpBody = httpBody
#if DEBUG
URLProtocol.setProperty(parameters, forKey: parameterPropertyKey, in: request)
#endif
}
}
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
headers.forEach { name, value in request.setValue(value, forHTTPHeaderField: name) }
Expand Down
16 changes: 16 additions & 0 deletions Auth0Tests/RequestSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ class RequestSpec: QuickSpec {
let request = Request(session: URLSession.shared, url: Url, method: "GET", handle: plainJson, parameters: [:], logger: nil, telemetry: Telemetry())
expect(request.parameters(["scope": "email phone"]).parameters["scope"] as? String) == "openid email phone"
}

it("should add the parameters as query parameters") {
let request = Request(session: URLSession.shared, url: Url, method: "GET", handle: plainJson, parameters: ["foo": "bar"], logger: nil, telemetry: Telemetry())
expect(request.request.url?.query) == "foo=bar"
}

it("should append the parameters to the existing query parameters") {
let request = Request(session: URLSession.shared, url: URL(string: "\(Url.absoluteString)?foo=bar")!, method: "GET", handle: plainJson, parameters: ["baz": "qux"], logger: nil, telemetry: Telemetry())
expect(request.request.url?.query) == "foo=bar&baz=qux"
}

it("should add the parameters to the request body") {
let request = Request(session: URLSession.shared, url: Url, method: "POST", handle: plainJson, parameters: ["foo": "bar"], logger: nil, telemetry: Telemetry())
let body = try! JSONSerialization.jsonObject(with: request.request.httpBody!, options: []) as! [String: Any]
expect(body["foo"] as? String) == "bar"
}
}

context("headers") {
Expand Down