Skip to content

Commit eca1cca

Browse files
Move 'contentType' to content dictionary.
1 parent cb6dbdb commit eca1cca

File tree

5 files changed

+130
-6
lines changed

5 files changed

+130
-6
lines changed

Tests/common/CommonExtensions.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ extension String {
1515
}
1616

1717
extension Dictionary where Key == AnyHashable {
18-
func toData() -> Data {
18+
func toJsonData() -> Data {
1919
return try! JSONSerialization.data(withJSONObject: self, options: [])
2020
}
2121

22-
func toString() -> String {
23-
return String(data: toData(), encoding: .utf8)!
22+
func toJsonString() -> String {
23+
return String(data: toJsonData(), encoding: .utf8)!
2424
}
2525
}
2626

Tests/common/CommonMocks.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,13 @@ class MockNetworkSession: NetworkSessionProtocol {
162162

163163
convenience init(statusCode: Int = 200) {
164164
self.init(statusCode: statusCode,
165-
data: [:].toData(),
165+
data: [:].toJsonData(),
166166
error: nil)
167167
}
168168

169169
convenience init(statusCode: Int, json: [AnyHashable : Any], error: Error? = nil) {
170170
self.init(statusCode: statusCode,
171-
data: json.toData(),
171+
data: json.toJsonData(),
172172
error: error)
173173
}
174174

Tests/swift-sdk-swift-tests/InAppHelperTests.swift

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,116 @@ class InAppHelperTests: XCTestCase {
304304
TestUtils.validateMatch(keyPath: KeyPath("myPayload.var1"), value: "val1", inDictionary: dict, message: "Expected to find val1")
305305
}
306306

307+
// Remove this test when backend is fixed
308+
// This test assumes that certain parts of payload
309+
// are in 'customPayload' element instead of the right places.
310+
func testInAppPayloadParsingWithPreprocessing() {
311+
let customPayloadStr1 = """
312+
{
313+
"messageId": "overridden",
314+
"var1" : "value1",
315+
"obj1" : {
316+
"something" : true,
317+
"nothing" : "is nothing"
318+
},
319+
}
320+
"""
321+
var customPayload1 = customPayloadStr1.toJsonDict()
322+
customPayload1["inAppType"] = "default"
323+
customPayload1["channelName"] = "channel1"
324+
customPayload1["contentType"] = "html"
325+
326+
let customPayloadStr2 = """
327+
{
328+
"promoteToContent" : {
329+
"title" : "title",
330+
"subTitle" : "subtitle",
331+
"imageUrl" : "http://somewhere.com/something.jpg"
332+
}
333+
}
334+
"""
335+
var customPayload2 = customPayloadStr2.toJsonDict()
336+
customPayload2["inAppType"] = "inBox"
337+
customPayload2["channelName"] = "channel2"
338+
customPayload2["contentType"] = "html"
339+
340+
let payload = """
341+
{
342+
"inAppMessages" : [
343+
{
344+
"content" : {
345+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>"
346+
},
347+
"messageId" : "messageId1",
348+
"campaignId" : "campaignIdxxx",
349+
"trigger" : {
350+
"type" : "myNewKind",
351+
"myPayload" : {"var1" : "val1"}
352+
},
353+
"customPayload" : \(customPayload1.toJsonString())
354+
},
355+
{
356+
"content" : {
357+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>"
358+
},
359+
"messageId" : "messageId2",
360+
"campaignId" : "campaignIdxxx",
361+
"trigger" : {
362+
"type" : "myNewKind",
363+
"myPayload" : {"var1" : "val1"}
364+
},
365+
"customPayload" : \(customPayload2.toJsonString())
366+
},
367+
{
368+
"content" : {
369+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>"
370+
},
371+
"messageId" : "messageId3",
372+
"campaignId" : "campaignIdxxx",
373+
"trigger" : {
374+
"type" : "myNewKind",
375+
"myPayload" : {"var1" : "val1"}
376+
},
377+
"customPayload" : {}
378+
},
379+
{
380+
"content" : {
381+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>"
382+
},
383+
"messageId" : "messageId4",
384+
"campaignId" : "campaignIdxxx",
385+
"trigger" : {
386+
"type" : "myNewKind",
387+
"myPayload" : {"var1" : "val1"}
388+
}
389+
}
390+
]
391+
}
392+
""".toJsonDict()
393+
let messages = InAppHelper.inAppMessages(fromPayload: payload, internalApi: IterableAPI.internalImplementation!)
394+
395+
XCTAssertEqual(messages.count, 4)
396+
let message1 = messages[0]
397+
XCTAssertEqual(message1.messageId, "messageId1")
398+
XCTAssertEqual(message1.channelName, "channel1")
399+
XCTAssertEqual(message1.inAppType, .default)
400+
XCTAssertTrue(TestUtils.areEqual(dict1: message1.extraInfo!, dict2: customPayloadStr1.toJsonDict()))
401+
402+
let message2 = messages[1]
403+
XCTAssertEqual(message2.channelName, "channel2")
404+
XCTAssertEqual(message2.inAppType, .inBox)
405+
XCTAssertTrue(TestUtils.areEqual(dict1: message2.extraInfo!, dict2: customPayloadStr2.toJsonDict()))
406+
407+
let message3 = messages[2]
408+
XCTAssertEqual(message3.channelName, "")
409+
XCTAssertEqual(message3.inAppType, .default)
410+
411+
let message4 = messages[3]
412+
XCTAssertEqual(message4.channelName, "")
413+
XCTAssertEqual(message4.inAppType, .default)
414+
}
415+
416+
307417
func testInAppPayloadParsing() {
308418
let customPayloadStr1 = """
309419
{

swift-sdk.xcodeproj/xcshareddata/xcschemes/ios9-tests.xcscheme

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@
8383
BlueprintName = "swift-sdk-swift-tests"
8484
ReferencedContainer = "container:swift-sdk.xcodeproj">
8585
</BuildableReference>
86+
<SkippedTests>
87+
<Test
88+
Identifier = "LoggingTests">
89+
</Test>
90+
</SkippedTests>
8691
</TestableReference>
8792
<TestableReference
8893
skipped = "NO">

swift-sdk/Internal/InAppHelper.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,15 +339,24 @@ struct InAppHelper {
339339
}
340340

341341
moveValue(withKey: AnyHashable.ITBL_IN_APP_INAPP_TYPE, from: &customPayloadDict, to: &result)
342-
moveValue(withKey: AnyHashable.ITBL_IN_APP_CONTENT_TYPE, from: &customPayloadDict, to: &result)
343342
moveValue(withKey: AnyHashable.ITBL_IN_APP_CHANNEL_NAME, from: &customPayloadDict, to: &result)
344343

344+
if var contentDict = dict[.ITBL_IN_APP_CONTENT] as? [AnyHashable : Any] {
345+
moveValue(withKey: AnyHashable.ITBL_IN_APP_CONTENT_TYPE, from: &customPayloadDict, to: &contentDict)
346+
result[.ITBL_IN_APP_CONTENT] = contentDict
347+
}
348+
345349
result[.ITBL_IN_APP_CUSTOM_PAYLOAD] = customPayloadDict
346350

347351
return result
348352
}
349353

350354
private static func moveValue(withKey key: String, from source: inout [AnyHashable : Any], to destination: inout [AnyHashable : Any]) {
355+
guard destination[key] == nil else {
356+
// value exists in destination, so don't override
357+
return
358+
}
359+
351360
if let value = source[key] {
352361
destination[key] = value
353362
source[key] = nil

0 commit comments

Comments
 (0)