|
| 1 | +import Foundation |
| 2 | +import XCTest |
| 3 | + |
| 4 | +import WordPressShared |
| 5 | +@testable import WordPressKit |
| 6 | + |
| 7 | +extension WordPressComRestApiTests { |
| 8 | + |
| 9 | + func testThatAppendingLocaleWorks() { |
| 10 | + // Given |
| 11 | + let path = "/path/path" |
| 12 | + let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug |
| 13 | + |
| 14 | + // When |
| 15 | + let localeAppendedPath = WordPressComRestApi().buildRequestURLFor(path: path) |
| 16 | + |
| 17 | + // Then |
| 18 | + XCTAssertNotNil(localeAppendedPath) |
| 19 | + let actualURL = URL(string: localeAppendedPath!, relativeTo: URL(string: WordPressComRestApi.apiBaseURLString)) |
| 20 | + XCTAssertNotNil(actualURL) |
| 21 | + |
| 22 | + let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false) |
| 23 | + XCTAssertNotNil(actualURLComponents) |
| 24 | + |
| 25 | + let expectedPath = path |
| 26 | + let actualPath = actualURLComponents!.path |
| 27 | + XCTAssertEqual(expectedPath, actualPath) |
| 28 | + |
| 29 | + let actualQueryItems = actualURLComponents!.queryItems |
| 30 | + XCTAssertNotNil(actualQueryItems) |
| 31 | + |
| 32 | + let expectedQueryItemCount = 1 |
| 33 | + let actualQueryItemCount = actualQueryItems!.count |
| 34 | + XCTAssertEqual(expectedQueryItemCount, actualQueryItemCount) |
| 35 | + |
| 36 | + let actualQueryItem = actualQueryItems!.first |
| 37 | + XCTAssertNotNil(actualQueryItem!) |
| 38 | + |
| 39 | + let actualQueryItemKey = actualQueryItem!.name |
| 40 | + let expectedQueryItemKey = WordPressComRestApi.localeKey |
| 41 | + XCTAssertEqual(expectedQueryItemKey, actualQueryItemKey) |
| 42 | + |
| 43 | + let actualQueryItemValue = actualQueryItem!.value |
| 44 | + XCTAssertNotNil(actualQueryItemValue) |
| 45 | + |
| 46 | + let expectedQueryItemValue = preferredLanguageIdentifier |
| 47 | + XCTAssertEqual(expectedQueryItemValue, actualQueryItemValue!) |
| 48 | + } |
| 49 | + |
| 50 | + func testThatAppendingLocaleWorksWithExistingParams() { |
| 51 | + // Given |
| 52 | + let path = "/path/path" |
| 53 | + let params: [String: AnyObject] = [ |
| 54 | + "someKey": "value" as AnyObject |
| 55 | + ] |
| 56 | + |
| 57 | + // When |
| 58 | + let localeAppendedPath = WordPressComRestApi().buildRequestURLFor(path: path, parameters: params) |
| 59 | + |
| 60 | + // Then |
| 61 | + XCTAssertNotNil(localeAppendedPath) |
| 62 | + let actualURL = URL(string: localeAppendedPath!, relativeTo: URL(string: WordPressComRestApi.apiBaseURLString)) |
| 63 | + XCTAssertNotNil(actualURL) |
| 64 | + |
| 65 | + let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false) |
| 66 | + XCTAssertNotNil(actualURLComponents) |
| 67 | + |
| 68 | + let expectedPath = "/path/path" |
| 69 | + let actualPath = actualURLComponents!.path |
| 70 | + XCTAssertEqual(expectedPath, actualPath) |
| 71 | + |
| 72 | + let actualQueryItems = actualURLComponents!.queryItems |
| 73 | + XCTAssertNotNil(actualQueryItems) |
| 74 | + |
| 75 | + let expectedQueryItemCount = 1 |
| 76 | + let actualQueryItemCount = actualQueryItems!.count |
| 77 | + XCTAssertEqual(expectedQueryItemCount, actualQueryItemCount) |
| 78 | + |
| 79 | + let actualQueryString = actualURLComponents?.query |
| 80 | + XCTAssertNotNil(actualQueryString) |
| 81 | + |
| 82 | + let queryStringIncludesLocale = actualQueryString!.contains(WordPressComRestApi.localeKey) |
| 83 | + XCTAssertTrue(queryStringIncludesLocale) |
| 84 | + } |
| 85 | + |
| 86 | + func testThatLocaleIsNotAppendedIfAlreadyIncludedInPath() { |
| 87 | + // Given |
| 88 | + let preferredLanguageIdentifier = "foo" |
| 89 | + let path = "/path/path?locale=\(preferredLanguageIdentifier)" |
| 90 | + |
| 91 | + // When |
| 92 | + let localeAppendedPath = WordPressComRestApi().buildRequestURLFor(path: path) |
| 93 | + |
| 94 | + // Then |
| 95 | + XCTAssertNotNil(localeAppendedPath) |
| 96 | + let actualURL = URL(string: localeAppendedPath!, relativeTo: URL(string: WordPressComRestApi.apiBaseURLString)) |
| 97 | + XCTAssertNotNil(actualURL) |
| 98 | + |
| 99 | + let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false) |
| 100 | + XCTAssertNotNil(actualURLComponents) |
| 101 | + |
| 102 | + let expectedPath = "/path/path" |
| 103 | + let actualPath = actualURLComponents!.path |
| 104 | + XCTAssertEqual(expectedPath, actualPath) |
| 105 | + |
| 106 | + let actualQueryItems = actualURLComponents!.queryItems |
| 107 | + XCTAssertNotNil(actualQueryItems) |
| 108 | + |
| 109 | + let expectedQueryItemCount = 1 |
| 110 | + let actualQueryItemCount = actualQueryItems!.count |
| 111 | + XCTAssertEqual(expectedQueryItemCount, actualQueryItemCount) |
| 112 | + |
| 113 | + let actualQueryItem = actualQueryItems!.first |
| 114 | + XCTAssertNotNil(actualQueryItem!) |
| 115 | + |
| 116 | + let actualQueryItemKey = actualQueryItem!.name |
| 117 | + let expectedQueryItemKey = WordPressComRestApi.localeKey |
| 118 | + XCTAssertEqual(expectedQueryItemKey, actualQueryItemKey) |
| 119 | + |
| 120 | + let actualQueryItemValue = actualQueryItem!.value |
| 121 | + XCTAssertNotNil(actualQueryItemValue) |
| 122 | + |
| 123 | + let expectedQueryItemValue = preferredLanguageIdentifier |
| 124 | + XCTAssertEqual(expectedQueryItemValue, actualQueryItemValue!) |
| 125 | + } |
| 126 | + |
| 127 | + func testThatAppendingLocaleIgnoresIfAlreadyIncludedInRequestParameters() { |
| 128 | + // Given |
| 129 | + let inputPath = "/path/path" |
| 130 | + let expectedLocaleValue = "foo" |
| 131 | + let params: [String : AnyObject] = [ |
| 132 | + WordPressComRestApi.localeKey: expectedLocaleValue as AnyObject |
| 133 | + ] |
| 134 | + |
| 135 | + // When |
| 136 | + let requestURLString = WordPressComRestApi().buildRequestURLFor(path: inputPath, parameters: params) |
| 137 | + |
| 138 | + // Then |
| 139 | + let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug |
| 140 | + XCTAssertFalse(requestURLString!.contains(preferredLanguageIdentifier)) |
| 141 | + } |
| 142 | + |
| 143 | + func testThatLocaleIsNotAppendedWhenDisabled() { |
| 144 | + // Given |
| 145 | + let path = "/path/path" |
| 146 | + |
| 147 | + // When |
| 148 | + let api = WordPressComRestApi() |
| 149 | + api.appendsPreferredLanguageLocale = false |
| 150 | + let localeAppendedPath = api.buildRequestURLFor(path: path) |
| 151 | + |
| 152 | + // Then |
| 153 | + XCTAssertNotNil(localeAppendedPath) |
| 154 | + let actualURL = URL(string: localeAppendedPath!, relativeTo: URL(string: WordPressComRestApi.apiBaseURLString)) |
| 155 | + XCTAssertNotNil(actualURL) |
| 156 | + |
| 157 | + let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false) |
| 158 | + XCTAssertNotNil(actualURLComponents) |
| 159 | + |
| 160 | + let expectedPath = path |
| 161 | + let actualPath = actualURLComponents!.path |
| 162 | + XCTAssertEqual(expectedPath, actualPath) |
| 163 | + |
| 164 | + let actualQueryItems = actualURLComponents!.queryItems |
| 165 | + XCTAssertNil(actualQueryItems) |
| 166 | + } |
| 167 | +} |
0 commit comments