Skip to content

Commit

Permalink
Handle URL double encoding & replace bid price on Native Ad (#902)
Browse files Browse the repository at this point in the history
* feat: handle double encoding & replace bid price on Native Ad

* test: update test signature
  • Loading branch information
OlenaPostindustria authored Aug 15, 2023
1 parent 641e2c4 commit 4ee7509
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 8 deletions.
4 changes: 4 additions & 0 deletions PrebidMobile.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
533FDF852A12030C0066ED5A /* MockPrebidJSLibraryManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 533FDF842A12030C0066ED5A /* MockPrebidJSLibraryManager.swift */; };
5355ACA929C454070014F16E /* VAST_with_empty_companion.xml in Resources */ = {isa = PBXBuildFile; fileRef = 5355ACA829C454070014F16E /* VAST_with_empty_companion.xml */; };
5355ACAB29C454770014F16E /* CreativeModelCollectionMakerVASTTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5355ACAA29C454770014F16E /* CreativeModelCollectionMakerVASTTests.swift */; };
536A39262A84C50F00B1CCEA /* StringExtensionsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 536A39252A84C50F00B1CCEA /* StringExtensionsTest.swift */; };
536A427F282D11DA0069E9B2 /* PrebidServerConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 536A427E282D11DA0069E9B2 /* PrebidServerConnection.swift */; };
536A4283282D12E80069E9B2 /* PrebidServerConnectionProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 536A4282282D12E80069E9B2 /* PrebidServerConnectionProtocol.swift */; };
537B6518283372FD008AE9D1 /* PathBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 537B6517283372FD008AE9D1 /* PathBuilder.swift */; };
Expand Down Expand Up @@ -911,6 +912,7 @@
533FDF842A12030C0066ED5A /* MockPrebidJSLibraryManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockPrebidJSLibraryManager.swift; sourceTree = "<group>"; };
5355ACA829C454070014F16E /* VAST_with_empty_companion.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = VAST_with_empty_companion.xml; sourceTree = "<group>"; };
5355ACAA29C454770014F16E /* CreativeModelCollectionMakerVASTTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreativeModelCollectionMakerVASTTests.swift; sourceTree = "<group>"; };
536A39252A84C50F00B1CCEA /* StringExtensionsTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringExtensionsTest.swift; sourceTree = "<group>"; };
536A427E282D11DA0069E9B2 /* PrebidServerConnection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrebidServerConnection.swift; sourceTree = "<group>"; };
536A4282282D12E80069E9B2 /* PrebidServerConnectionProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrebidServerConnectionProtocol.swift; sourceTree = "<group>"; };
537B6517283372FD008AE9D1 /* PathBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PathBuilder.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2973,6 +2975,7 @@
925D5E542737F2A900A8A2B5 /* PBMFunctionsPrivateTest.m */,
925D5E562737F2D900A8A2B5 /* PBMMacrosTest.m */,
925D5E582737F35500A8A2B5 /* UIViewExtensionsTest.swift */,
536A39252A84C50F00B1CCEA /* StringExtensionsTest.swift */,
);
path = UtilitiesExtensionsTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -3807,6 +3810,7 @@
925D5E302737ED8C00A8A2B5 /* PBMRewardedVideoViewTest.swift in Sources */,
922AFCCB27353B9A00732C53 /* MockServerRule.m in Sources */,
925D5D872737BAB300A8A2B5 /* PBMOpenMeasurementWrapperTest.swift in Sources */,
536A39262A84C50F00B1CCEA /* StringExtensionsTest.swift in Sources */,
925D5E782737F60100A8A2B5 /* VastEventTrackingTest.swift in Sources */,
533FDF852A12030C0066ED5A /* MockPrebidJSLibraryManager.swift in Sources */,
922AFD452737298200732C53 /* PBMHTMLCreativeTest_MRAIDPlayVideo.swift in Sources */,
Expand Down
7 changes: 5 additions & 2 deletions PrebidMobile/AdUnits/Native/NativeAd.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public class NativeAd: NSObject, CacheExpiryDelegate {
return nil
}

let macrosHelper = PBMORTBMacrosHelper(bid: rawBid)
rawBid.adm = macrosHelper.replaceMacros(in: rawBid.adm)
rawBid.nurl = macrosHelper.replaceMacros(in: rawBid.nurl)

let ad = NativeAd()

let internalEventTracker = PrebidServerEventTracker()
Expand Down Expand Up @@ -293,8 +297,7 @@ public class NativeAd: NSObject, CacheExpiryDelegate {
@objc private func handleClick() {
self.delegate?.adWasClicked?(ad: self)
if let clickUrl = nativeAdMarkup?.link?.url,
let clickUrlString = clickUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
let url = URL(string: clickUrlString) {
let url = clickUrl.encodedURL(with: .urlQueryAllowed) {
if openURLWithExternalBrowser(url: url) {
if let clickTrackers = nativeAdMarkup?.link?.clicktrackers {
fireClickTrackers(clickTrackersUrls: clickTrackers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,17 @@ extension String {

return false
}

func encodedURL(with characterSet: CharacterSet) -> URL? {
if let url = URL(string: self) {
return url
}

if let encodedURLString = addingPercentEncoding(withAllowedCharacters: characterSet),
let url = URL(string: encodedURLString) {
return url
}

return nil
}
}
12 changes: 6 additions & 6 deletions PrebidMobile/TrackerManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ class TrackerManager: NSObject {

Log.debug("Internet is reachable - FIRING TRACKERS: \(arrayWithURLs)")
arrayWithURLs.forEach { urlString in
if let clickUrlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), let url = URL(string: clickUrlString)
{
if let url = urlString.encodedURL(with: .urlQueryAllowed) {
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { [weak self] data, _, error in
guard error == nil else {
Expand All @@ -80,12 +79,11 @@ class TrackerManager: NSObject {
return
}
if let completion = completion {
completion(true)
completion(true)
}
}.resume()
}
}

}

private func queueTrackerURLForRetry(URL: String, completion: OnComplete){
Expand Down Expand Up @@ -120,12 +118,15 @@ class TrackerManager: NSObject {
trackerArray.removeAll()
trackerRetryTimer?.invalidate()
}

if trackerArrayCopy?.count != 0 {
trackerArrayCopy?.forEach({ info in
if info.expired{
return
}
if let urlString = info.URL, let clickUrlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), let url = URL(string: clickUrlString)

if let urlString = info.URL,
let url = urlString.encodedURL(with: .urlQueryAllowed)
{
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { [weak self] data, _, error in
Expand All @@ -151,7 +152,6 @@ class TrackerManager: NSObject {
}
}.resume()
}

})
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*   Copyright 2018-2023 Prebid.org, Inc.

 Licensed 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 CONDITIONS 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 PrebidMobile

class StringExtensionsTest: XCTestCase {

func testEncodeURL() {
let str1 = "https://example.com/search?q=Prebid mobile&page=1"
let encodedStr1 = str1.encodedURL(with: .urlQueryAllowed)?.absoluteString

XCTAssertNotEqual(str1, encodedStr1)
XCTAssertTrue(encodedStr1!.contains("%20"))

let str2 = "https://example.com/search?q=Prebid%20mobile&page=1"
let encodedStr2 = str2.encodedURL(with: .urlQueryAllowed)?.absoluteString

XCTAssertEqual(str2, encodedStr2)
}
}

0 comments on commit 4ee7509

Please sign in to comment.