Skip to content

Commit 83a97f1

Browse files
EdwinEdwin
authored andcommitted
Change HttpResponseBody.json to take Any
Update changelog Include the new tests added to the `XCTManifests.swift`
1 parent 0152210 commit 83a97f1

File tree

5 files changed

+89
-16
lines changed

5 files changed

+89
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ All notable changes to this project will be documented in this file. Changes not
3737
- Refactor: Use Foundation API for Base64 encoding. ([#403](https://github.com/httpswift/swifter/pull/403)) by [@mazyod](https://github.com/mazyod)
3838
- Refactor: Use `URLComponents` for `HttpRequest` path and query parameters parsing [#404](https://github.com/httpswift/swifter/pull/404)) by [@mazyod](https://github.com/mazyod)
3939
- `HttpResponse` functions `statusCode()` and `reasonPhrase` changed to computed variables instead of functions, and made public (No impact on existing usage as it was previously internal). ([#410](https://github.com/httpswift/swifter/pull/410)) by [@apocolipse](https://github.com/apocolipse)
40+
- Adjusted the associated type of enum case `HttpResponseBody.json` from `AnyObject` to `Any` to allow Swift dictionaries/arrays without converting to their Objective-C counterparts. ([#393](https://github.com/httpswift/swifter/pull/393)) by [@edwinveger](https://github.com/edwinveger)
4041

4142

4243
## Removed

XCode/Sources/HttpResponse.swift

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public protocol HttpResponseBodyWriter {
2121
}
2222

2323
public enum HttpResponseBody {
24-
25-
case json(AnyObject)
24+
25+
case json(Any)
2626
case html(String)
2727
case text(String)
2828
case data(Data)
@@ -32,20 +32,13 @@ public enum HttpResponseBody {
3232
do {
3333
switch self {
3434
case .json(let object):
35-
#if os(Linux)
36-
let data = [UInt8]("Not ready for Linux.".utf8)
37-
return (data.count, {
38-
try $0.write(data)
39-
})
40-
#else
41-
guard JSONSerialization.isValidJSONObject(object) else {
42-
throw SerializationError.invalidObject
43-
}
44-
let data = try JSONSerialization.data(withJSONObject: object)
45-
return (data.count, {
46-
try $0.write(data)
47-
})
48-
#endif
35+
guard JSONSerialization.isValidJSONObject(object) else {
36+
throw SerializationError.invalidObject
37+
}
38+
let data = try JSONSerialization.data(withJSONObject: object)
39+
return (data.count, {
40+
try $0.write(data)
41+
})
4942
case .text(let body):
5043
let data = [UInt8](body.utf8)
5144
return (data.count, {

XCode/Swifter.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
043660EB21FED52000497989 /* MimeTypesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4511204E9988000A0726 /* MimeTypesTests.swift */; };
2727
047F1F02226AB9AD00909B95 /* XCTestManifests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B912F49220507D00062C360 /* XCTestManifests.swift */; };
2828
0858E7F81D68BC2600491CD1 /* PingServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0858E7F61D68BC2600491CD1 /* PingServer.swift */; };
29+
0C1F3CAD2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C1F3CAC2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift */; };
30+
0C1F3CAE2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C1F3CAC2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift */; };
31+
0C1F3CAF2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C1F3CAC2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift */; };
2932
2659FC1A1DADC077003F3930 /* String+File.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C377E161D964B6A009C6148 /* String+File.swift */; };
3033
269B47881D3AAAE20042D137 /* HttpResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C76B6EF1D2C44F30030FC98 /* HttpResponse.swift */; };
3134
269B47891D3AAAE20042D137 /* Scopes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C76B6F41D2C44F30030FC98 /* Scopes.swift */; };
@@ -167,6 +170,7 @@
167170
043660DE21FED3A300497989 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
168171
0858E7F31D68BB2600491CD1 /* IOSafetyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IOSafetyTests.swift; sourceTree = "<group>"; };
169172
0858E7F61D68BC2600491CD1 /* PingServer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PingServer.swift; sourceTree = "<group>"; };
173+
0C1F3CAC2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = SwifterTestsHttpResponseBody.swift; sourceTree = "<group>"; tabWidth = 4; };
170174
269B47A11D3AAAE20042D137 /* Swifter.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Swifter.framework; sourceTree = BUILT_PRODUCTS_DIR; };
171175
269B47A41D3AAC4F0042D137 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
172176
269B47A51D3AAC4F0042D137 /* SwiftertvOS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SwiftertvOS.h; sourceTree = "<group>"; };
@@ -420,6 +424,7 @@
420424
7C4785E81C71D15600A9FE73 /* SwifterTestsWebSocketSession.swift */,
421425
0858E7F31D68BB2600491CD1 /* IOSafetyTests.swift */,
422426
6A0D4511204E9988000A0726 /* MimeTypesTests.swift */,
427+
0C1F3CAC2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift */,
423428
7B55EC94226E0E4F00042D23 /* ServerThreadingTests.swift */,
424429
);
425430
path = Tests;
@@ -773,6 +778,7 @@
773778
047F1F02226AB9AD00909B95 /* XCTestManifests.swift in Sources */,
774779
043660CE21FED35500497989 /* SwifterTestsHttpParser.swift in Sources */,
775780
043660D521FED36C00497989 /* MimeTypesTests.swift in Sources */,
781+
0C1F3CAE2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift in Sources */,
776782
7B55EC96226E0E4F00042D23 /* ServerThreadingTests.swift in Sources */,
777783
);
778784
runOnlyForDeploymentPostprocessing = 0;
@@ -789,6 +795,7 @@
789795
7B55EC97226E0E4F00042D23 /* ServerThreadingTests.swift in Sources */,
790796
043660E621FED51400497989 /* SwifterTestsHttpParser.swift in Sources */,
791797
043660EB21FED52000497989 /* MimeTypesTests.swift in Sources */,
798+
0C1F3CAF2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift in Sources */,
792799
);
793800
runOnlyForDeploymentPostprocessing = 0;
794801
};
@@ -902,6 +909,7 @@
902909
043660D421FED36900497989 /* IOSafetyTests.swift in Sources */,
903910
7CCD87721C660B250068099B /* SwifterTestsStringExtensions.swift in Sources */,
904911
7B11AD4B21C9A8A6002F8820 /* SwifterTestsHttpRouter.swift in Sources */,
912+
0C1F3CAD2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift in Sources */,
905913
7B55EC95226E0E4F00042D23 /* ServerThreadingTests.swift in Sources */,
906914
);
907915
runOnlyForDeploymentPostprocessing = 0;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// SwifterTestsHttpResponseBody.swift
3+
// Swifter
4+
//
5+
6+
import XCTest
7+
@testable import Swifter
8+
9+
class SwifterTestsHttpResponseBody: XCTestCase {
10+
11+
func testDictionaryAsJSONPayload() {
12+
verify(input: ["key": "value"], output: "{\"key\":\"value\"}")
13+
verify(input: ["key": ["value1", "value2", "value3"]], output: "{\"key\":[\"value1\",\"value2\",\"value3\"]}")
14+
}
15+
16+
func testArrayAsJSONPayload() {
17+
verify(input: ["key", "value"], output: "[\"key\",\"value\"]")
18+
verify(input: ["value1", "value2", "value3"], output: "[\"value1\",\"value2\",\"value3\"]")
19+
}
20+
21+
func testNSDictionaryAsJSONPayload() {
22+
verify(input: ["key": "value"] as NSDictionary, output: "{\"key\":\"value\"}")
23+
verify(input: ["key": ["value1", "value2", "value3"]] as NSDictionary, output: "{\"key\":[\"value1\",\"value2\",\"value3\"]}")
24+
}
25+
26+
func testNSArrayAsJSONPayload() {
27+
verify(input: ["key", "value"] as NSArray, output: "[\"key\",\"value\"]")
28+
verify(input: ["value1", "value2", "value3"] as NSArray, output: "[\"value1\",\"value2\",\"value3\"]")
29+
}
30+
31+
private func verify(input: Any, output expectedOutput: String, line: UInt = #line) {
32+
let response: HttpResponseBody = .json(input)
33+
34+
guard let writer = response.content().1 else {
35+
XCTFail(line: line)
36+
return
37+
}
38+
39+
do {
40+
let mockWriter = MockWriter()
41+
try writer(mockWriter)
42+
let output = String(decoding: mockWriter.data, as: UTF8.self)
43+
XCTAssertEqual(output, expectedOutput, line: line)
44+
} catch {
45+
XCTFail(line: line)
46+
}
47+
}
48+
}
49+
50+
private class MockWriter: HttpResponseBodyWriter {
51+
var data = Data()
52+
53+
func write(_ file: String.File) throws { }
54+
func write(_ data: [UInt8]) throws { }
55+
func write(_ data: ArraySlice<UInt8>) throws { }
56+
func write(_ data: NSData) throws { }
57+
func write(_ data: Data) throws { self.data = data }
58+
}

