Skip to content

Commit

Permalink
fix multipart/form-data in Swift (#231)
Browse files Browse the repository at this point in the history
## 🧰 Changes

- fix argument name

## 🧬 QA & Testing

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

`npm run test`
  • Loading branch information
zunda-pixel authored Apr 29, 2024
1 parent edf00ff commit 92a7097
Show file tree
Hide file tree
Showing 24 changed files with 33 additions and 33 deletions.
6 changes: 3 additions & 3 deletions src/targets/swift/urlsession/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const urlsession: Client<UrlsessionOptions> = {

case 'application/json':
if (postData.jsonObj) {
push(`${literalDeclaration('parameters', postData.jsonObj, opts)} as [String : Any]`);
push(`${literalDeclaration('parameters', postData.jsonObj, opts)} as [String : Any?]`);
blank();
push('let postData = try JSONSerialization.data(withJSONObject: parameters, options: [])');
blank();
Expand All @@ -87,7 +87,7 @@ export const urlsession: Client<UrlsessionOptions> = {
push('body += "--\\(boundary)\\r\\n"', 1);
push('body += "Content-Disposition:form-data; name=\\"\\(paramName)\\""', 1);
push('if let filename = param["fileName"] {', 1);
push('let contentType = param["content-type"]!', 2);
push('let contentType = param["contentType"]!', 2);
push('let fileContent = try String(contentsOfFile: filename, encoding: .utf8)', 2);
push('body += "; filename=\\"\\(filename)\\"\\r\\n"', 2);
push('body += "Content-Type: \\(contentType)\\r\\n\\r\\n"', 2);
Expand Down Expand Up @@ -150,7 +150,7 @@ export const urlsession: Client<UrlsessionOptions> = {

blank();

push('let (data, response) = try await URLSession.shared.data(for: request)');
push('let (data, _) = try await URLSession.shared.data(for: request)');
push('print(String(decoding: data, as: UTF8.self))');

return join();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ request.timeoutInterval = 10
request.allHTTPHeaderFields = ["content-type": "application/x-www-form-urlencoded"]
request.httpBody = postData

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
4 changes: 2 additions & 2 deletions src/targets/swift/urlsession/fixtures/application-json.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let parameters = [
"nested": ["a": "b"],
"arr_mix": [1, "a", ["arr_mix_nested": []]],
"boolean": false
] as [String : Any]
] as [String : Any?]

let postData = try JSONSerialization.data(withJSONObject: parameters, options: [])

Expand All @@ -18,5 +18,5 @@ request.timeoutInterval = 10
request.allHTTPHeaderFields = ["content-type": "application/json"]
request.httpBody = postData

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
2 changes: 1 addition & 1 deletion src/targets/swift/urlsession/fixtures/cookies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ request.httpMethod = "GET"
request.timeoutInterval = 10
request.allHTTPHeaderFields = ["cookie": "foo=bar; bar=baz"]

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
2 changes: 1 addition & 1 deletion src/targets/swift/urlsession/fixtures/custom-method.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ var request = URLRequest(url: url)
request.httpMethod = "PROPFIND"
request.timeoutInterval = 10

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
2 changes: 1 addition & 1 deletion src/targets/swift/urlsession/fixtures/full.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ request.allHTTPHeaderFields = [
]
request.httpBody = postData

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
2 changes: 1 addition & 1 deletion src/targets/swift/urlsession/fixtures/headers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ request.allHTTPHeaderFields = [
"quoted-value": "\"quoted\" 'string'"
]

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
2 changes: 1 addition & 1 deletion src/targets/swift/urlsession/fixtures/http-insecure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ var request = URLRequest(url: url)
request.httpMethod = "GET"
request.timeoutInterval = 10

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
2 changes: 1 addition & 1 deletion src/targets/swift/urlsession/fixtures/indent-option.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ var request = URLRequest(url: url)
request.httpMethod = "GET"
request.timeoutInterval = 10

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
4 changes: 2 additions & 2 deletions src/targets/swift/urlsession/fixtures/json-null-value.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

let parameters = ["foo": nil] as [String : Any]
let parameters = ["foo": nil] as [String : Any?]

let postData = try JSONSerialization.data(withJSONObject: parameters, options: [])

Expand All @@ -11,5 +11,5 @@ request.timeoutInterval = 10
request.allHTTPHeaderFields = ["content-type": "application/json"]
request.httpBody = postData

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
4 changes: 2 additions & 2 deletions src/targets/swift/urlsession/fixtures/jsonObj-multiline.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

let parameters = ["foo": "bar"] as [String : Any]
let parameters = ["foo": "bar"] as [String : Any?]

let postData = try JSONSerialization.data(withJSONObject: parameters, options: [])

Expand All @@ -11,5 +11,5 @@ request.timeoutInterval = 10
request.allHTTPHeaderFields = ["content-type": "application/json"]
request.httpBody = postData

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

let parameters = ["foo": nil] as [String : Any]
let parameters = ["foo": nil] as [String : Any?]

let postData = try JSONSerialization.data(withJSONObject: parameters, options: [])

Expand All @@ -11,5 +11,5 @@ request.timeoutInterval = 10
request.allHTTPHeaderFields = ["content-type": "application/json"]
request.httpBody = postData

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
4 changes: 2 additions & 2 deletions src/targets/swift/urlsession/fixtures/multipart-data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ for param in parameters {
body += "--\(boundary)\r\n"
body += "Content-Disposition:form-data; name=\"\(paramName)\""
if let filename = param["fileName"] {
let contentType = param["content-type"]!
let contentType = param["contentType"]!
let fileContent = try String(contentsOfFile: filename, encoding: .utf8)
body += "; filename=\"\(filename)\"\r\n"
body += "Content-Type: \(contentType)\r\n\r\n"
Expand All @@ -40,5 +40,5 @@ request.timeoutInterval = 10
request.allHTTPHeaderFields = ["content-type": "multipart/form-data; boundary=---011000010111000001101001"]
request.httpBody = postData

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
4 changes: 2 additions & 2 deletions src/targets/swift/urlsession/fixtures/multipart-file.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ for param in parameters {
body += "--\(boundary)\r\n"
body += "Content-Disposition:form-data; name=\"\(paramName)\""
if let filename = param["fileName"] {
let contentType = param["content-type"]!
let contentType = param["contentType"]!
let fileContent = try String(contentsOfFile: filename, encoding: .utf8)
body += "; filename=\"\(filename)\"\r\n"
body += "Content-Type: \(contentType)\r\n\r\n"
Expand All @@ -35,5 +35,5 @@ request.timeoutInterval = 10
request.allHTTPHeaderFields = ["content-type": "multipart/form-data; boundary=---011000010111000001101001"]
request.httpBody = postData

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = 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 @@ -6,5 +6,5 @@ request.httpMethod = "POST"
request.timeoutInterval = 10
request.allHTTPHeaderFields = ["Content-Type": "multipart/form-data"]

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = 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 @@ -15,7 +15,7 @@ for param in parameters {
body += "--\(boundary)\r\n"
body += "Content-Disposition:form-data; name=\"\(paramName)\""
if let filename = param["fileName"] {
let contentType = param["content-type"]!
let contentType = param["contentType"]!
let fileContent = try String(contentsOfFile: filename, encoding: .utf8)
body += "; filename=\"\(filename)\"\r\n"
body += "Content-Type: \(contentType)\r\n\r\n"
Expand All @@ -34,5 +34,5 @@ request.timeoutInterval = 10
request.allHTTPHeaderFields = ["Content-Type": "multipart/form-data; boundary=---011000010111000001101001"]
request.httpBody = postData

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
2 changes: 1 addition & 1 deletion src/targets/swift/urlsession/fixtures/nested.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ var request = URLRequest(url: components.url!)
request.httpMethod = "GET"
request.timeoutInterval = 10

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = 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 @@ -6,5 +6,5 @@ request.httpMethod = "POST"
request.timeoutInterval = 10
request.allHTTPHeaderFields = ["content-type": "application/json"]

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
2 changes: 1 addition & 1 deletion src/targets/swift/urlsession/fixtures/pretty-option.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ request.timeoutInterval = 10
request.allHTTPHeaderFields = ["cookie": "foo=bar; bar=baz", "accept": "application/json", "content-type": "application/x-www-form-urlencoded"]
request.httpBody = postData

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
2 changes: 1 addition & 1 deletion src/targets/swift/urlsession/fixtures/query-encoded.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ var request = URLRequest(url: components.url!)
request.httpMethod = "GET"
request.timeoutInterval = 10

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
2 changes: 1 addition & 1 deletion src/targets/swift/urlsession/fixtures/query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ var request = URLRequest(url: components.url!)
request.httpMethod = "GET"
request.timeoutInterval = 10

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
2 changes: 1 addition & 1 deletion src/targets/swift/urlsession/fixtures/short.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ var request = URLRequest(url: url)
request.httpMethod = "GET"
request.timeoutInterval = 10

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
2 changes: 1 addition & 1 deletion src/targets/swift/urlsession/fixtures/text-plain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ request.timeoutInterval = 10
request.allHTTPHeaderFields = ["content-type": "text/plain"]
request.httpBody = postData

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))
2 changes: 1 addition & 1 deletion src/targets/swift/urlsession/fixtures/timeout-option.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ var request = URLRequest(url: url)
request.httpMethod = "GET"
request.timeoutInterval = 5

let (data, response) = try await URLSession.shared.data(for: request)
let (data, _) = try await URLSession.shared.data(for: request)
print(String(decoding: data, as: UTF8.self))

0 comments on commit 92a7097

Please sign in to comment.