Skip to content

Commit

Permalink
tech(swiftlint): Fix warnings (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjechris authored Nov 24, 2022
1 parent ecf2743 commit 20b9a74
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- main

jobs:
test:
doc:
runs-on: macos-latest

steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
uses: actions/checkout@v2

- name: Lint code
uses: swiftlint lint
run: swiftlint lint

- name: Run tests
run: swift test --enable-test-discovery
6 changes: 4 additions & 2 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ opt_in_rules:
- empty_count
- indentation_width

line_length: 120
warning_threshold: 0
line_length:
warning: 120
ignores_comments: true
warning_threshold: 1
23 changes: 16 additions & 7 deletions Sources/SimpleHTTP/MultipartForm/MultipartFormData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,25 @@ public struct MultipartFormData {
private func mimeType(from url: URL) -> String {
if #available(iOS 14.0, *), #available(macOS 11.0, *) {
guard let type = UTType(filenameExtension: url.pathExtension), let mime = type.preferredMIMEType else {
return HTTPContentType.octetStream.value
return HTTPContentType.octetStream.value
}

return mime
} else {
if let id = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, url.pathExtension as CFString, nil)?.takeRetainedValue(),
let contentType = UTTypeCopyPreferredTagWithClass(id, kUTTagClassMIMEType)?.takeRetainedValue() {
return contentType as String
}
return HTTPContentType.octetStream.value
}

let contentType = UTTypeCreatePreferredIdentifierForTag(
kUTTagClassFilenameExtension,
url.pathExtension as CFString,
nil
)
.flatMap { UTTypeCopyPreferredTagWithClass($0.takeRetainedValue(), kUTTagClassMIMEType) }
.map { $0.takeRetainedValue() }

if let contentType = contentType {
return contentType as String
}

return HTTPContentType.octetStream.value
}

}
Expand Down
10 changes: 5 additions & 5 deletions Sources/SimpleHTTP/MultipartForm/MultipartFormDataEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ struct MultipartFormDataEncoder {
var encoded = Data()

if var first = bodyParts.first {
first.hasInitialBoundary = true
bodyParts[0] = first
first.hasInitialBoundary = true
bodyParts[0] = first
}

if var last = bodyParts.last {
last.hasFinalBoundary = true
bodyParts[bodyParts.count - 1] = last
last.hasFinalBoundary = true
bodyParts[bodyParts.count - 1] = last
}

for bodyPart in bodyParts {
encoded.append(try encodeBodyPart(bodyPart))
encoded.append(try encodeBodyPart(bodyPart))
}

return encoded
Expand Down
6 changes: 3 additions & 3 deletions Sources/SimpleHTTP/MultipartForm/URLRequest+Multipart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import FoundationNetworking

extension URLRequest {
public mutating func multipartBody(_ body: MultipartFormData) throws {
var multipartEncode = MultipartFormDataEncoder(body: body)
var multipartEncode = MultipartFormDataEncoder(body: body)

httpBody = try multipartEncode.encode()
httpBody = try multipartEncode.encode()

setHeaders([.contentType: HTTPContentType.multipart(boundary: body.boundary).value])
setHeaders([.contentType: HTTPContentType.multipart(boundary: body.boundary).value])
}
}
2 changes: 1 addition & 1 deletion Sources/SimpleHTTP/Request/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public struct Path: Equatable, ExpressibleByStringLiteral, ExpressibleByStringIn
self.init(value: stringInterpolation.description)
}

