Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds automatic calls to beforeEncode() and afterDecode() on Content
- Loading branch information
Showing
9 changed files
with
161 additions
and
1 deletion.
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
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
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,48 @@ | ||
import Vapor | ||
import XCTVapor | ||
import COperatingSystem | ||
import AsyncHTTPClient | ||
|
||
class ContentTests: XCTestCase { | ||
func testBeforeEncodeContent() throws { | ||
let content = SampleContent() | ||
XCTAssertEqual(content.name, "old name") | ||
|
||
let response = Response(status: .ok) | ||
try response.content.encode(content) | ||
|
||
let body = try XCTUnwrap(response.body.string) | ||
XCTAssertEqual(body, #"{"name":"new name"}"#) | ||
} | ||
|
||
func testAfterContentEncode() throws { | ||
let app = Application() | ||
defer { app.shutdown() } | ||
|
||
var body = ByteBufferAllocator().buffer(capacity: 0) | ||
body.writeString(#"{"name": "before decode"}"#) | ||
|
||
let request = Request( | ||
application: app, | ||
collectedBody: body, | ||
on: EmbeddedEventLoop() | ||
) | ||
|
||
request.headers.contentType = .json | ||
|
||
let content = try request.content.decode(SampleContent.self) | ||
XCTAssertEqual(content.name, "new name after decode") | ||
} | ||
} | ||
|
||
private struct SampleContent: Content { | ||
var name = "old name" | ||
|
||
mutating func beforeEncode() throws { | ||
name = "new name" | ||
} | ||
|
||
mutating func afterDecode() throws { | ||
name = "new name after decode" | ||
} | ||
} |