Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
296 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// BeerRequestTests.swift | ||
// PunkAPI_Tests | ||
// | ||
// Created by Andrea Altea on 24/02/2019. | ||
// Copyright © 2019 CocoaPods. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
import XCTest | ||
@testable import PunkAPI | ||
|
||
class BeerRequestTest: XCTestCase { | ||
|
||
func testRandomBeerRequest() { | ||
|
||
let request = RandomBeerRequest() | ||
XCTAssert(request.path == "beers/random") | ||
XCTAssert(request.parameters == nil) | ||
} | ||
|
||
func testBeerRequests() { | ||
|
||
self.testBeerRequest(with: 1) | ||
self.testBeerRequest(with: 2) | ||
self.testBeerRequest(with: 3) | ||
self.testBeerRequest(with: 4) | ||
self.testBeerRequest(with: 5) | ||
self.testBeerRequest(with: 6) | ||
self.testBeerRequest(with: 10) | ||
self.testBeerRequest(with: 1000) | ||
self.testBeerRequest(with: 165) | ||
} | ||
|
||
func testBeerRequest(with id: Int) { | ||
|
||
let request = BeerRequest(id: id) | ||
XCTAssert(request.path == "beers/\(id)") | ||
XCTAssert(request.parameters == nil) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// ConfigurationTests.swift | ||
// PunkAPI_Tests | ||
// | ||
// Created by Andrea Altea on 24/02/2019. | ||
// Copyright © 2019 CocoaPods. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import PunkAPI | ||
|
||
class ConfigurationTests: XCTestCase { | ||
|
||
func testConfigurationBuild() { | ||
|
||
let baseURL = URL(string: "test://api.test.it/")! | ||
let configuration = Configuration(sessionConfiguration: .ephemeral, baseURL: baseURL) | ||
|
||
XCTAssert(configuration.baseURL == baseURL) | ||
} | ||
|
||
func testDefaultConfiguration() { | ||
|
||
let configuration = Configuration.default | ||
XCTAssert(configuration.baseURL.absoluteString == "https://api.punkapi.com/v2/") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// MockSession.swift | ||
// PunkAPI_Tests | ||
// | ||
// Created by Andrea Altea on 24/02/2019. | ||
// Copyright © 2019 CocoaPods. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
class MockURLSession: URLSession { | ||
|
||
var urlCheckBlock: ((_ url: URL) -> Void)? | ||
|
||
var responseConfig: (data: Data?, response: HTTPURLResponse?, error: Error?)? | ||
|
||
override func dataTask(with url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask { | ||
|
||
if let urlCheckBlock = self.urlCheckBlock { | ||
urlCheckBlock(url) | ||
} | ||
|
||
return MockSessionDataTask(config: self.responseConfig, completionHandler: completionHandler) | ||
} | ||
} | ||
|
||
class MockSessionDataTask: URLSessionDataTask { | ||
|
||
enum DataError: String, ConvertibleError { | ||
|
||
case notConfigured = "Not Configured" | ||
} | ||
|
||
var config: (data: Data?, response: HTTPURLResponse?, error: Error?)? | ||
|
||
var completionHandler: (Data?, URLResponse?, Error?) -> Void | ||
|
||
init(config: (data: Data?, response: HTTPURLResponse?, error: Error?)?, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) { | ||
|
||
self.config = config | ||
self.completionHandler = completionHandler | ||
} | ||
|
||
override func resume() { | ||
|
||
guard let config = self.config else { | ||
|
||
completionHandler(nil, nil, DataError.notConfigured) | ||
return | ||
} | ||
completionHandler(config.data, config.response, config.error) | ||
} | ||
} | ||
|
||
protocol ConvertibleError: Error { | ||
|
||
var message: String { get } | ||
} | ||
|
||
extension ConvertibleError where Self: RawRepresentable, Self.RawValue == String{ | ||
|
||
var message: String { | ||
return self.rawValue | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// URLBuildTests.swift | ||
// PunkAPI_Example | ||
// | ||
// Created by Andrea Altea on 24/02/2019. | ||
// Copyright © 2019 CocoaPods. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import PunkAPI | ||
|
||
class URLBuildTests: XCTestCase { | ||
|
||
var baseURL: URL! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
|
||
self.baseURL = URL(string: "https://api.test.it/v2") | ||
} | ||
|
||
func testBaseURLBuilding() { | ||
|
||
let request = MockRequest(path: "beer/1", parameters: nil) | ||
let url = self.baseURL.url(request: request) | ||
XCTAssert(url?.absoluteString == "https://api.test.it/v2/beer/1") | ||
} | ||
|
||
func testWrongRelativePathURLBuilding() { | ||
|
||
let request = MockRequest(path: "/beer/1", parameters: nil) | ||
let url = self.baseURL.url(request: request) | ||
XCTAssert(url?.absoluteString == "https://api.test.it/v2/beer/1") | ||
} | ||
|
||
func testParametersURLBuilding() { | ||
|
||
let request = MockRequest(path: "/beer", | ||
parameters: ["integer": 1, "string": "text", "object": NSNumber(value: 3)]) | ||
guard let url = self.baseURL.url(request: request) else { | ||
return XCTFail("undefined url") | ||
} | ||
guard let queryItems = URLComponents(url: url, resolvingAgainstBaseURL: true)?.queryItems else { | ||
return XCTFail("undefined query") | ||
} | ||
XCTAssert(queryItems.count == 3) | ||
XCTAssert(queryItems.contains(URLQueryItem(name: "integer", value: "1"))) | ||
XCTAssert(queryItems.contains(URLQueryItem(name: "string", value: "text"))) | ||
XCTAssert(queryItems.contains(URLQueryItem(name: "object", value: "3"))) | ||
} | ||
} | ||
|
||
struct MockRequest: Request { | ||
|
||
var path: String | ||
var parameters: [String : Any]? = nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.