diff --git a/Tests/JetworkingTests/ClientTests/ClientTests.swift b/Tests/JetworkingTests/ClientTests/ClientTests.swift index aa87893..4c5e2a8 100644 --- a/Tests/JetworkingTests/ClientTests/ClientTests.swift +++ b/Tests/JetworkingTests/ClientTests/ClientTests.swift @@ -17,24 +17,30 @@ 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> = 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() { @@ -42,18 +48,23 @@ final class ClientTests: XCTestCase { 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> = 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() } @@ -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> = 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() } @@ -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> = 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()) @@ -203,7 +224,7 @@ final class ClientTests: XCTestCase { XCTAssertTrue(result == .completed) } - + func testIncorrectOrderDueToAsyncRequestExecutor() { let client = Client(configuration: makeDefaultClientConfiguration()) @@ -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, @@ -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( @@ -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")! diff --git a/Tests/JetworkingTests/MockData/Endpoints.swift b/Tests/JetworkingTests/MockData/Endpoints.swift index b413120..093c55f 100644 --- a/Tests/JetworkingTests/MockData/Endpoints.swift +++ b/Tests/JetworkingTests/MockData/Endpoints.swift @@ -7,4 +7,20 @@ enum Endpoints { static let patch: Endpoint = .init(pathComponent: "patch") static let put: Endpoint = .init(pathComponent: "put") static let delete: Endpoint = .init(pathComponent: "delete") + + static func postWithBody() -> Endpoint { + return .init(pathComponent: "post") + } + + static func getWithBody() -> Endpoint { + return .init(pathComponent: "get") + } + + static func putWithBody() -> Endpoint { + return .init(pathComponent: "put") + } + + static func patchWithBody() -> Endpoint { + return .init(pathComponent: "patch") + } } diff --git a/Tests/JetworkingTests/MockData/MockResponseForArguments.swift b/Tests/JetworkingTests/MockData/MockResponseForArguments.swift new file mode 100644 index 0000000..dbafca7 --- /dev/null +++ b/Tests/JetworkingTests/MockData/MockResponseForArguments.swift @@ -0,0 +1,4 @@ +struct MockResponseForArguments: Codable { + let args: ArgumentTypes + let url: String +} diff --git a/Tests/JetworkingTests/MockData/MockResponseForRequestBody.swift b/Tests/JetworkingTests/MockData/MockResponseForRequestBody.swift new file mode 100644 index 0000000..02d576a --- /dev/null +++ b/Tests/JetworkingTests/MockData/MockResponseForRequestBody.swift @@ -0,0 +1,4 @@ +struct MockResponseForRequestBody: Codable { + let json: RequestBodyType + let url: String +}