Skip to content

Commit

Permalink
Add query logic in Swift (#227)
Browse files Browse the repository at this point in the history
## 🧰 Changes

- Add query logic in Swift
  - URLQueryItem has url encode logic

## 🧬 QA & Testing

I fixed test code in
`/httpsnippet/src/targets/swift/urlsession/fixtures/`

`npm run test`
  • Loading branch information
zunda-pixel authored Apr 11, 2024
1 parent 1629af6 commit f30e998
Show file tree
Hide file tree
Showing 24 changed files with 143 additions and 49 deletions.
35 changes: 32 additions & 3 deletions src/targets/swift/urlsession/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const urlsession: Client<UrlsessionOptions> = {
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,
Expand Down Expand Up @@ -122,7 +122,36 @@ export const urlsession: Client<UrlsessionOptions> = {

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) {
Expand All @@ -136,7 +165,7 @@ export const urlsession: Client<UrlsessionOptions> = {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
5 changes: 3 additions & 2 deletions src/targets/swift/urlsession/fixtures/application-json.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
5 changes: 3 additions & 2 deletions src/targets/swift/urlsession/fixtures/cookies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
5 changes: 3 additions & 2 deletions src/targets/swift/urlsession/fixtures/custom-method.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
14 changes: 12 additions & 2 deletions src/targets/swift/urlsession/fixtures/full.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
5 changes: 3 additions & 2 deletions src/targets/swift/urlsession/fixtures/headers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
5 changes: 3 additions & 2 deletions src/targets/swift/urlsession/fixtures/http-insecure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
5 changes: 3 additions & 2 deletions src/targets/swift/urlsession/fixtures/indent-option.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
5 changes: 3 additions & 2 deletions src/targets/swift/urlsession/fixtures/json-null-value.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
5 changes: 3 additions & 2 deletions src/targets/swift/urlsession/fixtures/jsonObj-multiline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Original file line number Diff line number Diff line change
Expand Up @@ -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))
5 changes: 3 additions & 2 deletions src/targets/swift/urlsession/fixtures/multipart-data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
5 changes: 3 additions & 2 deletions src/targets/swift/urlsession/fixtures/multipart-file.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Original file line number Diff line number Diff line change
Expand Up @@ -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))
13 changes: 11 additions & 2 deletions src/targets/swift/urlsession/fixtures/nested.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Original file line number Diff line number Diff line change
Expand Up @@ -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))
14 changes: 12 additions & 2 deletions src/targets/swift/urlsession/fixtures/pretty-option.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
12 changes: 10 additions & 2 deletions src/targets/swift/urlsession/fixtures/query-encoded.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
14 changes: 12 additions & 2 deletions src/targets/swift/urlsession/fixtures/query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
5 changes: 3 additions & 2 deletions src/targets/swift/urlsession/fixtures/short.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
5 changes: 3 additions & 2 deletions src/targets/swift/urlsession/fixtures/text-plain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
5 changes: 3 additions & 2 deletions src/targets/swift/urlsession/fixtures/timeout-option.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit f30e998

Please sign in to comment.