Skip to content

Commit

Permalink
Covering edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSoundDefense committed Mar 18, 2017
1 parent f4baae1 commit 79c674f
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Sources/ServiceCredentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,20 @@ public struct ServiceCredentials {
private static func trimURL(_ url: String) -> String {
let trimmedURL = ServiceCredentials.removeTrailingSlash(from: url)

let alertIndex = trimmedURL.index(trimmedURL.startIndex, offsetBy: trimmedURL.characters.count-10)
let messageIndex = trimmedURL.index(trimmedURL.startIndex, offsetBy: trimmedURL.characters.count-12)
if trimmedURL.characters.count < 10 {
return trimmedURL
}

let alertIndex = trimmedURL.index(trimmedURL.startIndex, offsetBy: trimmedURL.characters.count-10)
if trimmedURL.substring(from: alertIndex) == "/alerts/v1" {
return trimmedURL.substring(to: alertIndex)
}

if trimmedURL.characters.count < 12 {
return trimmedURL
}

let messageIndex = trimmedURL.index(trimmedURL.startIndex, offsetBy: trimmedURL.characters.count-12)
if trimmedURL.substring(from: messageIndex) == "/messages/v1" {
return trimmedURL.substring(to: messageIndex)
}
Expand All @@ -55,6 +62,10 @@ public struct ServiceCredentials {

// Trim off a trailing slash from the URL.
private static func removeTrailingSlash(from url: String) -> String {
if url.characters.count < 1 {
return url
}

var urlCopy = url
var lastIndex = urlCopy.index(urlCopy.startIndex, offsetBy: urlCopy.characters.count-1)
while urlCopy.characters.count > 1 && urlCopy[lastIndex] == "/" {
Expand Down

0 comments on commit 79c674f

Please sign in to comment.