Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 42 additions & 21 deletions Tests/JetworkingTests/ClientTests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,54 @@ final class ClientTests: XCTestCase {

let expectation = self.expectation(description: "Wait for get")

client.get(endpoint: Endpoints.get.addQueryParameter(key: "SomeKey", value: "SomeValue")) { response, result in
let queryParameter: (key: String, value: String) = (key: "SomeKey", value: "SomeValue")
let endpoint: Endpoint<MockResponseForArguments<[String: String]>> = Endpoints.getWithBody()
client.get(endpoint: endpoint.addQueryParameter(key: queryParameter.key, value: queryParameter.value)) { response, result in

dispatchPrecondition(condition: .onQueue(DispatchQueue.main))

var resultData: [String: String]?
switch result {
case .failure:
break

case let .success(resultData):
print(resultData)
case let .success(data):
resultData = data.args
}

XCTAssertNotNil(response)
XCTAssertEqual(response?.statusCode, 200)
XCTAssertNotNil(resultData)
XCTAssertEqual(resultData?.first?.key, queryParameter.key)
XCTAssertEqual(resultData?.first?.value, queryParameter.value)
expectation.fulfill()
}

waitForExpectations(timeout: 5.0, handler: nil)
waitForExpectations(timeout: 5.0, handler: nil)
}

func testPostRequest() {
let client = Client(configuration: makeDefaultClientConfiguration())
let expectation = self.expectation(description: "Wait for post")

let body: MockBody = .init(foo1: "bar1", foo2: "bar2")
client.post(endpoint: Endpoints.post, body: body) { response, result in
let endpoint: Endpoint<MockResponseForRequestBody<MockBody>> = Endpoints.postWithBody()
client.post(endpoint: endpoint, body: body) { response, result in
dispatchPrecondition(condition: .onQueue(DispatchQueue.main))
var resultData: MockBody?
switch result {
case .failure:
break

case let .success(resultData):
print(resultData)
case let .success(data):
resultData = data.json
}

XCTAssertNotNil(response)
XCTAssertEqual(response?.statusCode, 200)
XCTAssertNotNil(resultData)
XCTAssertEqual(resultData?.foo1, body.foo1)
XCTAssertEqual(resultData?.foo2, body.foo2)
expectation.fulfill()
}

Expand All @@ -65,18 +76,23 @@ final class ClientTests: XCTestCase {
let expectation = self.expectation(description: "Wait for post")

let body: MockBody = .init(foo1: "bar1", foo2: "bar2")
client.put(endpoint: Endpoints.put, body: body) { response, result in
let endpoint: Endpoint<MockResponseForRequestBody<MockBody>> = Endpoints.putWithBody()
client.put(endpoint: endpoint, body: body) { response, result in
dispatchPrecondition(condition: .onQueue(DispatchQueue.main))
var resultData: MockBody?
switch result {
case .failure:
break

case let .success(resultData):
print(resultData)
case let .success(data):
resultData = data.json
}

XCTAssertNotNil(response)
XCTAssertEqual(response?.statusCode, 200)
XCTAssertNotNil(resultData)
XCTAssertEqual(resultData?.foo1, body.foo1)
XCTAssertEqual(resultData?.foo2, body.foo2)
expectation.fulfill()
}

Expand All @@ -88,24 +104,29 @@ final class ClientTests: XCTestCase {
let expectation = self.expectation(description: "Wait for post")

let body: MockBody = .init(foo1: "bar1", foo2: "bar2")
client.patch(endpoint: Endpoints.patch, body: body) { response, result in
let endpoint: Endpoint<MockResponseForRequestBody<MockBody>> = Endpoints.patchWithBody()
client.patch(endpoint: endpoint, body: body) { response, result in
dispatchPrecondition(condition: .onQueue(DispatchQueue.main))
var resultData: MockBody?
switch result {
case .failure:
break

case let .success(resultData):
print(resultData)
case let .success(data):
resultData = data.json
}

XCTAssertNotNil(response)
XCTAssertEqual(response?.statusCode, 200)
XCTAssertNotNil(resultData)
XCTAssertEqual(resultData?.foo1, body.foo1)
XCTAssertEqual(resultData?.foo2, body.foo2)
expectation.fulfill()
}

waitForExpectations(timeout: 5.0, handler: nil)
}

func testDeleteRequest() {
let client = Client(configuration: makeDefaultClientConfiguration())

Expand Down Expand Up @@ -203,7 +224,7 @@ final class ClientTests: XCTestCase {

XCTAssertTrue(result == .completed)
}

func testIncorrectOrderDueToAsyncRequestExecutor() {
let client = Client(configuration: makeDefaultClientConfiguration())

Expand All @@ -225,10 +246,10 @@ final class ClientTests: XCTestCase {

XCTAssertTrue(result == .incorrectOrder)
}

func testDownloadWithInvalidURL() {
let client = Client(configuration: makeDefaultClientConfiguration())

let url = URL(string: "smtp://www.mail.com")!
let task = client.download(
url: url,
Expand Down Expand Up @@ -274,14 +295,14 @@ final class ClientTests: XCTestCase {

expectation.fulfill()
}

waitForExpectations(timeout: 140.0, handler: nil)
}

func testUploadFile() {
let client = Client(configuration: makeDefaultClientConfiguration())
let expectation = self.expectation(description: "Wait for upload")

let url = URL(string: "https://catbox.moe/user/api.php")!
let path = Bundle.module.path(forResource: "avatar", ofType: "png")!
client.upload(
Expand All @@ -305,11 +326,11 @@ final class ClientTests: XCTestCase {

waitForExpectations(timeout: 5.0, handler: nil)
}

func testUploadMultipartData() {
let client = Client(configuration: makeDefaultClientConfiguration())
let expectation = self.expectation(description: "Wait for upload")

let url = URL(string: "https://catbox.moe/user/api.php")!

let filePath = Bundle.module.path(forResource: "avatar", ofType: ".png")!
Expand Down
16 changes: 16 additions & 0 deletions Tests/JetworkingTests/MockData/Endpoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,20 @@ enum Endpoints {
static let patch: Endpoint<Empty> = .init(pathComponent: "patch")
static let put: Endpoint<Empty> = .init(pathComponent: "put")
static let delete: Endpoint<Empty> = .init(pathComponent: "delete")

static func postWithBody<ResponseBody: Codable>() -> Endpoint<ResponseBody> {
return .init(pathComponent: "post")
}

static func getWithBody<ResponseBody: Codable>() -> Endpoint<ResponseBody> {
return .init(pathComponent: "get")
}

static func putWithBody<ResponseBody: Codable>() -> Endpoint<ResponseBody> {
return .init(pathComponent: "put")
}

static func patchWithBody<ResponseBody: Codable>() -> Endpoint<ResponseBody> {
return .init(pathComponent: "patch")
}
}
4 changes: 4 additions & 0 deletions Tests/JetworkingTests/MockData/MockResponseForArguments.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
struct MockResponseForArguments<ArgumentTypes: Codable>: Codable {
let args: ArgumentTypes
let url: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
struct MockResponseForRequestBody<RequestBodyType: Codable>: Codable {
let json: RequestBodyType
let url: String
}