Skip to content

Commit

Permalink
Merge pull request #393 from edwinveger/feature/response-body-json-di…
Browse files Browse the repository at this point in the history
…ctionary-literals

Change HttpResponseBody.json to take Any
  • Loading branch information
Vkt0r authored Jun 9, 2019
2 parents e531e9d + 9f43162 commit 38470f0
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ All notable changes to this project will be documented in this file. Changes not
- Refactor: Use Foundation API for Base64 encoding. ([#403](https://github.com/httpswift/swifter/pull/403)) by [@mazyod](https://github.com/mazyod)
- Refactor: Use `URLComponents` for `HttpRequest` path and query parameters parsing [#404](https://github.com/httpswift/swifter/pull/404)) by [@mazyod](https://github.com/mazyod)
- `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)
- 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)
- `HttpResponse`: `html` requires now a complete html-string, not only the body-part.
- Include the `CHANGELOG.md` and `README.md` in the Xcode-Project for easy access / changes.

Expand Down
25 changes: 9 additions & 16 deletions XCode/Sources/HttpResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public protocol HttpResponseBodyWriter {
}

public enum HttpResponseBody {

case json(AnyObject)
case json(Any)
case html(String)
case htmlBody(String)
case text(String)
Expand All @@ -33,20 +33,13 @@ public enum HttpResponseBody {
do {
switch self {
case .json(let object):
#if os(Linux)
let data = [UInt8]("Not ready for Linux.".utf8)
return (data.count, {
try $0.write(data)
})
#else
guard JSONSerialization.isValidJSONObject(object) else {
throw SerializationError.invalidObject
}
let data = try JSONSerialization.data(withJSONObject: object)
return (data.count, {
try $0.write(data)
})
#endif
guard JSONSerialization.isValidJSONObject(object) else {
throw SerializationError.invalidObject
}
let data = try JSONSerialization.data(withJSONObject: object)
return (data.count, {
try $0.write(data)
})
case .text(let body):
let data = [UInt8](body.utf8)
return (data.count, {
Expand Down
8 changes: 8 additions & 0 deletions XCode/Swifter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
043660EB21FED52000497989 /* MimeTypesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4511204E9988000A0726 /* MimeTypesTests.swift */; };
047F1F02226AB9AD00909B95 /* XCTestManifests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B912F49220507D00062C360 /* XCTestManifests.swift */; };
0858E7F81D68BC2600491CD1 /* PingServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0858E7F61D68BC2600491CD1 /* PingServer.swift */; };
0C1F3CAD2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C1F3CAC2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift */; };
0C1F3CAE2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C1F3CAC2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift */; };
0C1F3CAF2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C1F3CAC2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift */; };
2659FC1A1DADC077003F3930 /* String+File.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C377E161D964B6A009C6148 /* String+File.swift */; };
269B47881D3AAAE20042D137 /* HttpResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C76B6EF1D2C44F30030FC98 /* HttpResponse.swift */; };
269B47891D3AAAE20042D137 /* Scopes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C76B6F41D2C44F30030FC98 /* Scopes.swift */; };
Expand Down Expand Up @@ -167,6 +170,7 @@
043660DE21FED3A300497989 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
0858E7F31D68BB2600491CD1 /* IOSafetyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IOSafetyTests.swift; sourceTree = "<group>"; };
0858E7F61D68BC2600491CD1 /* PingServer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PingServer.swift; sourceTree = "<group>"; };
0C1F3CAC2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = SwifterTestsHttpResponseBody.swift; sourceTree = "<group>"; tabWidth = 4; };
269B47A11D3AAAE20042D137 /* Swifter.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Swifter.framework; sourceTree = BUILT_PRODUCTS_DIR; };
269B47A41D3AAC4F0042D137 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
269B47A51D3AAC4F0042D137 /* SwiftertvOS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SwiftertvOS.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -424,6 +428,7 @@
7C4785E81C71D15600A9FE73 /* SwifterTestsWebSocketSession.swift */,
0858E7F31D68BB2600491CD1 /* IOSafetyTests.swift */,
6A0D4511204E9988000A0726 /* MimeTypesTests.swift */,
0C1F3CAC2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift */,
7B55EC94226E0E4F00042D23 /* ServerThreadingTests.swift */,
);
path = Tests;
Expand Down Expand Up @@ -777,6 +782,7 @@
047F1F02226AB9AD00909B95 /* XCTestManifests.swift in Sources */,
043660CE21FED35500497989 /* SwifterTestsHttpParser.swift in Sources */,
043660D521FED36C00497989 /* MimeTypesTests.swift in Sources */,
0C1F3CAE2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift in Sources */,
7B55EC96226E0E4F00042D23 /* ServerThreadingTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -793,6 +799,7 @@
7B55EC97226E0E4F00042D23 /* ServerThreadingTests.swift in Sources */,
043660E621FED51400497989 /* SwifterTestsHttpParser.swift in Sources */,
043660EB21FED52000497989 /* MimeTypesTests.swift in Sources */,
0C1F3CAF2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -906,6 +913,7 @@
043660D421FED36900497989 /* IOSafetyTests.swift in Sources */,
7CCD87721C660B250068099B /* SwifterTestsStringExtensions.swift in Sources */,
7B11AD4B21C9A8A6002F8820 /* SwifterTestsHttpRouter.swift in Sources */,
0C1F3CAD2265FC470076B6F5 /* SwifterTestsHttpResponseBody.swift in Sources */,
7B55EC95226E0E4F00042D23 /* ServerThreadingTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
58 changes: 58 additions & 0 deletions XCode/Tests/SwifterTestsHttpResponseBody.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// SwifterTestsHttpResponseBody.swift
// Swifter
//

import XCTest
@testable import Swifter

class SwifterTestsHttpResponseBody: XCTestCase {

func testDictionaryAsJSONPayload() {
verify(input: ["key": "value"], output: "{\"key\":\"value\"}")
verify(input: ["key": ["value1", "value2", "value3"]], output: "{\"key\":[\"value1\",\"value2\",\"value3\"]}")
}

func testArrayAsJSONPayload() {
verify(input: ["key", "value"], output: "[\"key\",\"value\"]")
verify(input: ["value1", "value2", "value3"], output: "[\"value1\",\"value2\",\"value3\"]")
}

func testNSDictionaryAsJSONPayload() {
verify(input: ["key": "value"] as NSDictionary, output: "{\"key\":\"value\"}")
verify(input: ["key": ["value1", "value2", "value3"]] as NSDictionary, output: "{\"key\":[\"value1\",\"value2\",\"value3\"]}")
}

func testNSArrayAsJSONPayload() {
verify(input: ["key", "value"] as NSArray, output: "[\"key\",\"value\"]")
verify(input: ["value1", "value2", "value3"] as NSArray, output: "[\"value1\",\"value2\",\"value3\"]")
}

private func verify(input: Any, output expectedOutput: String, line: UInt = #line) {
let response: HttpResponseBody = .json(input)

guard let writer = response.content().1 else {
XCTFail(line: line)
return
}

do {
let mockWriter = MockWriter()
try writer(mockWriter)
let output = String(decoding: mockWriter.data, as: UTF8.self)
XCTAssertEqual(output, expectedOutput, line: line)
} catch {
XCTFail(line: line)
}
}
}

private class MockWriter: HttpResponseBodyWriter {
var data = Data()

func write(_ file: String.File) throws { }
func write(_ data: [UInt8]) throws { }
func write(_ data: ArraySlice<UInt8>) throws { }
func write(_ data: NSData) throws { }
func write(_ data: Data) throws { self.data = data }
}
13 changes: 13 additions & 0 deletions XCode/Tests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ extension SwifterTestsHttpParser {
]
}

extension SwifterTestsHttpResponseBody {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__SwifterTestsHttpResponseBody = [
("testArrayAsJSONPayload", testArrayAsJSONPayload),
("testDictionaryAsJSONPayload", testDictionaryAsJSONPayload),
("testNSArrayAsJSONPayload", testNSArrayAsJSONPayload),
("testNSDictionaryAsJSONPayload", testNSDictionaryAsJSONPayload),
]
}

extension SwifterTestsHttpRouter {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
Expand Down Expand Up @@ -88,6 +100,7 @@ public func __allTests() -> [XCTestCaseEntry] {
testCase(MimeTypeTests.__allTests__MimeTypeTests),
testCase(ServerThreadingTests.__allTests__ServerThreadingTests),
testCase(SwifterTestsHttpParser.__allTests__SwifterTestsHttpParser),
testCase(SwifterTestsHttpResponseBody.__allTests__SwifterTestsHttpResponseBody),
testCase(SwifterTestsHttpRouter.__allTests__SwifterTestsHttpRouter),
testCase(SwifterTestsStringExtensions.__allTests__SwifterTestsStringExtensions),
testCase(SwifterTestsWebSocketSession.__allTests__SwifterTestsWebSocketSession),
Expand Down

0 comments on commit 38470f0

Please sign in to comment.