-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from TrustlyInc/DEV
Version 3.2.2
- Loading branch information
Showing
10 changed files
with
185 additions
and
25 deletions.
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
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,69 @@ | ||
// | ||
// ValidationHelperTests.swift | ||
// TrustlySDK_Tests | ||
// | ||
// Created by Marcos Rivereto on 05/07/24. | ||
// Copyright © 2024 CocoaPods. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import TrustlySDK | ||
|
||
final class ValidationHelperTests: XCTestCase { | ||
|
||
override func setUpWithError() throws { | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
} | ||
|
||
func testRecognize400Error() throws { | ||
|
||
let content = "Error 404 not found" | ||
|
||
let expectdResult = true | ||
|
||
let matches = ValidationHelper.findMatchesForErrorCode(content: content) | ||
|
||
let isErrorCodePage = ValidationHelper.isErrorCodePage(matches: matches, content: content) | ||
|
||
XCTAssertEqual(expectdResult, isErrorCodePage) | ||
} | ||
|
||
func testRecognize500Error() throws { | ||
|
||
let content = "Internal error server 500" | ||
|
||
let expectdResult = true | ||
|
||
let matches = ValidationHelper.findMatchesForErrorCode(content: content) | ||
|
||
let isErrorCodePage = ValidationHelper.isErrorCodePage(matches: matches, content: content) | ||
|
||
XCTAssertEqual(expectdResult, isErrorCodePage) | ||
} | ||
|
||
func testRecognizeIsNotErrorPage() throws { | ||
|
||
let content = "Regular content" | ||
|
||
let expectdResult = false | ||
|
||
let matches = ValidationHelper.findMatchesForErrorCode(content: content) | ||
|
||
let isErrorCodePage = ValidationHelper.isErrorCodePage(matches: matches, content: content) | ||
|
||
XCTAssertEqual(expectdResult, isErrorCodePage) | ||
} | ||
|
||
func testMatchErrorCodes() throws { | ||
|
||
let content = "Internal error server 500" | ||
|
||
let expectdResult = 1 | ||
|
||
let matches = ValidationHelper.findMatchesForErrorCode(content: content) | ||
|
||
XCTAssertEqual(expectdResult, matches.count) | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// ValidationsHelper.swift | ||
// TrustlySDK | ||
// | ||
// Created by Marcos Rivereto on 05/07/24. | ||
// | ||
|
||
import Foundation | ||
import WebKit | ||
|
||
|
||
class ValidationHelper { | ||
|
||
static let HTTP_400_STATUS_CODE = 4 | ||
static let HTTP_500_STATUS_CODE = 5 | ||
|
||
static func isErrorCodePage(matches: [NSTextCheckingResult], content: String) -> Bool { | ||
for match in matches { | ||
|
||
let value = (Int(content[Range(match.range,in: content)!]) ?? 0) / 100 | ||
|
||
if value == HTTP_400_STATUS_CODE || value == HTTP_500_STATUS_CODE { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
static func findMatchesForErrorCode(content: String) -> [NSTextCheckingResult] { | ||
guard let regex = try? NSRegularExpression(pattern: "[0-9]+", options:NSRegularExpression.Options.caseInsensitive) else { return []} | ||
|
||
let matches = regex.matches(in: content, options:[], range: NSMakeRange(0, content.count)) | ||
|
||
return matches | ||
} | ||
|
||
} |
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