Skip to content
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

Remove use of HTTPBin #3017

Merged
merged 4 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
95 changes: 84 additions & 11 deletions Tests/AsyncTests/AsyncClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,57 @@ import Logging
import NIOEmbedded

final class AsyncClientTests: XCTestCase {

var remoteAppPort: Int!
var remoteApp: Application!

override func setUp() async throws {
remoteApp = Application(.testing)
remoteApp.http.server.configuration.port = 0

remoteApp.get("json") { _ in
SomeJSON()
}

remoteApp.get("status", ":status") { req -> HTTPStatus in
let status = try req.parameters.require("status", as: Int.self)
return HTTPStatus(statusCode: status)
}

remoteApp.post("anything") { req -> AnythingResponse in
let headers = req.headers.reduce(into: [String: String]()) {
$0[$1.0] = $1.1
}

guard let json:[String:Any] = try JSONSerialization.jsonObject(with: req.body.data!) as? [String:Any] else {
throw Abort(.badRequest)
}

let jsonResponse = json.mapValues {
return "\($0)"
}

return AnythingResponse(headers: headers, json: jsonResponse)
}

remoteApp.environment.arguments = ["serve"]
try remoteApp.boot()
try remoteApp.start()

XCTAssertNotNil(remoteApp.http.server.shared.localAddress)
guard let localAddress = remoteApp.http.server.shared.localAddress,
let port = localAddress.port else {
XCTFail("couldn't get ip/port from \(remoteApp.http.server.shared.localAddress.debugDescription)")
return
}

self.remoteAppPort = port
}

override func tearDown() async throws {
remoteApp.shutdown()
}

func testClientConfigurationChange() async throws {
let app = Application(.testing)
defer { app.shutdown() }
Expand Down Expand Up @@ -49,7 +100,7 @@ final class AsyncClientTests: XCTestCase {
let app = Application(.testing)
defer { app.shutdown() }

let res = try await app.client.get("https://httpbin.org/json")
let res = try await app.client.get("http://localhost:\(remoteAppPort!)/json")

let encoded = try JSONEncoder().encode(res)
let decoded = try JSONDecoder().decode(ClientResponse.self, from: encoded)
Expand All @@ -62,17 +113,13 @@ final class AsyncClientTests: XCTestCase {
defer { app.shutdown() }
try app.boot()

let res = try await app.client.post("http://httpbin.org/anything") { req in
let res = try await app.client.post("http://localhost:\(remoteAppPort!)/anything") { req in
try req.content.encode(["hello": "world"])
}

struct HTTPBinAnything: Codable {
var headers: [String: String]
var json: [String: String]
}
let data = try res.content.decode(HTTPBinAnything.self)
let data = try res.content.decode(AnythingResponse.self)
XCTAssertEqual(data.json, ["hello": "world"])
XCTAssertEqual(data.headers["Content-Type"], "application/json; charset=utf-8")
XCTAssertEqual(data.headers["content-type"], "application/json; charset=utf-8")
}

func testBoilerplateClient() async throws {
Expand All @@ -82,7 +129,7 @@ final class AsyncClientTests: XCTestCase {

app.get("foo") { req async throws -> String in
do {
let response = try await req.client.get("https://httpbin.org/status/201")
let response = try await req.client.get("http://localhost:\(self.remoteAppPort!)/status/201")
XCTAssertEqual(response.status.code, 201)
req.application.running?.stop()
return "bar"
Expand Down Expand Up @@ -121,13 +168,12 @@ final class AsyncClientTests: XCTestCase {
}

func testClientLogging() async throws {
print("We are testing client logging")
let app = Application(.testing)
defer { app.shutdown() }
let logs = TestLogHandler()
app.logger = logs.logger

_ = try await app.client.get("https://httpbin.org/json")
_ = try await app.client.get("http://localhost:\(remoteAppPort!)/status/201")

let metadata = logs.getMetadata()
XCTAssertNotNil(metadata["ahc-request-id"])
Expand Down Expand Up @@ -233,3 +279,30 @@ final class TestLogHandler: LogHandler {
}
}
}

struct SomeJSON: Content {
let vapor: SomeNestedJSON

init() {
vapor = SomeNestedJSON(name: "The Vapor Project", age: 7, repos: [
VaporRepoJSON(name: "WebsocketKit", url: "https://github.com/vapor/websocket-kit"),
VaporRepoJSON(name: "PostgresNIO", url: "https://github.com/vapor/postgres-nio")
])
}
}

struct SomeNestedJSON: Content {
let name: String
let age: Int
let repos: [VaporRepoJSON]
}

struct VaporRepoJSON: Content {
let name: String
let url: String
}

struct AnythingResponse: Content {
var headers: [String: String]
var json: [String: String]
}
105 changes: 88 additions & 17 deletions Tests/VaporTests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,57 @@ import AsyncHTTPClient
import NIOEmbedded

final class ClientTests: XCTestCase {

var remoteAppPort: Int!
var remoteApp: Application!

override func setUp() async throws {
remoteApp = Application(.testing)
remoteApp.http.server.configuration.port = 0

remoteApp.get("json") { _ in
SomeJSON()
}

remoteApp.get("status", ":status") { req -> HTTPStatus in
let status = try req.parameters.require("status", as: Int.self)
return HTTPStatus(statusCode: status)
}

remoteApp.post("anything") { req -> AnythingResponse in
let headers = req.headers.reduce(into: [String: String]()) {
$0[$1.0] = $1.1
}

guard let json:[String:Any] = try JSONSerialization.jsonObject(with: req.body.data!) as? [String:Any] else {
throw Abort(.badRequest)
}

let jsonResponse = json.mapValues {
return "\($0)"
}

return AnythingResponse(headers: headers, json: jsonResponse)
}

remoteApp.environment.arguments = ["serve"]
try remoteApp.boot()
try remoteApp.start()

XCTAssertNotNil(remoteApp.http.server.shared.localAddress)
guard let localAddress = remoteApp.http.server.shared.localAddress,
let port = localAddress.port else {
XCTFail("couldn't get ip/port from \(remoteApp.http.server.shared.localAddress.debugDescription)")
return
}

self.remoteAppPort = port
}

override func tearDown() async throws {
remoteApp.shutdown()
}

func testClientConfigurationChange() throws {
let app = Application(.testing)
defer { app.shutdown() }
Expand Down Expand Up @@ -48,7 +99,7 @@ final class ClientTests: XCTestCase {
let app = Application(.testing)
defer { app.shutdown() }

let res = try app.client.get("https://httpbin.org/json").wait()
let res = try app.client.get("http://localhost:\(remoteAppPort!)/json").wait()

let encoded = try JSONEncoder().encode(res)
let decoded = try JSONDecoder().decode(ClientResponse.self, from: encoded)
Expand All @@ -61,41 +112,33 @@ final class ClientTests: XCTestCase {
defer { app.shutdown() }
try app.boot()

let res = try app.client.post("http://httpbin.org/anything") { req in
let res = try app.client.post("http://localhost:\(remoteAppPort!)/anything") { req in
try req.content.encode(["hello": "world"])
}.wait()

struct HTTPBinAnything: Codable {
var headers: [String: String]
var json: [String: String]
}
let data = try res.content.decode(HTTPBinAnything.self)
let data = try res.content.decode(AnythingResponse.self)
XCTAssertEqual(data.json, ["hello": "world"])
XCTAssertEqual(data.headers["Content-Type"], "application/json; charset=utf-8")
XCTAssertEqual(data.headers["content-type"], "application/json; charset=utf-8")
}

func testClientContent() throws {
let app = Application()
defer { app.shutdown() }
try app.boot()

let res = try app.client.post("http://httpbin.org/anything", content: ["hello": "world"]).wait()
let res = try app.client.post("http://localhost:\(remoteAppPort!)/anything", content: ["hello": "world"]).wait()

struct HTTPBinAnything: Codable {
var headers: [String: String]
var json: [String: String]
}
let data = try res.content.decode(HTTPBinAnything.self)
let data = try res.content.decode(AnythingResponse.self)
XCTAssertEqual(data.json, ["hello": "world"])
XCTAssertEqual(data.headers["Content-Type"], "application/json; charset=utf-8")
XCTAssertEqual(data.headers["content-type"], "application/json; charset=utf-8")
}

func testBoilerplateClient() throws {
let app = Application(.testing)
defer { app.shutdown() }

app.get("foo") { req -> EventLoopFuture<String> in
return req.client.get("https://httpbin.org/status/201").map { res in
return req.client.get("http://localhost:\(self.remoteAppPort!)/status/201").map { res in
XCTAssertEqual(res.status.code, 201)
req.application.running?.stop()
return "bar"
Expand Down Expand Up @@ -169,7 +212,7 @@ final class ClientTests: XCTestCase {
let logs = TestLogHandler()
app.logger = logs.logger

_ = try app.client.get("https://httpbin.org/json").wait()
_ = try app.client.get("https://www.vapor.codes").wait()

let metadata = logs.getMetadata()
XCTAssertNotNil(metadata["ahc-request-id"])
Expand Down Expand Up @@ -267,3 +310,31 @@ final class TestLogHandler: LogHandler {
return self.metadata
}
}


struct SomeJSON: Content {
let vapor: SomeNestedJSON

init() {
vapor = SomeNestedJSON(name: "The Vapor Project", age: 7, repos: [
VaporRepoJSON(name: "WebsocketKit", url: "https://github.com/vapor/websocket-kit"),
VaporRepoJSON(name: "PostgresNIO", url: "https://github.com/vapor/postgres-nio")
])
}
}

struct SomeNestedJSON: Content {
let name: String
let age: Int
let repos: [VaporRepoJSON]
}

struct VaporRepoJSON: Content {
let name: String
let url: String
}

struct AnythingResponse: Content {
var headers: [String: String]
var json: [String: String]
}
2 changes: 1 addition & 1 deletion Tests/VaporTests/RouteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ final class RouteTests: XCTestCase {
defer { app.shutdown() }

app.get("client") { req in
return req.client.get("http://httpbin.org/status/2 1").map { $0.description }
return req.client.get("http://localhost/status/2 1").map { $0.description }
}

try app.testable(method: .running).test(.GET, "/client") { res in
Expand Down