public static func ==(lhs: Path, rhs: String) -> Bool {
public static func == (lhs: Path, rhs: String) -> Bool {
lhs.value == rhs
}
}
6 changes: 5 additions & 1 deletion Sources/SimpleHTTP/Request/Request+URLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ extension Request {
/// - Parameter encoder: the encoder to use to encode the body is present
/// - Parameter relativeTo: the base URL to append to the request path
/// - Parameter accepting: if not nil will be used to set "Accept" header value
public func toURLRequest(encoder: ContentDataEncoder, relativeTo baseURL: URL, accepting: ContentDataDecoder? = nil) throws -> URLRequest {
public func toURLRequest(
encoder: ContentDataEncoder,
relativeTo baseURL: URL,
accepting: ContentDataDecoder? = nil
) throws -> URLRequest {
let request = try toURLRequest(encoder: encoder)
.relativeTo(baseURL)

Expand Down
2 changes: 2 additions & 0 deletions Sources/SimpleHTTP/Response/URLSession+DataResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ extension URLSession {
public func data(for urlRequest: URLRequest) async throws -> URLDataResponse {
let (data, response) = try await data(for: urlRequest)

// swiftlint:disable force_cast
return URLDataResponse(data: data, response: response as! HTTPURLResponse)
// swiftlint:enable force_cast
}
}
2 changes: 1 addition & 1 deletion Sources/SimpleHTTP/Session/SessionConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public struct SessionConfiguration {
interceptors: CompositeInterceptor = [],
dataError: DataError.Type
) {
self.init(encoder: encoder, decoder: decoder, decodingQueue: decodingQueue, interceptors: interceptors)
self.init(encoder: encoder, decoder: decoder, decodingQueue: decodingQueue, interceptors: interceptors)
self.errorConverter = {
try decoder.decode(dataError, from: $0)
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/SimpleHTTPFoundation/HTTP/HTTPHeader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ extension HTTPHeader {
public static var contentDisposition: Self = "Content-Disposition"
}

// swiftlint:disable line_length
@available(*, unavailable, message: "This is a reserved header. See https://developer.apple.com/documentation/foundation/nsurlrequest#1776617")
// swiftlint:enable line_length
extension HTTPHeader {
public static let connection: Self = "Connection"
public static let contentLength: Self = "Content-Length"
Expand Down
4 changes: 4 additions & 0 deletions Tests/.swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
disabled_rules:
- force_try
- force_cast
- xctfail_message
12 changes: 6 additions & 6 deletions Tests/SimpleHTTPTests/HTTPURLResponse+Fixture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import FoundationNetworking
#endif

extension HTTPURLResponse {
convenience init(statusCode: Int) {
self.init(url: URL(string: "/")!, statusCode: statusCode, httpVersion: nil, headerFields: nil)!
}
convenience init(statusCode: Int) {
self.init(url: URL(string: "/")!, statusCode: statusCode, httpVersion: nil, headerFields: nil)!
}
}

extension URLResponse {
static let success = HTTPURLResponse(statusCode: 200)
static let unauthorized = HTTPURLResponse(statusCode: 401)
static let notFound = HTTPURLResponse(statusCode: 404)
static let success = HTTPURLResponse(statusCode: 200)
static let unauthorized = HTTPURLResponse(statusCode: 401)
static let notFound = HTTPURLResponse(statusCode: 404)
}
104 changes: 52 additions & 52 deletions Tests/SimpleHTTPTests/MultipartForm/MultipartFormDataTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,66 @@ import XCTest

class MultipartFormDataTest: XCTestCase {

let crlf = EncodingCharacters.crlf
let crlf = EncodingCharacters.crlf

func test_addData_withoutFileNameAndMimeType_expectOneBodyPart() throws {
let boundary = "boundary"
var multipart = MultipartFormData(boundary: boundary)
let data = "I'm pjechris, Nice to meet you"
let name = "data"
let expectedHeaders: [Header] = [
Header(name: .contentDisposition, value: "form-data; name=\"\(name)\"")
]
func test_addData_withoutFileNameAndMimeType_expectOneBodyPart() throws {
let boundary = "boundary"
var multipart = MultipartFormData(boundary: boundary)
let data = "I'm pjechris, Nice to meet you"
let name = "data"
let expectedHeaders: [Header] = [
Header(name: .contentDisposition, value: "form-data; name=\"\(name)\"")
]

multipart.add(data: Data(data.utf8), name: name)
multipart.add(data: Data(data.utf8), name: name)

XCTAssertEqual(multipart.bodyParts.count, 1)
let bodyPart = try XCTUnwrap(multipart.bodyParts.first)
XCTAssertEqual(bodyPart.headers, expectedHeaders)
}
XCTAssertEqual(multipart.bodyParts.count, 1)
let bodyPart = try XCTUnwrap(multipart.bodyParts.first)
XCTAssertEqual(bodyPart.headers, expectedHeaders)
}

func test_addData_oneWithoutFileNameAndMimeType_secondWithAllValue_expect2BodyParts() throws {
let boundary = "boundary"
var multipart = MultipartFormData(boundary: boundary)
let data1 = "Swift"
let name1 = "swift"
let data2 = "Combine"
let name2 = "combine"
let fileName2 = "combine.txt"
let mimeType2 = "text/plain"
let expectedFirstBodyPartHeaders: [Header] = [
Header(name: .contentDisposition, value: "form-data; name=\"\(name1)\"")
]
let expectedLastBodyPartHeaders: [Header] = [
Header(name: .contentDisposition, value: "form-data; name=\"\(name2)\"; filename=\"\(fileName2)\""),
Header(name: .contentType, value: mimeType2)
]
func test_addData_oneWithoutFileNameAndMimeType_secondWithAllValue_expect2BodyParts() throws {
let boundary = "boundary"
var multipart = MultipartFormData(boundary: boundary)
let data1 = "Swift"
let name1 = "swift"
let data2 = "Combine"
let name2 = "combine"
let fileName2 = "combine.txt"
let mimeType2 = "text/plain"
let expectedFirstBodyPartHeaders: [Header] = [
Header(name: .contentDisposition, value: "form-data; name=\"\(name1)\"")
]
let expectedLastBodyPartHeaders: [Header] = [
Header(name: .contentDisposition, value: "form-data; name=\"\(name2)\"; filename=\"\(fileName2)\""),
Header(name: .contentType, value: mimeType2)
]

multipart.add(data: Data(data1.utf8), name: name1)
multipart.add(data: Data(data2.utf8), name: name2, fileName: fileName2, mimeType: mimeType2)
multipart.add(data: Data(data1.utf8), name: name1)
multipart.add(data: Data(data2.utf8), name: name2, fileName: fileName2, mimeType: mimeType2)

XCTAssertEqual(multipart.bodyParts.count, 2)
let bodyPart1 = try XCTUnwrap(multipart.bodyParts.first)
XCTAssertEqual(bodyPart1.headers, expectedFirstBodyPartHeaders)
let bodyPart2 = try XCTUnwrap(multipart.bodyParts.last)
XCTAssertEqual(bodyPart2.headers, expectedLastBodyPartHeaders)
}
XCTAssertEqual(multipart.bodyParts.count, 2)
let bodyPart1 = try XCTUnwrap(multipart.bodyParts.first)
XCTAssertEqual(bodyPart1.headers, expectedFirstBodyPartHeaders)
let bodyPart2 = try XCTUnwrap(multipart.bodyParts.last)
XCTAssertEqual(bodyPart2.headers, expectedLastBodyPartHeaders)
}

func test_addURL_bodyPart() throws {
let boundary = "boundary"
var multipart = MultipartFormData(boundary: boundary)
let url = URL.Images.swift
let name = "swift"
let expectedHeaders: [Header] = [
Header(name: .contentDisposition, value: "form-data; name=\"\(name)\"; filename=\"swift.png\""),
Header(name: .contentType, value: "image/png")
]
func test_addURL_bodyPart() throws {
let boundary = "boundary"
var multipart = MultipartFormData(boundary: boundary)
let url = URL.Images.swift
let name = "swift"
let expectedHeaders: [Header] = [
Header(name: .contentDisposition, value: "form-data; name=\"\(name)\"; filename=\"swift.png\""),
Header(name: .contentType, value: "image/png")
]

try multipart.add(url: url, name: name)
try multipart.add(url: url, name: name)

XCTAssertEqual(multipart.bodyParts.count, 1)
let bodyPart = try XCTUnwrap(multipart.bodyParts.first)
XCTAssertEqual(bodyPart.headers, expectedHeaders)
}
XCTAssertEqual(multipart.bodyParts.count, 1)
let bodyPart = try XCTUnwrap(multipart.bodyParts.first)
XCTAssertEqual(bodyPart.headers, expectedHeaders)
}

}
5 changes: 4 additions & 1 deletion Tests/SimpleHTTPTests/Request/RequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ class RequestTests: XCTestCase {
let request = try Request<Void>.post(.test, body: multipart)
.toURLRequest(encoder: JSONEncoder(), relativeTo: baseURL)

XCTAssertEqual(request.allHTTPHeaderFields?["Content-Type"], HTTPContentType.multipart(boundary: multipart.boundary).value)
XCTAssertEqual(
request.allHTTPHeaderFields?["Content-Type"],
HTTPContentType.multipart(boundary: multipart.boundary).value
)
}

}
Expand Down
10 changes: 4 additions & 6 deletions Tests/SimpleHTTPTests/URL+Fixture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import Foundation

extension URL {

enum Images {

static let swift = URL.fromBundle(fileName: "swift", withExtension: "png")!
static let swiftUI = URL.fromBundle(fileName: "swiftUI", withExtension: "png")!

}
enum Images {
static let swift = URL.fromBundle(fileName: "swift", withExtension: "png")!
static let swiftUI = URL.fromBundle(fileName: "swiftUI", withExtension: "png")!
}

}

0 comments on commit 20b9a74

Please sign in to comment.