-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: Add/Retrieve Codable objects in a session #54
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
01d3cb5
Feat: Add codable add/retrieve from session
Andrew-Lees11 a280bc7
Added Jazzy docs
Andrew-Lees11 f02f80e
added Jazzy parameters
Andrew-Lees11 a3315ab
minor Jazzy fixes
Andrew-Lees11 3832a3d
Convert session error to struct
Andrew-Lees11 4cf59db
Add equatable implementation for SessionCodingError
Andrew-Lees11 68e4a0b
added Codable append functions
Andrew-Lees11 6ff9bf2
Use subscript for add/retrieve
Andrew-Lees11 0886acc
correct spelling of primitive
Andrew-Lees11 0b11b7f
Merge branch 'master' into saveCodable
Andrew-Lees11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
/** | ||
* Copyright IBM Corporation 2018 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
**/ | ||
|
||
import Kitura | ||
import KituraNet | ||
|
||
import Foundation | ||
import XCTest | ||
|
||
@testable import KituraSession | ||
|
||
struct CodableSessionTest: Codable, Equatable { | ||
let sessionKey: String | ||
public static func == (lhs: CodableSessionTest, rhs: CodableSessionTest) -> Bool { | ||
return lhs.sessionKey == rhs.sessionKey | ||
} | ||
} | ||
enum SessionEnum: String, Codable { | ||
case one, two | ||
} | ||
let CodableSessionTestArray = ["sessionValue1", "sessionValue2", "sessionValue3"] | ||
let CodableSessionTestDict = ["sessionKey1": "sessionValue1", "sessionKey2": "sessionValue2", "sessionKey3": "sessionValue3"] | ||
let CodableSessionTestCodableArray = [CodableSessionTest(sessionKey: "sessionValue1"), CodableSessionTest(sessionKey: "sessionValue2"), CodableSessionTest(sessionKey: "sessionValue3")] | ||
let CodableSessionTestCodableDict = ["sessionKey1": CodableSessionTest(sessionKey: "sessionValue1"), "sessionKey2": CodableSessionTest(sessionKey: "sessionValue2"), "sessionKey3": CodableSessionTest(sessionKey: "sessionValue3")] | ||
|
||
class TestCodableSession: XCTestCase, KituraTest { | ||
|
||
static var allTests: [(String, (TestCodableSession) -> () throws -> Void)] { | ||
return [ | ||
("testCodableSessionAddReadArray", testCodableSessionAddReadArray), | ||
("testCodableSessionAddReadCodable", testCodableSessionAddReadCodable), | ||
("testCodableSessionAddReadDict", testCodableSessionAddReadDict), | ||
("testCodableSessionAddReadCodableArray", testCodableSessionAddReadCodableArray), | ||
] | ||
} | ||
|
||
func testCodableSessionAddReadArray() { | ||
let router = Router() | ||
router.all(middleware: Session(secret: "secret")) | ||
|
||
router.post("/codable") { request, response, next in | ||
request.session?[sessionTestKey] = CodableSessionTestArray | ||
response.status(.created) | ||
next() | ||
} | ||
|
||
router.get("/codable") { request, response, next in | ||
guard let codable: [String] = request.session?[sessionTestKey] else { | ||
return try response.send(status: .notFound).end() | ||
} | ||
response.status(.OK) | ||
response.send(codable) | ||
next() | ||
} | ||
performServerTest(router: router, asyncTasks: { | ||
self.performRequest(method: "post", path: "/codable", callback: { response in | ||
XCTAssertNotNil(response, "ERROR!!! ClientRequest response object was nil") | ||
guard let response = response else { | ||
return XCTFail() | ||
} | ||
let (cookie, _) = CookieUtils.cookieFrom(response: response, named: cookieDefaultName) | ||
XCTAssertNotNil(cookie, "Cookie \(cookieDefaultName) wasn't found in the response.") | ||
guard let cookieValue = cookie?.value else { | ||
return XCTFail() | ||
} | ||
self.performRequest(method: "get", path: "/codable", callback: { response in | ||
XCTAssertNotNil(response, "ERROR!!! ClientRequest response object was nil") | ||
guard let response = response else { | ||
return XCTFail() | ||
} | ||
XCTAssertEqual(response.statusCode, HTTPStatusCode.OK, "HTTP Status code was \(response.statusCode)") | ||
do { | ||
guard let body = try response.readString(), let sessionData = body.data(using: .utf8) else { | ||
XCTFail("No response body") | ||
return | ||
} | ||
let decoder = JSONDecoder() | ||
let returnedSession = try decoder.decode([String].self, from: sessionData) | ||
XCTAssertEqual(returnedSession, CodableSessionTestArray) | ||
} catch { | ||
XCTFail("No response body") | ||
} | ||
}, headers: ["Cookie": "\(cookieDefaultName)=\(cookieValue)"]) | ||
}) | ||
}) | ||
} | ||
|
||
func testCodableSessionAddReadCodable() { | ||
let router = Router() | ||
router.all(middleware: Session(secret: "secret")) | ||
|
||
router.post("/codable") { request, response, next in | ||
let codableSession = CodableSessionTest(sessionKey: sessionTestValue) | ||
request.session?[sessionTestKey] = codableSession | ||
response.status(.created) | ||
next() | ||
} | ||
|
||
router.get("/codable") { request, response, next in | ||
let codable: CodableSessionTest? = request.session?[sessionTestKey] | ||
response.status(.OK) | ||
response.send(codable) | ||
next() | ||
} | ||
performServerTest(router: router, asyncTasks: { | ||
self.performRequest(method: "post", path: "/codable", callback: { response in | ||
XCTAssertNotNil(response, "ERROR!!! ClientRequest response object was nil") | ||
guard let response = response else { | ||
return XCTFail() | ||
} | ||
let (cookie, _) = CookieUtils.cookieFrom(response: response, named: cookieDefaultName) | ||
XCTAssertNotNil(cookie, "Cookie \(cookieDefaultName) wasn't found in the response.") | ||
guard let cookieValue = cookie?.value else { | ||
return XCTFail() | ||
} | ||
self.performRequest(method: "get", path: "/codable", callback: { response in | ||
XCTAssertNotNil(response, "ERROR!!! ClientRequest response object was nil") | ||
guard let response = response else { | ||
return XCTFail() | ||
} | ||
XCTAssertEqual(response.statusCode, HTTPStatusCode.OK, "HTTP Status code was \(response.statusCode)") | ||
do { | ||
guard let body = try response.readString(), let sessionData = body.data(using: .utf8) else { | ||
XCTFail("No response body") | ||
return | ||
} | ||
let decoder = JSONDecoder() | ||
let returnedSession = try decoder.decode(CodableSessionTest.self, from: sessionData) | ||
XCTAssertEqual(returnedSession.sessionKey, sessionTestValue) | ||
} catch { | ||
XCTFail("No response body") | ||
} | ||
}, headers: ["Cookie": "\(cookieDefaultName)=\(cookieValue)"]) | ||
}) | ||
}) | ||
} | ||
|
||
func testCodableSessionAddReadDict() { | ||
let router = Router() | ||
router.all(middleware: Session(secret: "secret")) | ||
|
||
router.post("/codable") { request, response, next in | ||
request.session?[sessionTestKey] = CodableSessionTestDict | ||
response.status(.created) | ||
next() | ||
} | ||
|
||
router.get("/codable") { request, response, next in | ||
guard let codable: [String: String] = request.session?[sessionTestKey] else { | ||
return try response.send(status: .notFound).end() | ||
} | ||
response.status(.OK) | ||
response.send(codable) | ||
next() | ||
} | ||
performServerTest(router: router, asyncTasks: { | ||
self.performRequest(method: "post", path: "/codable", callback: { response in | ||
XCTAssertNotNil(response, "ERROR!!! ClientRequest response object was nil") | ||
guard let response = response else { | ||
return XCTFail() | ||
} | ||
let (cookie, _) = CookieUtils.cookieFrom(response: response, named: cookieDefaultName) | ||
XCTAssertNotNil(cookie, "Cookie \(cookieDefaultName) wasn't found in the response.") | ||
guard let cookieValue = cookie?.value else { | ||
return XCTFail() | ||
} | ||
self.performRequest(method: "get", path: "/codable", callback: { response in | ||
XCTAssertNotNil(response, "ERROR!!! ClientRequest response object was nil") | ||
guard let response = response else { | ||
return XCTFail() | ||
} | ||
XCTAssertEqual(response.statusCode, HTTPStatusCode.OK, "HTTP Status code was \(response.statusCode)") | ||
do { | ||
guard let body = try response.readString(), let sessionData = body.data(using: .utf8) else { | ||
XCTFail("No response body") | ||
return | ||
} | ||
let decoder = JSONDecoder() | ||
let returnedSession = try decoder.decode([String: String].self, from: sessionData) | ||
XCTAssertEqual(returnedSession, CodableSessionTestDict) | ||
} catch { | ||
XCTFail("No response body") | ||
} | ||
}, headers: ["Cookie": "\(cookieDefaultName)=\(cookieValue)"]) | ||
}) | ||
}) | ||
} | ||
|
||
func testCodableSessionAddReadCodableArray() { | ||
let router = Router() | ||
router.all(middleware: Session(secret: "secret")) | ||
|
||
router.post("/codable") { request, response, next in | ||
request.session?[sessionTestKey] = CodableSessionTestCodableArray | ||
response.status(.created) | ||
next() | ||
} | ||
|
||
router.get("/codable") { request, response, next in | ||
guard let codable: [CodableSessionTest] = request.session?[sessionTestKey] else { | ||
return try response.send(status: .notFound).end() | ||
} | ||
response.status(.OK) | ||
response.send(codable) | ||
next() | ||
} | ||
performServerTest(router: router, asyncTasks: { | ||
self.performRequest(method: "post", path: "/codable", callback: { response in | ||
XCTAssertNotNil(response, "ERROR!!! ClientRequest response object was nil") | ||
guard let response = response else { | ||
return XCTFail() | ||
} | ||
let (cookie, _) = CookieUtils.cookieFrom(response: response, named: cookieDefaultName) | ||
XCTAssertNotNil(cookie, "Cookie \(cookieDefaultName) wasn't found in the response.") | ||
guard let cookieValue = cookie?.value else { | ||
return XCTFail() | ||
} | ||
self.performRequest(method: "get", path: "/codable", callback: { response in | ||
XCTAssertNotNil(response, "ERROR!!! ClientRequest response object was nil") | ||
guard let response = response else { | ||
return XCTFail() | ||
} | ||
XCTAssertEqual(response.statusCode, HTTPStatusCode.OK, "HTTP Status code was \(response.statusCode)") | ||
do { | ||
guard let body = try response.readString(), let sessionData = body.data(using: .utf8) else { | ||
XCTFail("No response body") | ||
return | ||
} | ||
let decoder = JSONDecoder() | ||
let returnedSession = try decoder.decode([CodableSessionTest].self, from: sessionData) | ||
XCTAssertEqual(returnedSession.map({$0.sessionKey}), CodableSessionTestCodableArray.map({$0.sessionKey})) | ||
} catch { | ||
XCTFail("No response body") | ||
} | ||
}, headers: ["Cookie": "\(cookieDefaultName)=\(cookieValue)"]) | ||
}) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does response.send with an optional Codable do what you expect?