-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathXcodeServerEndpointsTests.swift
240 lines (195 loc) · 9.65 KB
/
XcodeServerEndpointsTests.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//
// XcodeServerEndpointsTests.swift
// XcodeServerSDK
//
// Created by Mateusz Zając on 20/07/15.
// Copyright © 2015 Honza Dvorsky. All rights reserved.
//
import XCTest
@testable import XcodeServerSDK
class XcodeServerEndpointsTests: XCTestCase {
let serverConfig = try! XcodeServerConfig(host: "https://127.0.0.1", user: "test", password: "test")
var endpoints: XcodeServerEndpoints?
override func setUp() {
super.setUp()
self.endpoints = XcodeServerEndpoints(serverConfig: serverConfig)
}
// MARK: createRequest()
// If malformed URL is passed to request creation function it should early exit and retur nil
func testMalformedURLCreation() {
let expectation = endpoints?.createRequest(.GET, endpoint: .Bots, params: ["test": "test"], query: ["test//http\\": "!test"], body: ["test": "test"], doBasicAuth: true)
XCTAssertNil(expectation, "Shouldn't create request from malformed URL")
}
func testRequestCreationForEmptyAuthorizationParams() {
let expectedUrl = NSURL(string: "https://127.0.0.1:20343/api/bots/bot_id/integrations")
let expectedRequest = NSMutableURLRequest(URL: expectedUrl!)
// HTTPMethod
expectedRequest.HTTPMethod = "GET"
// Authorization header: "": ""
expectedRequest.setValue("Basic Og==", forHTTPHeaderField: "Authorization")
let noAuthorizationConfig = try! XcodeServerConfig(host: "https://127.0.0.1")
let noAuthorizationEndpoints = XcodeServerEndpoints(serverConfig: noAuthorizationConfig)
let request = noAuthorizationEndpoints.createRequest(.GET, endpoint: .Integrations, params: ["bot": "bot_id"], query: nil, body: nil, doBasicAuth: true)
XCTAssertEqual(expectedRequest, request!)
}
func testGETRequestCreation() {
let expectedUrl = NSURL(string: "https://127.0.0.1:20343/api/bots/bot_id/integrations?format=json")
let expectedRequest = NSMutableURLRequest(URL: expectedUrl!)
// HTTPMethod
expectedRequest.HTTPMethod = "GET"
// Authorization header: "test": "test"
expectedRequest.setValue("Basic dGVzdDp0ZXN0", forHTTPHeaderField: "Authorization")
let request = self.endpoints?.createRequest(.GET, endpoint: .Integrations, params: ["bot": "bot_id"], query: ["format": "json"], body: nil, doBasicAuth: true)
XCTAssertEqual(expectedRequest, request!)
}
func testPOSTRequestCreation() {
let expectedUrl = NSURL(string: "https://127.0.0.1:20343/api/auth/logout")
let expectedRequest = NSMutableURLRequest(URL: expectedUrl!)
// HTTPMethod
expectedRequest.HTTPMethod = "POST"
// HTTPBody
let expectedData = "{\n \"bodyParam\" : \"bodyValue\"\n}".dataUsingEncoding(NSUTF8StringEncoding)
expectedRequest.HTTPBody = expectedData!
expectedRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
let request = self.endpoints?.createRequest(.POST, endpoint: .Logout, params: nil, query: nil, body: ["bodyParam": "bodyValue"], doBasicAuth: false)
XCTAssertEqual(expectedRequest, request!)
XCTAssertEqual(expectedRequest.HTTPBody!, request!.HTTPBody!)
}
func testDELETERequestCreation() {
let expectedUrl = NSURL(string: "https://127.0.0.1:20343/api/bots/bot_id/rev_id")
let expectedRequest = NSMutableURLRequest(URL: expectedUrl!)
// HTTPMethod
expectedRequest.HTTPMethod = "DELETE"
let request = self.endpoints?.createRequest(.DELETE, endpoint: .Bots, params: ["bot": "bot_id", "rev": "rev_id"], query: nil, body: nil, doBasicAuth: false)
XCTAssertEqual(expectedRequest, request!)
}
// MARK: endpointURL(.Bots)
func testEndpointURLCreationForBotsPath() {
let expectation = "/api/bots"
let url = self.endpoints?.endpointURL(.Bots)
XCTAssertEqual(url!, expectation, "endpointURL(.Bots) should return \(expectation)")
}
func testEndpointURLCreationForBotsBotPath() {
let expectation = "/api/bots/bot_id"
let params = [
"bot": "bot_id"
]
let url = self.endpoints?.endpointURL(.Bots, params: params)
XCTAssertEqual(url!, expectation, "endpointURL(.Bots, \(params)) should return \(expectation)")
}
func testEndpointURLCreationForBotsBotRevPath() {
let expectation = "/api/bots/bot_id/rev_id"
let params = [
"bot": "bot_id",
"rev": "rev_id",
"method": "DELETE"
]
let url = self.endpoints?.endpointURL(.Bots, params: params)
XCTAssertEqual(url!, expectation, "endpointURL(.Bots, \(params)) should return \(expectation)")
}
// MARK: endpointURL(.Integrations)
func testEndpointURLCreationForIntegrationsPath() {
let expectation = "/api/integrations"
let url = self.endpoints?.endpointURL(.Integrations)
XCTAssertEqual(url!, expectation, "endpointURL(.Integrations) should return \(expectation)")
}
func testEndpointURLCreationForIntegrationsIntegrationPath() {
let expectation = "/api/integrations/integration_id"
let params = [
"integration": "integration_id"
]
let url = self.endpoints?.endpointURL(.Integrations, params: params)
XCTAssertEqual(url!, expectation, "endpointURL(.Integrations, \(params)) should return \(expectation)")
}
func testEndpointURLCreationForBotsBotIntegrationsPath() {
let expectation = "/api/bots/bot_id/integrations"
let params = [
"bot": "bot_id"
]
let url = self.endpoints?.endpointURL(.Integrations, params: params)
XCTAssertEqual(url!, expectation, "endpointURL(.Integrations, \(params)) should return \(expectation)")
}
// MARK: endpointURL(.CancelIntegration)
func testEndpointURLCreationForIntegrationsIntegrationCancelPath() {
let expectation = "/api/integrations/integration_id/cancel"
let params = [
"integration": "integration_id"
]
let url = self.endpoints?.endpointURL(.CancelIntegration, params: params)
XCTAssertEqual(url!, expectation, "endpointURL(.CancelIntegration, \(params)) should return \(expectation)")
}
// MARK: endpointURL(.Devices)
func testEndpointURLCreationForDevicesPath() {
let expectation = "/api/devices"
let url = self.endpoints?.endpointURL(.Devices)
XCTAssertEqual(url!, expectation, "endpointURL(.Devices) should return \(expectation)")
}
// MARK: endpointURL(.UserCanCreateBots)
func testEndpointURLCreationForAuthIsBotCreatorPath() {
let expectation = "/api/auth/isBotCreator"
let url = self.endpoints?.endpointURL(.UserCanCreateBots)
XCTAssertEqual(url!, expectation, "endpointURL(.UserCanCreateBots) should return \(expectation)")
}
// MARK: endpointURL(.Login)
func testEndpointURLCreationForAuthLoginPath() {
let expectation = "/api/auth/login"
let url = self.endpoints?.endpointURL(.Login)
XCTAssertEqual(url!, expectation, "endpointURL(.Login) should return \(expectation)")
}
// MARK: endpointURL(.Logout)
func testEndpointURLCreationForAuthLogoutPath() {
let expectation = "/api/auth/logout"
let url = self.endpoints?.endpointURL(.Logout)
XCTAssertEqual(url!, expectation, "endpointURL(.Logout) should return \(expectation)")
}
// MARK: endpointURL(.Platforms)
func testEndpointURLCreationForPlatformsPath() {
let expectation = "/api/platforms"
let url = self.endpoints?.endpointURL(.Platforms)
XCTAssertEqual(url!, expectation, "endpointURL(.Platforms) should return \(expectation)")
}
// MARK: endpointURL(.SCM_Branches)
func testEndpointURLCreationForScmBranchesPath() {
let expectation = "/api/scm/branches"
let url = self.endpoints?.endpointURL(.SCM_Branches)
XCTAssertEqual(url!, expectation, "endpointURL(.SCM_Branches) should return \(expectation)")
}
// MARK: endpointURL(.Repositories)
func testEndpointURLCreationForRepositoriesPath() {
let expectation = "/api/repositories"
let url = self.endpoints?.endpointURL(.Repositories)
XCTAssertEqual(url!, expectation, "endpointURL(.Repositories) should return \(expectation)")
}
// MARK: endpointURL(.Commits)
func testEndpointURLCreationForCommits() {
let expected = "/api/integrations/integration_id/commits"
let params = [
"integration": "integration_id"
]
let url = self.endpoints?.endpointURL(.Commits, params: params)
XCTAssertEqual(url!, expected)
}
// MARK: endpointURL(.Issues)
func testEndpointURLCreationForIssues() {
let expected = "/api/integrations/integration_id/issues"
let params = [
"integration": "integration_id"
]
let url = self.endpoints?.endpointURL(.Issues, params: params)
XCTAssertEqual(url!, expected)
}
// MARK: endpoingURL(.LiveUpdates)
func testEndpointURLCreationForLiveUpdates_Start() {
let expected = "/xcode/internal/socket.io/1"
let url = self.endpoints?.endpointURL(.LiveUpdates, params: nil)
XCTAssertEqual(url!, expected)
}
func testEndpointURLCreationForLiveUpdates_Poll() {
let expected = "/xcode/internal/socket.io/1/xhr-polling/sup3rS3cret"
let params = [
"poll_id": "sup3rS3cret"
]
let url = self.endpoints?.endpointURL(.LiveUpdates, params: params)
XCTAssertEqual(url!, expected)
}
}