diff --git a/src/targets/swift/urlsession/client.ts b/src/targets/swift/urlsession/client.ts index e980cf473..466f96b82 100644 --- a/src/targets/swift/urlsession/client.ts +++ b/src/targets/swift/urlsession/client.ts @@ -25,7 +25,7 @@ export const urlsession: Client = { description: "Foundation's URLSession request", extname: '.swift', }, - convert: ({ allHeaders, postData, fullUrl, method }, options) => { + convert: ({ allHeaders, postData, uriObj, queryObj, method }, options) => { const opts = { indent: ' ', pretty: true, @@ -122,7 +122,36 @@ export const urlsession: Client = { blank(); - push(`var request = URLRequest(url: URL(string: "${fullUrl}")!)`); + push(`let url = URL(string: "${uriObj.href}")!`); + + const queries = queryObj ? Object.entries(queryObj) : []; + if (queries.length < 1) { + push('var request = URLRequest(url: url)'); + } else { + push('var components = URLComponents(url: url, resolvingAgainstBaseURL: true)!'); + push('let queryItems: [URLQueryItem] = ['); + + queries.forEach(query => { + const key = query[0]; + const value = query[1]; + switch (Object.prototype.toString.call(value)) { + case '[object String]': + push(`${opts.indent}URLQueryItem(name: "${key}", value: "${value}"),`); + break; + case '[object Array]': + value.forEach(val => { + push(`${opts.indent}URLQueryItem(name: "${key}", value: "${val}"),`); + }); + break; + } + }); + push(']'); + push('components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems'); + + blank(); + push('var request = URLRequest(url: components.url!)'); + } + push(`request.httpMethod = "${method}"`); if (req.hasHeaders) { @@ -136,7 +165,7 @@ export const urlsession: Client = { blank(); // Retrieving the shared session will be less verbose than creating a new one. - push('let (data, response) = try await URLSession.shared.data(with: request)'); + push('let (data, response) = try await URLSession.shared.data(for: request)'); push('print(String(decoding: data, as: UTF8.self))'); blank(); diff --git a/src/targets/swift/urlsession/fixtures/application-form-encoded.swift b/src/targets/swift/urlsession/fixtures/application-form-encoded.swift index 5fcc2d295..2e5a5630b 100644 --- a/src/targets/swift/urlsession/fixtures/application-form-encoded.swift +++ b/src/targets/swift/urlsession/fixtures/application-form-encoded.swift @@ -8,10 +8,11 @@ let headers = ["content-type": "application/x-www-form-urlencoded"] var postData = Data("foo=bar".utf8) postData.append(Data("&hello=world".utf8)) -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/application-json.swift b/src/targets/swift/urlsession/fixtures/application-json.swift index 7f97c825d..d2c6b3ccb 100644 --- a/src/targets/swift/urlsession/fixtures/application-json.swift +++ b/src/targets/swift/urlsession/fixtures/application-json.swift @@ -15,10 +15,11 @@ let parameters = [ let postData = try JSONSerialization.data(withJSONObject: parameters, options: []) -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/cookies.swift b/src/targets/swift/urlsession/fixtures/cookies.swift index 483e7bbe5..dd0f7cdb2 100644 --- a/src/targets/swift/urlsession/fixtures/cookies.swift +++ b/src/targets/swift/urlsession/fixtures/cookies.swift @@ -5,9 +5,10 @@ import Foundation let headers = ["cookie": "foo=bar; bar=baz"] -var request = URLRequest(url: URL(string: "https://httpbin.org/cookies")!) +let url = URL(string: "https://httpbin.org/cookies")! +var request = URLRequest(url: url) request.httpMethod = "GET" request.allHTTPHeaderFields = headers -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/custom-method.swift b/src/targets/swift/urlsession/fixtures/custom-method.swift index 5bd19a3bd..40b4a86f0 100644 --- a/src/targets/swift/urlsession/fixtures/custom-method.swift +++ b/src/targets/swift/urlsession/fixtures/custom-method.swift @@ -3,8 +3,9 @@ import Foundation import FoundationNetworking #endif -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "PROPFIND" -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/full.swift b/src/targets/swift/urlsession/fixtures/full.swift index 39cba3459..7df94abda 100644 --- a/src/targets/swift/urlsession/fixtures/full.swift +++ b/src/targets/swift/urlsession/fixtures/full.swift @@ -11,10 +11,20 @@ let headers = [ let postData = Data("foo=bar".utf8) -var request = URLRequest(url: URL(string: "https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value")!) +let url = URL(string: "https://httpbin.org/anything?key=value")! +var components = URLComponents(url: url, resolvingAgainstBaseURL: true)! +let queryItems: [URLQueryItem] = [ + URLQueryItem(name: "foo", value: "bar"), + URLQueryItem(name: "foo", value: "baz"), + URLQueryItem(name: "baz", value: "abc"), + URLQueryItem(name: "key", value: "value"), +] +components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems + +var request = URLRequest(url: components.url!) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/headers.swift b/src/targets/swift/urlsession/fixtures/headers.swift index 07601f12c..da999ee4a 100644 --- a/src/targets/swift/urlsession/fixtures/headers.swift +++ b/src/targets/swift/urlsession/fixtures/headers.swift @@ -10,9 +10,10 @@ let headers = [ "quoted-value": "\"quoted\" 'string'" ] -var request = URLRequest(url: URL(string: "https://httpbin.org/headers")!) +let url = URL(string: "https://httpbin.org/headers")! +var request = URLRequest(url: url) request.httpMethod = "GET" request.allHTTPHeaderFields = headers -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/http-insecure.swift b/src/targets/swift/urlsession/fixtures/http-insecure.swift index 42687b86f..e48266733 100644 --- a/src/targets/swift/urlsession/fixtures/http-insecure.swift +++ b/src/targets/swift/urlsession/fixtures/http-insecure.swift @@ -3,8 +3,9 @@ import Foundation import FoundationNetworking #endif -var request = URLRequest(url: URL(string: "http://httpbin.org/anything")!) +let url = URL(string: "http://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "GET" -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/indent-option.swift b/src/targets/swift/urlsession/fixtures/indent-option.swift index b58e44740..d7ae52740 100644 --- a/src/targets/swift/urlsession/fixtures/indent-option.swift +++ b/src/targets/swift/urlsession/fixtures/indent-option.swift @@ -3,8 +3,9 @@ import Foundation import FoundationNetworking #endif -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "GET" -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/json-null-value.swift b/src/targets/swift/urlsession/fixtures/json-null-value.swift index 4ba8885f1..caac59fed 100644 --- a/src/targets/swift/urlsession/fixtures/json-null-value.swift +++ b/src/targets/swift/urlsession/fixtures/json-null-value.swift @@ -8,10 +8,11 @@ let parameters = ["foo": nil] as [String : Any] let postData = try JSONSerialization.data(withJSONObject: parameters, options: []) -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/jsonObj-multiline.swift b/src/targets/swift/urlsession/fixtures/jsonObj-multiline.swift index 016fff7fd..ed53b1ef9 100644 --- a/src/targets/swift/urlsession/fixtures/jsonObj-multiline.swift +++ b/src/targets/swift/urlsession/fixtures/jsonObj-multiline.swift @@ -8,10 +8,11 @@ let parameters = ["foo": "bar"] as [String : Any] let postData = try JSONSerialization.data(withJSONObject: parameters, options: []) -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/jsonObj-null-value.swift b/src/targets/swift/urlsession/fixtures/jsonObj-null-value.swift index 4ba8885f1..caac59fed 100644 --- a/src/targets/swift/urlsession/fixtures/jsonObj-null-value.swift +++ b/src/targets/swift/urlsession/fixtures/jsonObj-null-value.swift @@ -8,10 +8,11 @@ let parameters = ["foo": nil] as [String : Any] let postData = try JSONSerialization.data(withJSONObject: parameters, options: []) -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/multipart-data.swift b/src/targets/swift/urlsession/fixtures/multipart-data.swift index d858b314f..529bf7998 100644 --- a/src/targets/swift/urlsession/fixtures/multipart-data.swift +++ b/src/targets/swift/urlsession/fixtures/multipart-data.swift @@ -39,10 +39,11 @@ for param in parameters { } } -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/multipart-file.swift b/src/targets/swift/urlsession/fixtures/multipart-file.swift index af9bc9f87..ba9c867d3 100644 --- a/src/targets/swift/urlsession/fixtures/multipart-file.swift +++ b/src/targets/swift/urlsession/fixtures/multipart-file.swift @@ -34,10 +34,11 @@ for param in parameters { } } -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/multipart-form-data-no-params.swift b/src/targets/swift/urlsession/fixtures/multipart-form-data-no-params.swift index 9ff1fa7bc..eb2276acb 100644 --- a/src/targets/swift/urlsession/fixtures/multipart-form-data-no-params.swift +++ b/src/targets/swift/urlsession/fixtures/multipart-form-data-no-params.swift @@ -5,9 +5,10 @@ import Foundation let headers = ["Content-Type": "multipart/form-data"] -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/multipart-form-data.swift b/src/targets/swift/urlsession/fixtures/multipart-form-data.swift index 3b11e3895..e9d349eb7 100644 --- a/src/targets/swift/urlsession/fixtures/multipart-form-data.swift +++ b/src/targets/swift/urlsession/fixtures/multipart-form-data.swift @@ -33,10 +33,11 @@ for param in parameters { } } -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/nested.swift b/src/targets/swift/urlsession/fixtures/nested.swift index 56330a19e..a67a057bd 100644 --- a/src/targets/swift/urlsession/fixtures/nested.swift +++ b/src/targets/swift/urlsession/fixtures/nested.swift @@ -3,8 +3,17 @@ import Foundation import FoundationNetworking #endif -var request = URLRequest(url: URL(string: "https://httpbin.org/anything?foo%5Bbar%5D=baz%2Czap&fiz=buz&key=value")!) +let url = URL(string: "https://httpbin.org/anything")! +var components = URLComponents(url: url, resolvingAgainstBaseURL: true)! +let queryItems: [URLQueryItem] = [ + URLQueryItem(name: "foo[bar]", value: "baz,zap"), + URLQueryItem(name: "fiz", value: "buz"), + URLQueryItem(name: "key", value: "value"), +] +components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems + +var request = URLRequest(url: components.url!) request.httpMethod = "GET" -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/postdata-malformed.swift b/src/targets/swift/urlsession/fixtures/postdata-malformed.swift index 71b4f4a9b..29df4fd87 100644 --- a/src/targets/swift/urlsession/fixtures/postdata-malformed.swift +++ b/src/targets/swift/urlsession/fixtures/postdata-malformed.swift @@ -5,9 +5,10 @@ import Foundation let headers = ["content-type": "application/json"] -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/pretty-option.swift b/src/targets/swift/urlsession/fixtures/pretty-option.swift index 3dcf9c52b..3da5fca59 100644 --- a/src/targets/swift/urlsession/fixtures/pretty-option.swift +++ b/src/targets/swift/urlsession/fixtures/pretty-option.swift @@ -7,10 +7,20 @@ let headers = ["cookie": "foo=bar; bar=baz", "accept": "application/json", "cont let postData = Data("foo=bar".utf8) -var request = URLRequest(url: URL(string: "https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value")!) +let url = URL(string: "https://httpbin.org/anything?key=value")! +var components = URLComponents(url: url, resolvingAgainstBaseURL: true)! +let queryItems: [URLQueryItem] = [ + URLQueryItem(name: "foo", value: "bar"), + URLQueryItem(name: "foo", value: "baz"), + URLQueryItem(name: "baz", value: "abc"), + URLQueryItem(name: "key", value: "value"), +] +components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems + +var request = URLRequest(url: components.url!) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/query-encoded.swift b/src/targets/swift/urlsession/fixtures/query-encoded.swift index d57bd20da..a264a7d2f 100644 --- a/src/targets/swift/urlsession/fixtures/query-encoded.swift +++ b/src/targets/swift/urlsession/fixtures/query-encoded.swift @@ -3,8 +3,16 @@ import Foundation import FoundationNetworking #endif -var request = URLRequest(url: URL(string: "https://httpbin.org/anything?startTime=2019-06-13T19%3A08%3A25.455Z&endTime=2015-09-15T14%3A00%3A12-04%3A00")!) +let url = URL(string: "https://httpbin.org/anything")! +var components = URLComponents(url: url, resolvingAgainstBaseURL: true)! +let queryItems: [URLQueryItem] = [ + URLQueryItem(name: "startTime", value: "2019-06-13T19%3A08%3A25.455Z"), + URLQueryItem(name: "endTime", value: "2015-09-15T14%3A00%3A12-04%3A00"), +] +components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems + +var request = URLRequest(url: components.url!) request.httpMethod = "GET" -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/query.swift b/src/targets/swift/urlsession/fixtures/query.swift index a8a0b801e..8e5f34f2d 100644 --- a/src/targets/swift/urlsession/fixtures/query.swift +++ b/src/targets/swift/urlsession/fixtures/query.swift @@ -3,8 +3,18 @@ import Foundation import FoundationNetworking #endif -var request = URLRequest(url: URL(string: "https://httpbin.org/anything?foo=bar&foo=baz&baz=abc&key=value")!) +let url = URL(string: "https://httpbin.org/anything?key=value")! +var components = URLComponents(url: url, resolvingAgainstBaseURL: true)! +let queryItems: [URLQueryItem] = [ + URLQueryItem(name: "foo", value: "bar"), + URLQueryItem(name: "foo", value: "baz"), + URLQueryItem(name: "baz", value: "abc"), + URLQueryItem(name: "key", value: "value"), +] +components.queryItems = components.queryItems.map { $0 + queryItems } ?? queryItems + +var request = URLRequest(url: components.url!) request.httpMethod = "GET" -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/short.swift b/src/targets/swift/urlsession/fixtures/short.swift index b58e44740..d7ae52740 100644 --- a/src/targets/swift/urlsession/fixtures/short.swift +++ b/src/targets/swift/urlsession/fixtures/short.swift @@ -3,8 +3,9 @@ import Foundation import FoundationNetworking #endif -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "GET" -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/text-plain.swift b/src/targets/swift/urlsession/fixtures/text-plain.swift index 0c6b04874..725832782 100644 --- a/src/targets/swift/urlsession/fixtures/text-plain.swift +++ b/src/targets/swift/urlsession/fixtures/text-plain.swift @@ -7,10 +7,11 @@ let headers = ["content-type": "text/plain"] let postData = Data("Hello World".utf8) -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "POST" request.allHTTPHeaderFields = headers request.httpBody = postData -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self)) diff --git a/src/targets/swift/urlsession/fixtures/timeout-option.swift b/src/targets/swift/urlsession/fixtures/timeout-option.swift index b58e44740..d7ae52740 100644 --- a/src/targets/swift/urlsession/fixtures/timeout-option.swift +++ b/src/targets/swift/urlsession/fixtures/timeout-option.swift @@ -3,8 +3,9 @@ import Foundation import FoundationNetworking #endif -var request = URLRequest(url: URL(string: "https://httpbin.org/anything")!) +let url = URL(string: "https://httpbin.org/anything")! +var request = URLRequest(url: url) request.httpMethod = "GET" -let (data, response) = try await URLSession.shared.data(with: request) +let (data, response) = try await URLSession.shared.data(for: request) print(String(decoding: data, as: UTF8.self))