|
| 1 | +import Foundation |
| 2 | +import XCTest |
| 3 | + |
| 4 | +import URLMatcher |
| 5 | + |
| 6 | +final class URLConvertibleTests: XCTestCase { |
| 7 | + |
| 8 | + // MARK: URL |
| 9 | + |
| 10 | + func test_urlValue_returns_an_URL_instance() { |
| 11 | + // given |
| 12 | + let url = URL(string: "https://xoul.kr")! |
| 13 | + |
| 14 | + // then |
| 15 | + XCTAssertEqual(url.urlValue, url) |
| 16 | + XCTAssertEqual(url.absoluteString.urlValue, url) |
| 17 | + } |
| 18 | + |
| 19 | + func test_urlValue_returns_an_URL_instance_from_unicode_string() { |
| 20 | + // given |
| 21 | + let urlString = "https://xoul.kr/한글" |
| 22 | + |
| 23 | + // then |
| 24 | + XCTAssertEqual(urlString.urlValue, URL(string: "https://xoul.kr/%ED%95%9C%EA%B8%80")!) |
| 25 | + } |
| 26 | + |
| 27 | + func test_urlStringValue_returns_a_URL_string_value() { |
| 28 | + // given |
| 29 | + let url = URL(string: "https://xoul.kr")! |
| 30 | + |
| 31 | + // then |
| 32 | + XCTAssertEqual(url.urlStringValue, url.absoluteString) |
| 33 | + XCTAssertEqual(url.absoluteString.urlStringValue, url.absoluteString) |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + // MARK: Query Parameters |
| 38 | + |
| 39 | + func test_queryParameters_when_there_is_no_query_string_return_empty_dict() { |
| 40 | + // given |
| 41 | + let url = "https://xoul.kr" |
| 42 | + |
| 43 | + // then |
| 44 | + XCTAssertEqual(url.urlValue?.queryParameters, [:]) |
| 45 | + XCTAssertEqual(url.urlStringValue.queryParameters, [:]) |
| 46 | + } |
| 47 | + |
| 48 | + func test_queryParameters_when_there_is_an_empty_query_string_returns_empty_dict() { |
| 49 | + // given |
| 50 | + let url = "https://xoul.kr?" |
| 51 | + |
| 52 | + // then |
| 53 | + XCTAssertEqual(url.urlValue?.queryParameters, [:]) |
| 54 | + XCTAssertEqual(url.urlStringValue.queryParameters, [:]) |
| 55 | + } |
| 56 | + |
| 57 | + func test_queryParameters_when_there_is_a_query_string() { |
| 58 | + // given |
| 59 | + let url = "https://xoul.kr?key=this%20is%20a%20value&greeting=hello+world!&int=12&int=34&url=https://foo/bar?hello=world" |
| 60 | + |
| 61 | + // then |
| 62 | + /// has proper keys |
| 63 | + XCTAssertEqual(Set(url.urlValue!.queryParameters.keys), ["key", "greeting", "int", "url"]) |
| 64 | + XCTAssertEqual(Set(url.urlStringValue.queryParameters.keys), ["key", "greeting", "int", "url"]) |
| 65 | + |
| 66 | + /// decodes a percent encoding |
| 67 | + XCTAssertEqual(url.urlValue?.queryParameters["key"], "this is a value") |
| 68 | + XCTAssertEqual(url.urlStringValue.queryParameters["key"], "this is a value") |
| 69 | + |
| 70 | + /// doesn't convert + to whitespace |
| 71 | + XCTAssertEqual(url.urlValue?.queryParameters["greeting"], "hello+world!") |
| 72 | + XCTAssertEqual(url.urlStringValue.queryParameters["greeting"], "hello+world!") |
| 73 | + |
| 74 | + /// takes last value from duplicated keys |
| 75 | + XCTAssertEqual(url.urlValue?.queryParameters["int"], "34") |
| 76 | + XCTAssertEqual(url.urlStringValue.queryParameters["int"], "34") |
| 77 | + |
| 78 | + /// has an url |
| 79 | + XCTAssertEqual(url.urlValue?.queryParameters["url"], "https://foo/bar?hello=world") |
| 80 | + } |
| 81 | + |
| 82 | + // MARK: Query Items |
| 83 | + |
| 84 | + func test_queryItems_when_there_is_no_query_string_returns_nil() { |
| 85 | + // given |
| 86 | + let url = "https://xoul.kr" |
| 87 | + |
| 88 | + // then |
| 89 | + XCTAssertNil(url.urlValue?.queryItems) |
| 90 | + XCTAssertNil(url.urlStringValue.queryItems) |
| 91 | + } |
| 92 | + |
| 93 | + func test_queryItems_when_there_is_an_empty_query_string_returns_an_empty_array() { |
| 94 | + // given |
| 95 | + let url = "https://xoul.kr?" |
| 96 | + |
| 97 | + // then |
| 98 | + XCTAssertEqual(url.urlValue?.queryItems, []) |
| 99 | + XCTAssertEqual(url.urlStringValue.queryItems, []) |
| 100 | + } |
| 101 | + |
| 102 | + func test_queryItems_when_there_is_a_query_string() { |
| 103 | + // given |
| 104 | + let url = "https://xoul.kr?key=this%20is%20a%20value&greeting=hello+world!&int=12&int=34" |
| 105 | + |
| 106 | + // then |
| 107 | + /// has exact number of items |
| 108 | + XCTAssertEqual(url.urlValue?.queryItems?.count, 4) |
| 109 | + XCTAssertEqual(url.urlStringValue.queryItems?.count, 4) |
| 110 | + |
| 111 | + /// decodes a percent encoding |
| 112 | + XCTAssertEqual(url.urlValue?.queryItems?[0], URLQueryItem(name: "key", value: "this is a value")) |
| 113 | + XCTAssertEqual(url.urlStringValue.queryItems?[0], URLQueryItem(name: "key", value: "this is a value")) |
| 114 | + |
| 115 | + /// doesn't convert + to whitespace |
| 116 | + XCTAssertEqual(url.urlValue?.queryItems?[1], URLQueryItem(name: "greeting", value: "hello+world!")) |
| 117 | + XCTAssertEqual(url.urlStringValue.queryItems?[1], URLQueryItem(name: "greeting", value: "hello+world!")) |
| 118 | + |
| 119 | + /// takes all duplicated keys |
| 120 | + XCTAssertEqual(url.urlValue?.queryItems?[2], URLQueryItem(name: "int", value: "12")) |
| 121 | + XCTAssertEqual(url.urlValue?.queryItems?[3], URLQueryItem(name: "int", value: "34")) |
| 122 | + XCTAssertEqual(url.urlStringValue.queryItems?[2], URLQueryItem(name: "int", value: "12")) |
| 123 | + XCTAssertEqual(url.urlStringValue.queryItems?[3], URLQueryItem(name: "int", value: "34")) |
| 124 | + } |
| 125 | +} |
0 commit comments