Skip to content

Commit 62a6737

Browse files
authored
Merge pull request #1 from codecat15/urlComponent-getMethod
adding encodable extension and test cases
2 parents c0d92aa + 8a01e15 commit 62a6737

File tree

4 files changed

+259
-0
lines changed

4 files changed

+259
-0
lines changed

HttpUtility.xcodeproj/project.pbxproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
8656BC582483E3C60023549D /* EncodableExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8656BC572483E3C60023549D /* EncodableExtension.swift */; };
11+
8656BC5B2483E43D0023549D /* EncodableExtensionUnitTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8656BC5A2483E43D0023549D /* EncodableExtensionUnitTest.swift */; };
1012
86719EA024720BD1002A2AB0 /* HttpUtility.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 86719E9624720BD1002A2AB0 /* HttpUtility.framework */; };
1113
86719EA524720BD1002A2AB0 /* HttpUtilityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86719EA424720BD1002A2AB0 /* HttpUtilityTests.swift */; };
1214
86719EA724720BD1002A2AB0 /* HttpUtility.h in Headers */ = {isa = PBXBuildFile; fileRef = 86719E9924720BD1002A2AB0 /* HttpUtility.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -24,6 +26,8 @@
2426
/* End PBXContainerItemProxy section */
2527

2628
/* Begin PBXFileReference section */
29+
8656BC572483E3C60023549D /* EncodableExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EncodableExtension.swift; sourceTree = "<group>"; };
30+
8656BC5A2483E43D0023549D /* EncodableExtensionUnitTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EncodableExtensionUnitTest.swift; sourceTree = "<group>"; };
2731
86719E9624720BD1002A2AB0 /* HttpUtility.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = HttpUtility.framework; sourceTree = BUILT_PRODUCTS_DIR; };
2832
86719E9924720BD1002A2AB0 /* HttpUtility.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HttpUtility.h; sourceTree = "<group>"; };
2933
86719E9A24720BD1002A2AB0 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -52,6 +56,22 @@
5256
/* End PBXFrameworksBuildPhase section */
5357

5458
/* Begin PBXGroup section */
59+
8656BC562483E3B20023549D /* Extensions */ = {
60+
isa = PBXGroup;
61+
children = (
62+
8656BC572483E3C60023549D /* EncodableExtension.swift */,
63+
);
64+
path = Extensions;
65+
sourceTree = "<group>";
66+
};
67+
8656BC592483E4200023549D /* ExtensionsTests */ = {
68+
isa = PBXGroup;
69+
children = (
70+
8656BC5A2483E43D0023549D /* EncodableExtensionUnitTest.swift */,
71+
);
72+
path = ExtensionsTests;
73+
sourceTree = "<group>";
74+
};
5575
86719E8C24720BD1002A2AB0 = {
5676
isa = PBXGroup;
5777
children = (
@@ -73,6 +93,7 @@
7393
86719E9824720BD1002A2AB0 /* HttpUtility */ = {
7494
isa = PBXGroup;
7595
children = (
96+
8656BC562483E3B20023549D /* Extensions */,
7697
86719E9924720BD1002A2AB0 /* HttpUtility.h */,
7798
86719E9A24720BD1002A2AB0 /* Info.plist */,
7899
86719EB024720E40002A2AB0 /* HttpUtility.swift */,
@@ -83,6 +104,7 @@
83104
86719EA324720BD1002A2AB0 /* HttpUtilityTests */ = {
84105
isa = PBXGroup;
85106
children = (
107+
8656BC592483E4200023549D /* ExtensionsTests */,
86108
86719EA424720BD1002A2AB0 /* HttpUtilityTests.swift */,
87109
86719EA624720BD1002A2AB0 /* Info.plist */,
88110
);
@@ -200,13 +222,15 @@
200222
buildActionMask = 2147483647;
201223
files = (
202224
86719EB124720E40002A2AB0 /* HttpUtility.swift in Sources */,
225+
8656BC582483E3C60023549D /* EncodableExtension.swift in Sources */,
203226
);
204227
runOnlyForDeploymentPostprocessing = 0;
205228
};
206229
86719E9B24720BD1002A2AB0 /* Sources */ = {
207230
isa = PBXSourcesBuildPhase;
208231
buildActionMask = 2147483647;
209232
files = (
233+
8656BC5B2483E43D0023549D /* EncodableExtensionUnitTest.swift in Sources */,
210234
86719EA524720BD1002A2AB0 /* HttpUtilityTests.swift in Sources */,
211235
);
212236
runOnlyForDeploymentPostprocessing = 0;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1150"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "86719E9524720BD1002A2AB0"
18+
BuildableName = "HttpUtility.framework"
19+
BlueprintName = "HttpUtility"
20+
ReferencedContainer = "container:HttpUtility.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
shouldUseLaunchSchemeArgsEnv = "YES"
30+
codeCoverageEnabled = "YES"
31+
onlyGenerateCoverageForSpecifiedTargets = "YES">
32+
<CodeCoverageTargets>
33+
<BuildableReference
34+
BuildableIdentifier = "primary"
35+
BlueprintIdentifier = "86719E9524720BD1002A2AB0"
36+
BuildableName = "HttpUtility.framework"
37+
BlueprintName = "HttpUtility"
38+
ReferencedContainer = "container:HttpUtility.xcodeproj">
39+
</BuildableReference>
40+
</CodeCoverageTargets>
41+
<Testables>
42+
<TestableReference
43+
skipped = "NO">
44+
<BuildableReference
45+
BuildableIdentifier = "primary"
46+
BlueprintIdentifier = "86719E9E24720BD1002A2AB0"
47+
BuildableName = "HttpUtilityTests.xctest"
48+
BlueprintName = "HttpUtilityTests"
49+
ReferencedContainer = "container:HttpUtility.xcodeproj">
50+
</BuildableReference>
51+
</TestableReference>
52+
</Testables>
53+
</TestAction>
54+
<LaunchAction
55+
buildConfiguration = "Debug"
56+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
57+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
58+
launchStyle = "0"
59+
useCustomWorkingDirectory = "NO"
60+
ignoresPersistentStateOnLaunch = "NO"
61+
debugDocumentVersioning = "YES"
62+
debugServiceExtension = "internal"
63+
allowLocationSimulation = "YES">
64+
</LaunchAction>
65+
<ProfileAction
66+
buildConfiguration = "Release"
67+
shouldUseLaunchSchemeArgsEnv = "YES"
68+
savedToolIdentifier = ""
69+
useCustomWorkingDirectory = "NO"
70+
debugDocumentVersioning = "YES">
71+
<MacroExpansion>
72+
<BuildableReference
73+
BuildableIdentifier = "primary"
74+
BlueprintIdentifier = "86719E9524720BD1002A2AB0"
75+
BuildableName = "HttpUtility.framework"
76+
BlueprintName = "HttpUtility"
77+
ReferencedContainer = "container:HttpUtility.xcodeproj">
78+
</BuildableReference>
79+
</MacroExpansion>
80+
</ProfileAction>
81+
<AnalyzeAction
82+
buildConfiguration = "Debug">
83+
</AnalyzeAction>
84+
<ArchiveAction
85+
buildConfiguration = "Release"
86+
revealArchiveInOrganizer = "YES">
87+
</ArchiveAction>
88+
</Scheme>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// EncodableExtension.swift
3+
// HttpUtility
4+
//
5+
// Created by CodeCat15 on 5/31/20.
6+
// Copyright © 2020 CodeCat15. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
extension Encodable
12+
{
13+
func convertToURLQueryItems() -> [URLQueryItem]?
14+
{
15+
do {
16+
let encoder = try JSONEncoder().encode(self)
17+
let requestDictionary = (try? JSONSerialization.jsonObject(with: encoder, options: .allowFragments)).flatMap{$0 as? [String: Any?]}
18+
19+
if(requestDictionary != nil)
20+
{
21+
var queryItems: [URLQueryItem] = []
22+
23+
requestDictionary?.forEach({ (key, value) in
24+
25+
if(value != nil)
26+
{
27+
let strValue = value.map { String(describing: $0) }
28+
if(strValue != nil && strValue?.count != 0)
29+
{
30+
queryItems.append(URLQueryItem(name: key, value: strValue))
31+
}
32+
}
33+
})
34+
35+
return queryItems
36+
}
37+
38+
} catch let error {
39+
debugPrint(error)
40+
}
41+
42+
return nil
43+
}
44+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//
2+
// EncodableExtensionUnitTest.swift
3+
// HttpUtilityTests
4+
//
5+
// Created by CodeCat15 on 5/31/20.
6+
// Copyright © 2020 CodeCat15. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import HttpUtility
11+
12+
class EncodableExtensionUnitTest: XCTestCase {
13+
14+
override func setUpWithError() throws {
15+
// Put setup code here. This method is called before the invocation of each test method in the class.
16+
}
17+
18+
override func tearDownWithError() throws {
19+
// Put teardown code here. This method is called after the invocation of each test method in the class.
20+
}
21+
22+
func test_convertToURLQueryItems_With_SimpleStructure_Returuns_URLQueryItemCollection()
23+
{
24+
// ARRANGE
25+
struct Simple : Encodable {let name, description: String}
26+
let objSimple = Simple(name: UUID().uuidString, description: UUID().uuidString)
27+
28+
// ACT
29+
let result = objSimple.convertToURLQueryItems()
30+
31+
// ASSERT
32+
XCTAssertNotNil(result)
33+
XCTAssertTrue(result?.count == 2)
34+
}
35+
36+
func test_convertToURLQueryItems_With_IntegerValue_Returuns_URLQueryItemCollection()
37+
{
38+
// ARRANGE
39+
struct simple : Encodable {
40+
let id: Int
41+
let name: String
42+
43+
}
44+
let objSimple = simple(id: 1, name: UUID().uuidString)
45+
46+
// ACT
47+
let result = objSimple.convertToURLQueryItems()
48+
49+
// ASSERT
50+
XCTAssertNotNil(result)
51+
XCTAssertTrue(result?.count == 2)
52+
}
53+
54+
func test_convertToURLQueryItems_With_array_Returuns_URLQueryItemCollection()
55+
{
56+
// ARRANGE
57+
struct simple : Encodable {
58+
let id: [Int]
59+
let name: String
60+
61+
}
62+
let objSimple = simple(id: [1,2,3], name: UUID().uuidString)
63+
64+
// ACT
65+
let result = objSimple.convertToURLQueryItems()
66+
67+
// ASSERT
68+
XCTAssertNotNil(result)
69+
XCTAssertTrue(result?.count == 2)
70+
}
71+
72+
func test_convertToURLQueryItems_With_Multiple_DataType_Returuns_URLQueryItemCollection()
73+
{
74+
// ARRANGE
75+
struct simple : Encodable {
76+
let id: Int
77+
let name: String
78+
let salary: Double
79+
let isOnContract: Bool
80+
}
81+
82+
let objSimple = simple(id: 1, name: "codecat15", salary: 25000.0, isOnContract: false)
83+
var components = URLComponents(string: "https://www.example.com")
84+
// let expectedQueryString = "https://www.example.com?id=1&name=codecat15&salary=25000&isOnContract=0"
85+
86+
// ACT
87+
let result = objSimple.convertToURLQueryItems()
88+
components?.queryItems = result
89+
90+
// ASSERT
91+
XCTAssertNotNil(result)
92+
XCTAssertTrue(result?.count == 4)
93+
}
94+
95+
96+
// func testPerformanceExample() throws {
97+
// // This is an example of a performance test case.
98+
// self.measure {
99+
// // Put the code you want to measure the time of here.
100+
// }
101+
// }
102+
103+
}

0 commit comments

Comments
 (0)