-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunit_ApiProviderTests.swift
103 lines (93 loc) · 4.26 KB
/
unit_ApiProviderTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// unit_ApiProviderTests.swift
// YM-API
//
// Created by Developer on 02.09.2021.
//
import XCTest
#if canImport(YmuzApiMac)
@testable import YmuzApiMac
#endif
#if canImport(YmuzApi)
@testable import YmuzApi
#endif
class unit_ApiProviderTests: XCTestCase {
var client: YMClient!
var device: YMDevice {get {return TestConstants.device}}
var apiLang: ApiLanguage {get {return TestConstants.apiLang}}
var token: String {get {return TestCredentials.token}}
var xToken: String {get {return TestCredentials.xToken}}
var uid: Int {get {return TestCredentials.uid}}
override func setUp() {
client = YMClient.initialize(device: device, lang: apiLang, uid: uid, token: token, xToken: xToken)
}
override func tearDown() {
client = nil
}
func testDataResponse() {
let exp = self.expectation(description: "Request time-out expectation")
client.sendRawDataRequest(basePath: "https://api.music.yandex.net/", payloadPath: "account/status", authHeaderValue: token, headers: [:], method: "GET", bodyData: nil, completion: { (result: Result<Data, YMError>) in
let data = try? result.get()
XCTAssertNotNil(data, "Response data is nil")
XCTAssertTrue(data?.count != 0, "Response data is empty")
exp.fulfill()
})
waitForExpectations(timeout: 5) { error in
if let g_error = error
{
print(g_error)
XCTAssert(false, "Timeout error: " + g_error.localizedDescription)
}
}
}
func testJsonResponse() {
let exp = self.expectation(description: "Request time-out expectation")
client.sendRawDataRequest(basePath: "https://api.music.yandex.net/", payloadPath: "account/status", authHeaderValue: token, headers: [:], method: "GET", bodyData: nil, completion: { (result: Result<[String: Any], YMError>) in
let json = try? result.get()
XCTAssertNotNil(json, "Response json is nil")
XCTAssertTrue(json?.count != 0, "Response json is empty")
exp.fulfill()
})
waitForExpectations(timeout: 5) { error in
if let g_error = error
{
print(g_error)
XCTAssert(false, "Timeout error: " + g_error.localizedDescription)
}
}
}
func testYmParsedPayloadResponse() {
let exp = self.expectation(description: "Request time-out expectation")
client.sendRawDataRequest(basePath: "https://api.music.yandex.net/", payloadPath: "account/status", authHeaderValue: token, headers: [:], method: "GET", bodyData: nil, completion: { (result: Result<YMResponse, YMError>) in
let ymResponse = try? result.get()
XCTAssertNotNil(ymResponse, "Prased response is nil")
XCTAssertNotNil(ymResponse?.invocationInfo, "Response invocation info is nil")
XCTAssertTrue(ymResponse?.result != nil && ymResponse?.error == nil, "Response payload data is nil")
exp.fulfill()
})
waitForExpectations(timeout: 5) { error in
if let g_error = error
{
print(g_error)
XCTAssert(false, "Timeout error: " + g_error.localizedDescription)
}
}
}
func testYmParsedErrorResponse() {
let exp = self.expectation(description: "Request time-out expectation")
client.sendRawDataRequest(basePath: "https://api.music.yandex.net/", payloadPath: "queues", authHeaderValue: token, headers: ["Content-Type": "application/json; charset=UTF-8"], method: "POST", bodyData: "{}".data(using: .utf8), completion: { (result: Result<YMResponse, YMError>) in
let ymResponse = try? result.get()
XCTAssertNotNil(ymResponse, "Prased response is nil")
XCTAssertNotNil(ymResponse?.invocationInfo, "Response invocation info is nil")
XCTAssertTrue(ymResponse?.error != nil && ymResponse?.result == nil, "Response error block is nil")
exp.fulfill()
})
waitForExpectations(timeout: 5) { error in
if let g_error = error
{
print(g_error)
XCTAssert(false, "Timeout error: " + g_error.localizedDescription)
}
}
}
}