Skip to content

Commit

Permalink
ADD support for injecting headerFields into Request
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihail Nacev authored and mihailnacev committed Oct 31, 2023
1 parent 22935a9 commit 3e2bc79
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
4 changes: 2 additions & 2 deletions GirdersSwift.podspec
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Pod::Spec.new do |s|
s.name = 'GirdersSwift'
s.version = '0.7.1'
s.version = '0.7.2'
s.summary = 'Girders for iOS, written in Swift.'
s.homepage = 'https://www.netcetera.com'
s.author = 'Netcetera'
s.description = 'A library that reduces development time for iOS Swift applications.'
s.platform = :ios, '11.0'
s.source = { :git => 'https://github.com/netceteragroup/GirdersSwift.git', :tag => '0.7.1' }
s.source = { :git => 'https://github.com/netceteragroup/GirdersSwift.git', :tag => '0.7.2' }
s.requires_arc = true
s.swift_version = "5.0"
s.module_name = 'GirdersSwift'
Expand Down
4 changes: 3 additions & 1 deletion Sources/GirdersSwift/http/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ public struct Request: Equatable {
method: HTTPMethod,
parameters: [String: Any],
queryParameters: [String: Any] = [:],
additionalHeaders: [String: String] = [:],
requestGenerator: RequestGenerator) {
var mutableRequest = requestGenerator.generateRequest(withMethod: method)
mutableRequest.updateParameters(parameters: parameters)
mutableRequest.updateQueryParameters(parameters: queryParameters)

mutableRequest.updateHTTPHeaderFields(headerFields: additionalHeaders)

if method == .GET && mutableRequest.queryString != nil {
if let queryString = URL.appendQueryString(queryString: mutableRequest.queryString!) {
self.url = queryString
Expand Down
38 changes: 38 additions & 0 deletions Tests/GirdersSwiftTests/swift/http/TestRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,44 @@ class TestRequest: XCTestCase {
XCTAssertNotNil(request)
}

func testRequestWithAdditionalHeaders() {
// Given
let url = URL(string: "https://www.example.com/api")!
let method: HTTPMethod = .GET
let parameters: [String : Any] = [:]
let additionalHeaders: [String: String] = ["User-Agent": "MyApp/1.0"]

// When
let request = Request(URL: url,
method: method,
parameters: parameters,
additionalHeaders: additionalHeaders,
requestGenerator: mockGenerator)
let sortedHeaderFields = request.headerFields.sorted { $0.key < $1.key }
let sortedDictionary = Dictionary(uniqueKeysWithValues: sortedHeaderFields)

// Then
XCTAssertEqual(sortedDictionary, ["Accept": "application/json", "User-Agent": "MyApp/1.0"])
}

func testRequestWithoutAdditionalHeaders() {
// Given
let url = URL(string: "https://www.example.com/api")!
let method: HTTPMethod = .GET
let parameters: [String : Any] = [:]

// When
let request = Request(URL: url,
method: method,
parameters: parameters,
requestGenerator: mockGenerator)
let sortedHeaderFields = request.headerFields.sorted { $0.key < $1.key }
let sortedDictionary = Dictionary(uniqueKeysWithValues: sortedHeaderFields)

// Then
XCTAssertEqual(sortedDictionary, ["Accept": "application/json"])
}

/**
Testing == operator, when Requests are equal
*/
Expand Down

0 comments on commit 3e2bc79

Please sign in to comment.