XCode/Tests/XCTestManifests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ extension SwifterTestsHttpParser {
4141
]
4242
}
4343

44+
extension SwifterTestsHttpResponseBody {
45+
// DO NOT MODIFY: This is autogenerated, use:
46+
// `swift test --generate-linuxmain`
47+
// to regenerate.
48+
static let __allTests__SwifterTestsHttpResponseBody = [
49+
("testArrayAsJSONPayload", testArrayAsJSONPayload),
50+
("testDictionaryAsJSONPayload", testDictionaryAsJSONPayload),
51+
("testNSArrayAsJSONPayload", testNSArrayAsJSONPayload),
52+
("testNSDictionaryAsJSONPayload", testNSDictionaryAsJSONPayload),
53+
]
54+
}
55+
4456
extension SwifterTestsHttpRouter {
4557
// DO NOT MODIFY: This is autogenerated, use:
4658
// `swift test --generate-linuxmain`
@@ -88,6 +100,7 @@ public func __allTests() -> [XCTestCaseEntry] {
88100
testCase(MimeTypeTests.__allTests__MimeTypeTests),
89101
testCase(ServerThreadingTests.__allTests__ServerThreadingTests),
90102
testCase(SwifterTestsHttpParser.__allTests__SwifterTestsHttpParser),
103+
testCase(SwifterTestsHttpResponseBody.__allTests__SwifterTestsHttpResponseBody),
91104
testCase(SwifterTestsHttpRouter.__allTests__SwifterTestsHttpRouter),
92105
testCase(SwifterTestsStringExtensions.__allTests__SwifterTestsStringExtensions),
93106
testCase(SwifterTestsWebSocketSession.__allTests__SwifterTestsWebSocketSession),

0 commit comments

Comments
 (0)