-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default Header Locale Formatter #520
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
Copyright 2021 Adobe. All rights reserved. | ||
This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. You may obtain a copy | ||
of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software distributed under | ||
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
OF ANY KIND, either express or implied. See the License for the specific language | ||
governing permissions and limitations under the License. | ||
*/ | ||
|
||
import Foundation | ||
|
||
/// | ||
/// Helper class used to format the default http headers | ||
/// | ||
public struct DefaultHeadersFormatter { | ||
|
||
typealias httpHeaderConstants = HttpConnectionConstants.Header | ||
|
||
/// | ||
/// Gets the formatted locale | ||
/// - Parameter unformattedLocale: The unformatted locale as a string | ||
/// - Returns: The formatted locale as a string or nil if not able to format it. | ||
/// | ||
public static func getFormattedLocale(unformattedLocale: String) -> String { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Decided to change the name as well. Lmk what you think. |
||
// "^" // beginning of line | ||
// "([a-zA-Z]{2,3})" // language (required) (match group 1) | ||
// "(?:(?:-|_)[a-zA-Z]{3})?" // extlang (optional) | ||
// "(?:(?:-|_)[a-zA-Z]{4})?" // script (optional) | ||
// "(?:(?:-|_)([a-zA-Z]{2}|[0-9]{3}))?" // region (optional) (match group 2) | ||
// "(?:(?:\\.|-|_).*)?" // variant, extension, private, or anything else | ||
// "$" | ||
let pattern = #"^([a-zA-Z]{2,3})(?:(?:-|_)[a-zA-Z]{3})?(?:(?:-|_)[a-zA-Z]{4})?(?:(?:-|_)([a-zA-Z]{2}|[0-9]{3}))?(?:(?:\.|-|_).*)?$"# | ||
|
||
guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else { | ||
return unformattedLocale | ||
} | ||
|
||
let localeRange = NSRange(unformattedLocale.startIndex ..< unformattedLocale.endIndex, in: unformattedLocale) | ||
var language: String? | ||
var region: String? | ||
regex.enumerateMatches(in: unformattedLocale, options: [], range: localeRange) { match, _, _ in | ||
guard let match = match else { return } | ||
guard let languageCaptureRange = Range(match.range(at: 1), in: unformattedLocale) else { return } | ||
language = String(unformattedLocale[languageCaptureRange]) | ||
guard let regionCaptureRange = Range(match.range(at: 2), in: unformattedLocale) else { return } | ||
region = String(unformattedLocale[regionCaptureRange]) | ||
} | ||
|
||
if let language = language { | ||
if let region = region { | ||
return language + "-" + region | ||
} | ||
return language | ||
} | ||
|
||
// Default return if no language or region is found | ||
return "en-US" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
Copyright 2021 Adobe. All rights reserved. | ||
This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. You may obtain a copy | ||
of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software distributed under | ||
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
OF ANY KIND, either express or implied. See the License for the specific language | ||
governing permissions and limitations under the License. | ||
*/ | ||
|
||
import Foundation | ||
|
||
public enum HttpConnectionConstants { | ||
public enum ResponseCodes { | ||
static let HTTP_OK = 200 | ||
static let HTTP_NOT_FOUND = 404 | ||
static let HTTP_CLIENT_TIMEOUT = 408 | ||
static let HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416 | ||
static let HTTP_GATEWAY_TIMEOUT = 504 | ||
static let HTTP_UNAVAILABLE = 503 | ||
} | ||
|
||
public enum Header { | ||
static let HTTP_HEADER_KEY_CONTENT_TYPE = "Content-Type" | ||
static let HTTP_HEADER_KEY_ACCEPT_LANGUAGE = "Accept-Language" | ||
static let HTTP_HEADER_KEY_ACCEPT = "Accept" | ||
static let HTTP_HEADER_CONTENT_TYPE_JSON_APPLICATION = "application/json" | ||
static let HTTP_HEADER_CONTENT_TYPE_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded" | ||
static let HTTP_HEADER_ACCEPT_TEXT_HTML = "text/html" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
AEPServices/Tests/services/DefaultHeadersFormatterTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
Copyright 2021 Adobe. All rights reserved. | ||
This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. You may obtain a copy | ||
of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software distributed under | ||
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
OF ANY KIND, either express or implied. See the License for the specific language | ||
governing permissions and limitations under the License. | ||
*/ | ||
|
||
import XCTest | ||
|
||
@testable import AEPServices | ||
|
||
class DefaultHeadersFormatterTest: XCTestCase { | ||
private var localeStringArr: [[String: String]] = { | ||
[ | ||
["en-US": "C"], | ||
["en-US": "*"], | ||
["en-US": "POSIX"], | ||
["en-GB": "en_GB"], | ||
["en-US": "en_US.UTF-8"], | ||
["it-CH": "it_CH.ISO8859-1"], | ||
["it-CH": "it_CH.ISO8859-15"], | ||
["ja-JP": "ja_JP.SJIS"], | ||
["ru-RU": "ru_RU.KOI8-R"], | ||
["zh-HK": "zh_HK.Big5HKSCS"], | ||
["de-DE": "de-DE_phoneb"], | ||
["en": "en"], | ||
["ast": "ast"], | ||
["zh": "zh-Hant"], | ||
["zh-HK": "zh_Hant_HK"], | ||
["zh": "zh-yue"], | ||
["zh-HK": "zh_yue_HK"], | ||
["zh-HK": "zh-yue-Hant-HK"], | ||
["es-005": "es_005"], | ||
["de-DE": "de_DE_u_co_phonebk"], | ||
["sl": "sl_nedis"], | ||
["es-ES": "es-ES-i-klingon"] | ||
] | ||
}() | ||
|
||
func testDifferentFormattedLocales() { | ||
for dict in localeStringArr { | ||
guard let (k, v) = dict.first else { | ||
XCTFail() | ||
return | ||
} | ||
|
||
let formattedLocale = DefaultHeadersFormatter.getFormattedLocale(unformattedLocale: v) | ||
|
||
XCTAssertEqual(k, formattedLocale) | ||
} | ||
} | ||
|
||
func testEmptyLocale() { | ||
let formattedLocale = DefaultHeadersFormatter.getFormattedLocale(unformattedLocale: "") | ||
XCTAssertEqual("en-US", formattedLocale) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,4 @@ SPEC CHECKSUMS: | |
|
||
PODFILE CHECKSUM: 2f62254a30b50ed814c4364b36843ce9076bb9be | ||
|
||
COCOAPODS: 1.9.3 | ||
COCOAPODS: 1.10.1 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we still need this line?