diff --git a/Podfile.lock b/Podfile.lock index 06b154ce..b85e81be 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -27,7 +27,7 @@ PODS: - OHHTTPStubs/Swift (6.1.0): - OHHTTPStubs/Default - UIDeviceIdentifier (1.1.4) - - WordPressKit (1.8.0-beta.1): + - WordPressKit (2.0.0-beta.1): - Alamofire (~> 4.7.3) - CocoaLumberjack (= 3.4.2) - NSObject-SafeExpectations (= 0.0.3) @@ -70,7 +70,7 @@ SPEC CHECKSUMS: OCMock: 43565190abc78977ad44a61c0d20d7f0784d35ab OHHTTPStubs: 1e21c7d2c084b8153fc53d48400d8919d2d432d0 UIDeviceIdentifier: 8f8a24b257a4d978c8d40ad1e7355b944ffbfa8c - WordPressKit: f2edbc8f99f7c698306193cfe216fd6e5b74fa54 + WordPressKit: f54ffbba343061975b72e64e5d49615c28156147 WordPressShared: a2fc2db66c210a05d317ae9678b5823dd6a4d708 wpxmlrpc: 6ba55c773cfa27083ae4a2173e69b19f46da98e2 diff --git a/WordPressKit.podspec b/WordPressKit.podspec index eb617fd0..ee1adffa 100644 --- a/WordPressKit.podspec +++ b/WordPressKit.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "WordPressKit" - s.version = "1.8.0" + s.version = "2.0.0-beta.1" s.summary = "WordPressKit offers a clean and simple WordPress.com and WordPress.org API." s.description = <<-DESC diff --git a/WordPressKit.xcodeproj/project.pbxproj b/WordPressKit.xcodeproj/project.pbxproj index 9b821d27..d4243f37 100644 --- a/WordPressKit.xcodeproj/project.pbxproj +++ b/WordPressKit.xcodeproj/project.pbxproj @@ -50,6 +50,7 @@ 73A2F38A21E7F81E00388609 /* WordPressComServiceRemote+SiteVerticalsPrompt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73A2F38921E7F81E00388609 /* WordPressComServiceRemote+SiteVerticalsPrompt.swift */; }; 73A2F38D21E7FC8200388609 /* WordPressComServiceRemoteTests+SiteVerticalsPrompt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73A2F38C21E7FC8200388609 /* WordPressComServiceRemoteTests+SiteVerticalsPrompt.swift */; }; 73A2F38E21E7FD9B00388609 /* site-verticals-prompt.json in Resources */ = {isa = PBXBuildFile; fileRef = 73A2F38B21E7FC2A00388609 /* site-verticals-prompt.json */; }; + 73B3DAD621FBB20D00B2CF18 /* WordPressComRestApiTests+Locale.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73B3DAD521FBB20D00B2CF18 /* WordPressComRestApiTests+Locale.swift */; }; 73D592FB21E550D300E4CF84 /* site-verticals-multiple.json in Resources */ = {isa = PBXBuildFile; fileRef = 73D592F821E550D200E4CF84 /* site-verticals-multiple.json */; }; 73D592FC21E550D300E4CF84 /* site-verticals-single.json in Resources */ = {isa = PBXBuildFile; fileRef = 73D592F921E550D300E4CF84 /* site-verticals-single.json */; }; 73D592FD21E550D300E4CF84 /* site-verticals-empty.json in Resources */ = {isa = PBXBuildFile; fileRef = 73D592FA21E550D300E4CF84 /* site-verticals-empty.json */; }; @@ -510,6 +511,7 @@ 73A2F38921E7F81E00388609 /* WordPressComServiceRemote+SiteVerticalsPrompt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WordPressComServiceRemote+SiteVerticalsPrompt.swift"; sourceTree = ""; }; 73A2F38B21E7FC2A00388609 /* site-verticals-prompt.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "site-verticals-prompt.json"; sourceTree = ""; }; 73A2F38C21E7FC8200388609 /* WordPressComServiceRemoteTests+SiteVerticalsPrompt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WordPressComServiceRemoteTests+SiteVerticalsPrompt.swift"; sourceTree = ""; }; + 73B3DAD521FBB20D00B2CF18 /* WordPressComRestApiTests+Locale.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WordPressComRestApiTests+Locale.swift"; sourceTree = ""; }; 73D592F821E550D200E4CF84 /* site-verticals-multiple.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "site-verticals-multiple.json"; sourceTree = ""; }; 73D592F921E550D300E4CF84 /* site-verticals-single.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "site-verticals-single.json"; sourceTree = ""; }; 73D592FA21E550D300E4CF84 /* site-verticals-empty.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "site-verticals-empty.json"; sourceTree = ""; }; @@ -1132,6 +1134,7 @@ 740B23D51F17F7C100067A2A /* XMLRPCTestable.swift */, FFE247A620C891D1002DF3A2 /* WordPressComOAuthTests.swift */, 74B335D91F06F3D60053A184 /* WordPressComRestApiTests.swift */, + 73B3DAD521FBB20D00B2CF18 /* WordPressComRestApiTests+Locale.swift */, 74B335DB1F06F4180053A184 /* WordPressOrgXMLRPCApiTests.swift */, 93BD273E1EE732CC002BB00B /* Accounts */, 826016FE1F9FD59400533B6C /* Activity */, @@ -2304,6 +2307,7 @@ 74B335D81F06F1CA0053A184 /* MockWordPressComRestApi.swift in Sources */, 74B5F0DE1EF82A9600B411E7 /* BlogServiceRemoteRESTTests.m in Sources */, 74E2294B1F1E73340085F7F2 /* SharingServiceRemoteTests.m in Sources */, + 73B3DAD621FBB20D00B2CF18 /* WordPressComRestApiTests+Locale.swift in Sources */, 9F3E0BAC20873785009CB5BA /* ServiceRequestTest.swift in Sources */, 93AC8EDE1ED32FD000900F5A /* StatsItemTests.m in Sources */, 740B23D31F17F6BB00067A2A /* PostServiceRemoteXMLRPCTests.swift in Sources */, diff --git a/WordPressKit/WordPressComRestApi.swift b/WordPressKit/WordPressComRestApi.swift index 13db1506..ffb3ae88 100644 --- a/WordPressKit/WordPressComRestApi.swift +++ b/WordPressKit/WordPressComRestApi.swift @@ -43,7 +43,7 @@ open class WordPressComRestApi: NSObject { fileprivate let backgroundUploads: Bool - fileprivate static let localeKey = "locale" + static let localeKey = "locale" fileprivate let oAuthToken: String? fileprivate let userAgent: String? @@ -145,7 +145,7 @@ open class WordPressComRestApi: NSObject { success: @escaping SuccessResponseBlock, failure: @escaping FailureReponseBlock) -> Progress? { - guard let URLString = buildRequestURLFor(path: urlString) else { + guard let URLString = buildRequestURLFor(path: urlString, parameters: parameters) else { let error = NSError(domain: String(describing: WordPressComRestApiError.self), code: WordPressComRestApiError.requestSerializationFailed.rawValue, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("Failed to serialize request to the REST API.", comment: "Error message to show when wrong URL format is used to access the REST API")]) @@ -241,7 +241,7 @@ open class WordPressComRestApi: NSObject { success: @escaping SuccessResponseBlock, failure: @escaping FailureReponseBlock) -> Progress? { - guard let URLString = buildRequestURLFor(path: URLString) else { + guard let URLString = buildRequestURLFor(path: URLString, parameters: parameters) else { let error = NSError(domain: String(describing: WordPressComRestApiError.self), code: WordPressComRestApiError.requestSerializationFailed.rawValue, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("Failed to serialize request to the REST API.", comment: "Error message to show when wrong URL format is used to access the REST API")]) @@ -299,18 +299,52 @@ open class WordPressComRestApi: NSObject { return "\(String(describing: oAuthToken)),\(String(describing: userAgent))".hashValue } - fileprivate func buildRequestURLFor(path: String) -> String? { - let pathWithLocale = appendLocaleIfNeeded(path) + /// This method assembles a valid request URL for the specified path & parameters. + /// The framework relies on a field (`appendsPreferredLanguageLocale`) to influence whether or not locale should be + /// added to the path of requests. This approach did not consider request parameters. + /// + /// This method now considers both the path and specified request parameters when performing the substitution. + /// It only accounts for the locale parameter. AlamoFire encodes other parameters via `SessionManager.request(_:method:parameters:encoding:headers:)` + /// + /// - Parameters: + /// - path: the path for the request, which might include `locale` + /// - parameters: the request parameters, which could conceivably include `locale` + /// - localeKey: the locale key to search for (`locale` in v1 endpoints, `_locale` for v2) + /// - Returns: a request URL if successful, `nil` otherwise. + /// + func buildRequestURLFor(path: String, parameters: [String: AnyObject]? = [:], localeKey: String = WordPressComRestApi.localeKey) -> String? { + let baseURL = URL(string: WordPressComRestApi.apiBaseURLString) - let requestURLString = URL(string: pathWithLocale, relativeTo: baseURL)?.absoluteString - return requestURLString + + guard let requestURLString = URL(string: path, relativeTo: baseURL)?.absoluteString, + let urlComponents = URLComponents(string: requestURLString) else { + + return nil + } + + let urlComponentsWithLocale = applyLocaleIfNeeded(urlComponents: urlComponents, parameters: parameters, localeKey: localeKey) + + return urlComponentsWithLocale?.url?.absoluteString } - fileprivate func appendLocaleIfNeeded(_ path: String) -> String { + private func applyLocaleIfNeeded(urlComponents: URLComponents, parameters: [String: AnyObject]? = [:], localeKey: String) -> URLComponents? { guard appendsPreferredLanguageLocale else { - return path + return urlComponents } - return WordPressComRestApi.pathByAppendingPreferredLanguageLocale(path) + + var componentsWithLocale = urlComponents + var existingQueryItems = componentsWithLocale.queryItems ?? [] + + let existingLocaleQueryItems = existingQueryItems.filter { $0.name == localeKey } + if let parameters = parameters, parameters[localeKey] == nil, existingLocaleQueryItems.isEmpty { + let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug + let localeQueryItem = URLQueryItem(name: localeKey, value: preferredLanguageIdentifier) + + existingQueryItems.append(localeQueryItem) + } + componentsWithLocale.queryItems = existingQueryItems + + return componentsWithLocale } @objc public func temporaryFileURL(withExtension fileExtension: String) -> URL { @@ -412,7 +446,6 @@ extension WordPressComRestApi { let errorWithLocalizedMessage = NSError(domain: nsError.domain, code: nsError.code, userInfo:userInfo) return errorWithLocalizedMessage } - } extension WordPressComRestApi { @@ -421,25 +454,6 @@ extension WordPressComRestApi { @objc class public func anonymousApi(userAgent: String) -> WordPressComRestApi { return WordPressComRestApi(oAuthToken: nil, userAgent: userAgent) } - - /// Append the user's preferred device locale as a query param to the URL path. - /// If the locale already exists the original path is returned. - /// - /// - Parameters: - /// - path: A URL string. Can be an absolute or relative URL string. - /// - /// - Returns: The path with the locale appended, or the original path if it already had a locale param. - /// - @objc class public func pathByAppendingPreferredLanguageLocale(_ path: String) -> String { - let localeKey = WordPressComRestApi.localeKey - if path.isEmpty || path.contains("\(localeKey)=") { - return path - } - let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug - let separator = path.contains("?") ? "&" : "?" - return "\(path)\(separator)\(localeKey)=\(preferredLanguageIdentifier)" - } - } @objc extension Progress { diff --git a/WordPressKitTests/WordPressComRestApiTests+Locale.swift b/WordPressKitTests/WordPressComRestApiTests+Locale.swift new file mode 100644 index 00000000..4459982d --- /dev/null +++ b/WordPressKitTests/WordPressComRestApiTests+Locale.swift @@ -0,0 +1,167 @@ +import Foundation +import XCTest + +import WordPressShared +@testable import WordPressKit + +extension WordPressComRestApiTests { + + func testThatAppendingLocaleWorks() { + // Given + let path = "/path/path" + let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug + + // When + let localeAppendedPath = WordPressComRestApi().buildRequestURLFor(path: path) + + // Then + XCTAssertNotNil(localeAppendedPath) + let actualURL = URL(string: localeAppendedPath!, relativeTo: URL(string: WordPressComRestApi.apiBaseURLString)) + XCTAssertNotNil(actualURL) + + let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false) + XCTAssertNotNil(actualURLComponents) + + let expectedPath = path + let actualPath = actualURLComponents!.path + XCTAssertEqual(expectedPath, actualPath) + + let actualQueryItems = actualURLComponents!.queryItems + XCTAssertNotNil(actualQueryItems) + + let expectedQueryItemCount = 1 + let actualQueryItemCount = actualQueryItems!.count + XCTAssertEqual(expectedQueryItemCount, actualQueryItemCount) + + let actualQueryItem = actualQueryItems!.first + XCTAssertNotNil(actualQueryItem!) + + let actualQueryItemKey = actualQueryItem!.name + let expectedQueryItemKey = WordPressComRestApi.localeKey + XCTAssertEqual(expectedQueryItemKey, actualQueryItemKey) + + let actualQueryItemValue = actualQueryItem!.value + XCTAssertNotNil(actualQueryItemValue) + + let expectedQueryItemValue = preferredLanguageIdentifier + XCTAssertEqual(expectedQueryItemValue, actualQueryItemValue!) + } + + func testThatAppendingLocaleWorksWithExistingParams() { + // Given + let path = "/path/path" + let params: [String: AnyObject] = [ + "someKey": "value" as AnyObject + ] + + // When + let localeAppendedPath = WordPressComRestApi().buildRequestURLFor(path: path, parameters: params) + + // Then + XCTAssertNotNil(localeAppendedPath) + let actualURL = URL(string: localeAppendedPath!, relativeTo: URL(string: WordPressComRestApi.apiBaseURLString)) + XCTAssertNotNil(actualURL) + + let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false) + XCTAssertNotNil(actualURLComponents) + + let expectedPath = "/path/path" + let actualPath = actualURLComponents!.path + XCTAssertEqual(expectedPath, actualPath) + + let actualQueryItems = actualURLComponents!.queryItems + XCTAssertNotNil(actualQueryItems) + + let expectedQueryItemCount = 1 + let actualQueryItemCount = actualQueryItems!.count + XCTAssertEqual(expectedQueryItemCount, actualQueryItemCount) + + let actualQueryString = actualURLComponents?.query + XCTAssertNotNil(actualQueryString) + + let queryStringIncludesLocale = actualQueryString!.contains(WordPressComRestApi.localeKey) + XCTAssertTrue(queryStringIncludesLocale) + } + + func testThatLocaleIsNotAppendedIfAlreadyIncludedInPath() { + // Given + let preferredLanguageIdentifier = "foo" + let path = "/path/path?locale=\(preferredLanguageIdentifier)" + + // When + let localeAppendedPath = WordPressComRestApi().buildRequestURLFor(path: path) + + // Then + XCTAssertNotNil(localeAppendedPath) + let actualURL = URL(string: localeAppendedPath!, relativeTo: URL(string: WordPressComRestApi.apiBaseURLString)) + XCTAssertNotNil(actualURL) + + let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false) + XCTAssertNotNil(actualURLComponents) + + let expectedPath = "/path/path" + let actualPath = actualURLComponents!.path + XCTAssertEqual(expectedPath, actualPath) + + let actualQueryItems = actualURLComponents!.queryItems + XCTAssertNotNil(actualQueryItems) + + let expectedQueryItemCount = 1 + let actualQueryItemCount = actualQueryItems!.count + XCTAssertEqual(expectedQueryItemCount, actualQueryItemCount) + + let actualQueryItem = actualQueryItems!.first + XCTAssertNotNil(actualQueryItem!) + + let actualQueryItemKey = actualQueryItem!.name + let expectedQueryItemKey = WordPressComRestApi.localeKey + XCTAssertEqual(expectedQueryItemKey, actualQueryItemKey) + + let actualQueryItemValue = actualQueryItem!.value + XCTAssertNotNil(actualQueryItemValue) + + let expectedQueryItemValue = preferredLanguageIdentifier + XCTAssertEqual(expectedQueryItemValue, actualQueryItemValue!) + } + + func testThatAppendingLocaleIgnoresIfAlreadyIncludedInRequestParameters() { + // Given + let inputPath = "/path/path" + let expectedLocaleValue = "foo" + let params: [String : AnyObject] = [ + WordPressComRestApi.localeKey: expectedLocaleValue as AnyObject + ] + + // When + let requestURLString = WordPressComRestApi().buildRequestURLFor(path: inputPath, parameters: params) + + // Then + let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug + XCTAssertFalse(requestURLString!.contains(preferredLanguageIdentifier)) + } + + func testThatLocaleIsNotAppendedWhenDisabled() { + // Given + let path = "/path/path" + + // When + let api = WordPressComRestApi() + api.appendsPreferredLanguageLocale = false + let localeAppendedPath = api.buildRequestURLFor(path: path) + + // Then + XCTAssertNotNil(localeAppendedPath) + let actualURL = URL(string: localeAppendedPath!, relativeTo: URL(string: WordPressComRestApi.apiBaseURLString)) + XCTAssertNotNil(actualURL) + + let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false) + XCTAssertNotNil(actualURLComponents) + + let expectedPath = path + let actualPath = actualURLComponents!.path + XCTAssertEqual(expectedPath, actualPath) + + let actualQueryItems = actualURLComponents!.queryItems + XCTAssertNil(actualQueryItems) + } +} diff --git a/WordPressKitTests/WordPressComRestApiTests.swift b/WordPressKitTests/WordPressComRestApiTests.swift index e26bfc87..f7009f1e 100644 --- a/WordPressKitTests/WordPressComRestApiTests.swift +++ b/WordPressKitTests/WordPressComRestApiTests.swift @@ -6,9 +6,10 @@ import WordPressShared class WordPressComRestApiTests: XCTestCase { - let wordPressComRestApi = "https://public-api.wordpress.com/" - let wordPressMediaRoute = "rest/v1.1/sites/0/media/" - let wordPressMediaNewEndpoint = "rest/v1.1/sites/0/media/new" + let scheme = "https" + let host = "public-api.wordpress.com" + let wordPressMediaRoutePath = "/rest/v1.1/sites/0/media/" + let wordPressMediaNewEndpointPath = "/rest/v1.1/sites/0/media/new" override func setUp() { super.setUp() @@ -19,17 +20,43 @@ class WordPressComRestApiTests: XCTestCase { OHHTTPStubs.removeAllStubs() } - fileprivate func isRestAPIRequest() -> OHHTTPStubsTestBlock { + private func isRestAPIRequest() -> OHHTTPStubsTestBlock { return { request in - let pathWithLocale = WordPressComRestApi.pathByAppendingPreferredLanguageLocale(self.wordPressMediaRoute) - return request.url?.absoluteString == self.wordPressComRestApi + pathWithLocale + guard let requestURL = request.url, let components = URLComponents(string: requestURL.absoluteString) else { + return false + } + + let expectedScheme = self.scheme + let actualScheme = components.scheme + + let expectedHost = self.host + let actualHost = components.host + + let expectedPath = self.wordPressMediaRoutePath + let actualPath = components.path + + let result = expectedScheme == actualScheme && expectedHost == actualHost && expectedPath == actualPath + return result } } - fileprivate func isRestAPIMediaNewRequest() -> OHHTTPStubsTestBlock { + private func isRestAPIMediaNewRequest() -> OHHTTPStubsTestBlock { return { request in - let pathWithLocale = WordPressComRestApi.pathByAppendingPreferredLanguageLocale(self.wordPressMediaNewEndpoint) - return request.url?.absoluteString == self.wordPressComRestApi + pathWithLocale + guard let requestURL = request.url, let components = URLComponents(string: requestURL.absoluteString) else { + return false + } + + let expectedScheme = self.scheme + let actualScheme = components.scheme + + let expectedHost = self.host + let actualHost = components.host + + let expectedPath = self.wordPressMediaNewEndpointPath + let actualPath = components.path + + let result = expectedScheme == actualScheme && expectedHost == actualHost && expectedPath == actualPath + return result } } @@ -41,7 +68,7 @@ class WordPressComRestApiTests: XCTestCase { let expect = self.expectation(description: "One callback should be invoked") let api = WordPressComRestApi(oAuthToken: "fakeToken") - api.GET(wordPressMediaRoute, parameters: nil, success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in + api.GET(wordPressMediaRoutePath, parameters: nil, success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in expect.fulfill() XCTAssert(responseObject is [String: AnyObject], "The response should be a dictionary") }, failure: { (error, httpResponse) in @@ -60,7 +87,7 @@ class WordPressComRestApiTests: XCTestCase { let expect = self.expectation(description: "One callback should be invoked") let api = WordPressComRestApi(oAuthToken: "fakeToken") - api.GET(wordPressMediaRoute, parameters: nil, success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in + api.GET(wordPressMediaRoutePath, parameters: nil, success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in expect.fulfill() XCTFail("This call should fail") }, failure: { (error, httpResponse) in @@ -78,7 +105,7 @@ class WordPressComRestApiTests: XCTestCase { } let expect = self.expectation(description: "One callback should be invoked") let api = WordPressComRestApi(oAuthToken: "fakeToken") - api.GET(wordPressMediaRoute, parameters: nil, success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in + api.GET(wordPressMediaRoutePath, parameters: nil, success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in expect.fulfill() XCTFail("This call should fail") }, failure: { (error, httpResponse) in @@ -96,7 +123,7 @@ class WordPressComRestApiTests: XCTestCase { } let expect = self.expectation(description: "One callback should be invoked") let api = WordPressComRestApi(oAuthToken: "fakeToken") - api.POST(wordPressMediaNewEndpoint, parameters: nil, success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in + api.POST(wordPressMediaNewEndpointPath, parameters: nil, success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in expect.fulfill() XCTFail("This call should fail") }, failure: { (error, httpResponse) in @@ -114,7 +141,7 @@ class WordPressComRestApiTests: XCTestCase { } let expect = self.expectation(description: "One callback should be invoked") let api = WordPressComRestApi(oAuthToken: "fakeToken") - api.POST(wordPressMediaNewEndpoint, parameters: nil, success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in + api.POST(wordPressMediaNewEndpointPath, parameters: nil, success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in expect.fulfill() XCTFail("This call should fail") }, failure: { (error, httpResponse) in @@ -132,7 +159,7 @@ class WordPressComRestApiTests: XCTestCase { } let expect = self.expectation(description: "One callback should be invoked") let api = WordPressComRestApi(oAuthToken: "fakeToken") - api.POST(wordPressMediaNewEndpoint, parameters: nil, success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in + api.POST(wordPressMediaNewEndpointPath, parameters: nil, success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in expect.fulfill() XCTFail("This call should fail") }, failure: { (error, httpResponse) in @@ -150,7 +177,7 @@ class WordPressComRestApiTests: XCTestCase { } let expect = self.expectation(description: "One callback should be invoked") let api = WordPressComRestApi(oAuthToken: "fakeToken") - api.multipartPOST(wordPressMediaNewEndpoint, parameters: nil, fileParts: [], success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in + api.multipartPOST(wordPressMediaNewEndpointPath, parameters: nil, fileParts: [], success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in expect.fulfill() XCTFail("This call should fail") }, failure: { (error, httpResponse) in @@ -161,38 +188,6 @@ class WordPressComRestApiTests: XCTestCase { self.waitForExpectations(timeout: 2, handler: nil) } - func testThatAppendingLocaleWorks() { - - let path = "path/path" - let localeKey = "locale" - let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug - let expectedPath = "\(path)?\(localeKey)=\(preferredLanguageIdentifier)" - - let localeAppendedPath = WordPressComRestApi.pathByAppendingPreferredLanguageLocale(path) - XCTAssert(localeAppendedPath == expectedPath, "Expected the locale to be appended to the path as (\(expectedPath)) but instead encountered (\(localeAppendedPath)).") - } - - func testThatAppendingLocaleWorksWithExistingParams() { - - let path = "path/path?someKey=value" - let localeKey = "locale" - let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug - let expectedPath = "\(path)&\(localeKey)=\(preferredLanguageIdentifier)" - - let localeAppendedPath = WordPressComRestApi.pathByAppendingPreferredLanguageLocale(path) - XCTAssert(localeAppendedPath == expectedPath, "Expected the locale to be appended to the path as (\(expectedPath)) but instead encountered (\(localeAppendedPath)).") - } - - func testThatAppendingLocaleIgnoresIfAlreadyIncluded() { - - let localeKey = "locale" - let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug - let path = "path/path?\(localeKey)=\(preferredLanguageIdentifier)&someKey=value" - - let localeAppendedPath = WordPressComRestApi.pathByAppendingPreferredLanguageLocale(path) - XCTAssert(localeAppendedPath == path, "Expected the locale to already be appended to the path as (\(path)) but instead encountered (\(localeAppendedPath)).") - } - func testStreamMethodCallWithInvalidFile() { stub(condition: isRestAPIMediaNewRequest()) { request in let stubPath = OHPathForFile("WordPressComRestApiMedia.json", type(of: self)) @@ -202,7 +197,7 @@ class WordPressComRestApiTests: XCTestCase { let expect = self.expectation(description: "One callback should be invoked") let api = WordPressComRestApi(oAuthToken: "fakeToken") let filePart = FilePart(parameterName: "file", url: URL(fileURLWithPath: "/a.txt") as URL, filename: "a.txt", mimeType: "image/jpeg") - api.multipartPOST(wordPressMediaNewEndpoint, parameters: nil, fileParts: [filePart], success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in + api.multipartPOST(wordPressMediaNewEndpointPath, parameters: nil, fileParts: [filePart], success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in expect.fulfill() XCTFail("This call should fail") }, failure: { (error, httpResponse) in @@ -226,7 +221,7 @@ class WordPressComRestApiTests: XCTestCase { let expect = self.expectation(description: "One callback should be invoked") let api = WordPressComRestApi(oAuthToken: "fakeToken") let filePart = FilePart(parameterName: "media[]", url: mediaURL as URL, filename: "test-image.jpg", mimeType: "image/jpeg") - let progress1 = api.multipartPOST(wordPressMediaNewEndpoint, parameters: nil, fileParts: [filePart], success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in + let progress1 = api.multipartPOST(wordPressMediaNewEndpointPath, parameters: nil, fileParts: [filePart], success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in XCTFail("This call should fail") }, failure: { (error, httpResponse) in print(error) @@ -235,7 +230,7 @@ class WordPressComRestApiTests: XCTestCase { } ) progress1?.cancel() - api.multipartPOST(wordPressMediaNewEndpoint, parameters: nil, fileParts: [filePart], success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in + api.multipartPOST(wordPressMediaNewEndpointPath, parameters: nil, fileParts: [filePart], success: { (responseObject: AnyObject, httpResponse: HTTPURLResponse?) in expect.fulfill() }, failure: { (error, httpResponse) in