-
Notifications
You must be signed in to change notification settings - Fork 16
/
HttpEndpointTest.swift
74 lines (50 loc) · 2.54 KB
/
HttpEndpointTest.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
@testable import CioInternalCommon
import Foundation
import SharedTests
import XCTest
class HttpEndpointTest: UnitTest {
private let defaultEndpoint = CIOApiEndpoint.findAccountRegion
private var httpBaseUrls: HttpBaseUrls!
override func setUp() {
super.setUp()
setHttpBaseUrls()
}
private func setHttpBaseUrls(trackingApi: String = Region.US.productionTrackingUrl) {
httpBaseUrls = HttpBaseUrls(trackingApi: trackingApi)
}
// MARK: getUrlString
func test_getUrlString_givenEmptyStringBaseUrl_expectEmptyString() {
setHttpBaseUrls(trackingApi: "")
let actual = defaultEndpoint.getUrlString(baseUrls: httpBaseUrls)
XCTAssertEqual(actual, "")
}
func test_getUrlString_givenBaseUrlTrailingSlash_expectValidBaseUrl() {
let expected = "https://customer.io/api/v1/accounts/region"
setHttpBaseUrls(trackingApi: "https://customer.io/")
let actual = defaultEndpoint.getUrlString(baseUrls: httpBaseUrls)
XCTAssertEqual(actual, expected)
}
func test_getUrlString_givenBaseUrlNoTrailingSlash_expectValueBaseUrl() {
let expected = "https://customer.io/api/v1/accounts/region"
setHttpBaseUrls(trackingApi: "https://customer.io")
let actual = defaultEndpoint.getUrlString(baseUrls: httpBaseUrls)
XCTAssertEqual(actual, expected)
}
func test_getUrlString_givenPathWithSpecialCharacters_expectEncodedPath() {
let identifierWithSpecialChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~:/?#[]@!$&'()*+,;=%"
let endpoint = CIOApiEndpoint.identifyCustomer(identifier: identifierWithSpecialChar)
let rawPath = "/api/v1/customers/\(identifierWithSpecialChar)"
let expectedPath = rawPath.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? rawPath
let expected = "https://customer.io\(expectedPath)"
setHttpBaseUrls(trackingApi: "https://customer.io")
let actual = endpoint.getUrlString(baseUrls: httpBaseUrls)
XCTAssertEqual(actual, expected)
}
func test_getUrl_givenUnencodedPathWithSpecialCharacter_expectValidUrl() {
let identifierWithSpecialChar = "social-login|1234567890abcde"
let endpoint = CIOApiEndpoint.identifyCustomer(identifier: identifierWithSpecialChar)
setHttpBaseUrls(trackingApi: "https://customer.io")
let actualUrl = endpoint.getUrl(baseUrls: httpBaseUrls)
XCTAssertNotNil(actualUrl, "Expected valid URL but got nil. Ensure path is encoded correctly.")
}
}