Skip to content

Commit

Permalink
private HTTPClient
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Jun 8, 2024
1 parent 53ee852 commit 2e83805
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 18 deletions.
63 changes: 63 additions & 0 deletions FlyingFox/Sources/HTTPClient.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// HTTPClient.swift
// FlyingFox
//
// Created by Simon Whitty on 8/06/2024.
// Copyright © 2024 Simon Whitty. All rights reserved.
//
// Distributed under the permissive MIT license
// Get the latest version from here:
//
// https://github.com/swhitty/FlyingFox
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

import FlyingSocks

@_spi(Private)
public struct _HTTPClient {

public func sendHTTPRequest(_ request: HTTPRequest, to address: some SocketAddress) async throws -> HTTPResponse {
let socket = try await AsyncSocket.connected(to: address)
try await socket.writeRequest(request)
let response = try await socket.readResponse()
try? socket.close()
return response
}
}

@_spi(Private)
public extension AsyncSocket {
func writeRequest(_ request: HTTPRequest) async throws {
try await write(HTTPEncoder.encodeRequest(request))
}

func readResponse() async throws -> HTTPResponse {
try await HTTPDecoder.decodeResponse(from: bytes)
}

func writeFrame(_ frame: WSFrame) async throws {
try await write(WSFrameEncoder.encodeFrame(frame))
}

func readFrame() async throws -> WSFrame {
try await WSFrameEncoder.decodeFrame(from: bytes)
}
}
16 changes: 0 additions & 16 deletions FlyingFox/Tests/AsyncSocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,6 @@ extension AsyncSocket {
try await write(string.data(using: .utf8)!)
}

func writeRequest(_ request: HTTPRequest) async throws {
try await write(HTTPEncoder.encodeRequest(request))
}

func writeFrame(_ frame: WSFrame) async throws {
try await write(WSFrameEncoder.encodeFrame(frame))
}

func readResponse() async throws -> HTTPResponse {
try await HTTPDecoder.decodeResponse(from: bytes)
}

func readFrame() async throws -> WSFrame {
try await WSFrameEncoder.decodeFrame(from: bytes)
}

func readString(length: Int) async throws -> String {
let bytes = try await read(bytes: length)
guard let string = String(data: Data(bytes), encoding: .utf8) else {
Expand Down
55 changes: 55 additions & 0 deletions FlyingFox/Tests/HTTPClientTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// HTTPClientTests.swift
// FlyingFox
//
// Created by Simon Whitty on 8/06/2024.
// Copyright © 2024 Simon Whitty. All rights reserved.
//
// Distributed under the permissive MIT license
// Get the latest version from here:
//
// https://github.com/swhitty/FlyingFox
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

@_spi(Private) import struct FlyingFox._HTTPClient
@testable import FlyingFox
@testable import FlyingSocks
import XCTest
import Foundation

final class HTTPClientTests: XCTestCase {

func testClient() async throws {
// given
let server = HTTPServer(port: 0)
let task = Task { try await server.start() }
defer { task.cancel() }
let client = _HTTPClient()

// when
let port = try await server.waitForListeningPort()
let response = try await client.sendHTTPRequest(HTTPRequest.make(), to: .loopback(port: port))

// then
XCTAssertEqual(response.statusCode, .notFound)
}

}
2 changes: 1 addition & 1 deletion FlyingFox/Tests/HTTPServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
// SOFTWARE.
//

@testable import FlyingFox
@_spi(Private) @testable import FlyingFox
@testable import FlyingSocks
import XCTest
import Foundation
Expand Down
2 changes: 1 addition & 1 deletion FlyingFox/Tests/WebSocket/WSFrameEncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
// SOFTWARE.
//

@testable import FlyingFox
@_spi(Private) @testable import FlyingFox
@testable import FlyingSocks
import Foundation
import XCTest
Expand Down

0 comments on commit 2e83805

Please sign in to comment.