Skip to content

Commit

Permalink
Add HummingBirdJSON tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Jan 18, 2021
1 parent c94206c commit ccda3b9
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Package.swift
Expand Up @@ -7,6 +7,7 @@ let package = Package(
name: "hummingbird",
products: [
.library(name: "HummingBird", targets: ["HummingBird"]),
.library(name: "HummingBirdCompression", targets: ["HummingBirdCompression"]),
.library(name: "HummingBirdFiles", targets: ["HummingBirdFiles"]),
.library(name: "HummingBirdJSON", targets: ["HummingBirdJSON"]),
.library(name: "HummingBirdTLS", targets: ["HummingBirdTLS"]),
Expand Down Expand Up @@ -36,6 +37,10 @@ let package = Package(
.byName(name: "HummingBird"),
.product(name: "NIO", package: "swift-nio"),
]),
.target(name: "HummingBirdCompression", dependencies: [
.byName(name: "HummingBird"),
.product(name: "NIOHTTPCompression", package: "swift-nio-extras"),
]),
.target(name: "HummingBirdJSON", dependencies: [
.byName(name: "HummingBird"),
.product(name: "NIOFoundationCompat", package: "swift-nio"),
Expand All @@ -50,6 +55,14 @@ let package = Package(
.byName(name: "HummingBird"),
.product(name: "AsyncHTTPClient", package: "async-http-client"),
]),
.testTarget(name: "HummingBirdCompressionTests", dependencies: [
.byName(name: "HummingBirdCompression"),
.product(name: "AsyncHTTPClient", package: "async-http-client"),
]),
.testTarget(name: "HummingBirdJSONTests", dependencies: [
.byName(name: "HummingBirdJSON"),
.product(name: "AsyncHTTPClient", package: "async-http-client"),
]),
.testTarget(name: "HummingBirdTLSTests", dependencies: [
.byName(name: "HummingBirdTLS"),
.product(name: "AsyncHTTPClient", package: "async-http-client"),
Expand Down
3 changes: 2 additions & 1 deletion Sources/HummingBirdJSON/JSONCoding.swift
@@ -1,4 +1,5 @@
import Foundation
@_exported import class Foundation.JSONEncoder
@_exported import class Foundation.JSONDecoder
import HummingBird
import NIOFoundationCompat

Expand Down
@@ -0,0 +1,19 @@
import AsyncHTTPClient
import HummingBird
import HummingBirdCompression
import XCTest

class HummingBirdCompressionTests: XCTestCase {
func testDecode() {
let app = Application(.init(host: "localhost", port: 8000))
app.router.get("/hello") { request in
return "hello"
}
app.start()
defer { app.stop(); app.wait() }

let client = HTTPClient(eventLoopGroupProvider: .shared(app.eventLoopGroup))
defer { XCTAssertNoThrow(try client.syncShutdown()) }
}

}
57 changes: 57 additions & 0 deletions Tests/HummingBirdJSONTests/HummingBirdJSONTests.swift
@@ -0,0 +1,57 @@
import AsyncHTTPClient
import HummingBird
import HummingBirdJSON
import XCTest

class HummingBirdJSONTests: XCTestCase {
struct User: ResponseCodable {
let name: String
let email: String
let age: Int
}
struct Error: Swift.Error {}

func testDecode() {
let app = Application(.init(port: 8000))
app.decoder = JSONDecoder()
app.router.put("/user") { request -> HTTPResponseStatus in
guard let user = try? request.decode(as: User.self) else { throw HTTPError(.badRequest) }
XCTAssertEqual(user.name, "John Smith")
XCTAssertEqual(user.email, "john.smith@email.com")
XCTAssertEqual(user.age, 25)
return .ok
}
app.start()
defer { app.stop(); app.wait() }

let client = HTTPClient(eventLoopGroupProvider: .shared(app.eventLoopGroup))
defer { XCTAssertNoThrow(try client.syncShutdown()) }

let body = #"{"name": "John Smith", "email": "john.smith@email.com", "age": 25}"#
let response = client.put(url: "http://localhost:8000/user", body: .string(body), deadline: .now() + .seconds(10))
XCTAssertNoThrow(try response.wait())
}

func testEncode() {
let app = Application(.init(port: 8000))
app.encoder = JSONEncoder()
app.router.get("/user") { request -> User in
return User(name: "John Smith", email: "john.smith@email.com", age: 25)
}
app.start()
defer { app.stop(); app.wait() }

let client = HTTPClient(eventLoopGroupProvider: .shared(app.eventLoopGroup))
defer { XCTAssertNoThrow(try client.syncShutdown()) }

let response = client.get(url: "http://localhost:8000/user").flatMapThrowing { response in
guard let body = response.body else { throw HummingBirdJSONTests.Error() }
let user = try JSONDecoder().decode(User.self, from: body)
XCTAssertEqual(user.name, "John Smith")
XCTAssertEqual(user.email, "john.smith@email.com")
XCTAssertEqual(user.age, 25)
}
XCTAssertNoThrow(try response.wait())
}
}

0 comments on commit ccda3b9

Please sign in to comment.