Skip to content

Commit a72a9b8

Browse files
committed
Add posibility to specified custom headers
1 parent a1b038e commit a72a9b8

File tree

5 files changed

+97
-6
lines changed

5 files changed

+97
-6
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1010

1111
### Removed
1212

13+
## [0.1.7](https://github.com/openium/SwiftiumTestingKit/compare/latest...HEAD)
14+
### Added
15+
Allow pilotable server to specify responses headers for all requests and responses headers for each request
16+
17+
### Changed
18+
19+
### Removed
20+
1321
## [0.1.6](https://github.com/openium/SwiftiumTestingKit/compare/latest...HEAD)
1422
### Added
1523

STKTestAppTests/STKPilotableHTTPServerTests.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,57 @@ class STKPilotableHTTPServerTests: XCTestCase {
195195
XCTAssertTrue(sut.hasServedAllQueuedResponses)
196196
}
197197

198+
func testResponseFileHasSpecifiedHeadersResponses_shouldReturnWithSpecifiedHeaders() {
199+
// Given
200+
let headerContentType = "Content-Type"
201+
let headerContentTypeValue = "application/vnd.openium.v1+json"
202+
let headerCacheControl = "Cache-Control"
203+
let headerCacheControlValue = "no-cache"
204+
let customResponseHeaders = [headerContentType: headerContentTypeValue]
205+
sut.defaultResponseHeaders = [headerCacheControl: headerCacheControlValue]
206+
207+
// When
208+
209+
let url = sut.makeRequest(onPath: "/hello.json", serveContentOfFileAtPath: "hello.json", customResponseHeaders: customResponseHeaders)
210+
let expectation = self.expectation(description: "file hello.json must be served with custom headers content type")
211+
let downloadTask = URLSession.shared.dataTask(with: url) { (data, urlResponse, error) in
212+
if let httpResponse = urlResponse as? HTTPURLResponse,
213+
(httpResponse.allHeaderFields[headerContentType] as? String) == headerContentTypeValue,
214+
(httpResponse.allHeaderFields[headerCacheControl] as? String) == headerCacheControlValue {
215+
expectation.fulfill()
216+
}
217+
}
218+
downloadTask.resume()
219+
220+
// Expect
221+
self.waitForExpectations(timeout: 2.0, handler: nil)
222+
}
223+
224+
func testResponseDataHasSpecifiedHeadersResponses_shouldReturnWithSpecifiedHeaders() {
225+
// Given
226+
let jsonData = dataFromCurrentClassBundleRessource(filename: "hello.json")
227+
let headerContentType = "Content-Type"
228+
let headerContentTypeValue = "application/vnd.openium.v1+json"
229+
let headerCacheControl = "Cache-Control"
230+
let headerCacheControlValue = "no-cache"
231+
let customResponseHeaders = [headerContentType: headerContentTypeValue]
232+
sut.defaultResponseHeaders = [headerCacheControl: headerCacheControlValue]
233+
234+
// When
235+
let url = sut.makeRequest(onPath: "/hello.json", data: jsonData, customResponseHeaders: customResponseHeaders)
236+
let expectation = self.expectation(description: "file hello.json must be served with custom headers content type")
237+
let downloadTask = URLSession.shared.dataTask(with: url) { (data, urlResponse, error) in
238+
if let httpResponse = urlResponse as? HTTPURLResponse,
239+
(httpResponse.allHeaderFields[headerContentType] as? String) == headerContentTypeValue,
240+
(httpResponse.allHeaderFields[headerCacheControl] as? String) == headerCacheControlValue {
241+
expectation.fulfill()
242+
}
243+
}
244+
downloadTask.resume()
245+
246+
// Expect
247+
self.waitForExpectations(timeout: 2.0, handler: nil)
248+
}
198249
}
199250

200251
extension STKPilotableHTTPServerTests: URLSessionTaskDelegate {

SwiftiumTestingKit.xcodeproj/project.pbxproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636
/* End PBXBuildFile section */
3737

3838
/* Begin PBXContainerItemProxy section */
39+
33EB268621AE9CC800945E63 /* PBXContainerItemProxy */ = {
40+
isa = PBXContainerItemProxy;
41+
containerPortal = BA6442502152465300E847EC /* Project object */;
42+
proxyType = 1;
43+
remoteGlobalIDString = BA64426E215246CC00E847EC;
44+
remoteInfo = STKTestApp;
45+
};
3946
F9C485292179C70B00A3A25D /* PBXContainerItemProxy */ = {
4047
isa = PBXContainerItemProxy;
4148
containerPortal = BA6442502152465300E847EC /* Project object */;
@@ -270,6 +277,7 @@
270277
buildRules = (
271278
);
272279
dependencies = (
280+
33EB268721AE9CC800945E63 /* PBXTargetDependency */,
273281
);
274282
name = STKTestAppTests;
275283
productName = STKTestAppTests;
@@ -382,6 +390,11 @@
382390
/* End PBXSourcesBuildPhase section */
383391

384392
/* Begin PBXTargetDependency section */
393+
33EB268721AE9CC800945E63 /* PBXTargetDependency */ = {
394+
isa = PBXTargetDependency;
395+
target = BA64426E215246CC00E847EC /* STKTestApp */;
396+
targetProxy = 33EB268621AE9CC800945E63 /* PBXContainerItemProxy */;
397+
};
385398
F9C4852A2179C70B00A3A25D /* PBXTargetDependency */ = {
386399
isa = PBXTargetDependency;
387400
target = BA6442582152465300E847EC /* SwiftiumTestingKit */;

SwiftiumTestingKit/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>0.1.6</string>
18+
<string>0.1.7</string>
1919
<key>CFBundleVersion</key>
2020
<string>$(CURRENT_PROJECT_VERSION)</string>
2121
</dict>

SwiftiumTestingKit/STKPilotableHTTPServer.swift

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public class STKPilotableHTTPServer: NSObject {
9696
let scheme: String
9797
let host: String
9898
let documentRoot: String
99-
var defaultResponseHeaders: [String: String]?
99+
public var defaultResponseHeaders: [String: String]?
100100

101101
public init(scheme: String, host: String, documentRoot: String) {
102102
self.scheme = scheme
@@ -157,9 +157,16 @@ public class STKPilotableHTTPServer: NSObject {
157157
data: Data?,
158158
httpVerb: HTTPVerb = .get,
159159
statusCode: Int32 = 200,
160-
serveForever: Bool = false) -> URL {
160+
serveForever: Bool = false,
161+
customResponseHeaders: [String: String]? = nil) -> URL {
161162
let descriptor = stub(condition: isScheme(scheme) && isHost(host) && isPath(path) && isMethod(httpVerb)) { _ in
162-
return OHHTTPStubsResponse(data: data ?? Data(), statusCode: statusCode, headers: nil)
163+
var headers = self.defaultResponseHeaders ?? [String: String]()
164+
if let customResponseHeaders = customResponseHeaders {
165+
headers.merge(customResponseHeaders) { (left, right) -> String in
166+
return right
167+
}
168+
}
169+
return OHHTTPStubsResponse(data: data ?? Data(), statusCode: statusCode, headers: headers)
163170
}
164171
queue(descriptor: descriptor, serveForever: serveForever)
165172
return urlForRequest(onPath: path)
@@ -170,9 +177,21 @@ public class STKPilotableHTTPServer: NSObject {
170177
serveContentOfFileAtPath fileAtPath: String,
171178
httpVerb: HTTPVerb = .get,
172179
statusCode: Int32 = 200,
173-
serveForever: Bool = false) -> URL {
180+
serveForever: Bool = false,
181+
customResponseHeaders: [String: String]? = nil) -> URL {
174182
let stubPath = self.pathFromDocumentRoot(of: fileAtPath)
175-
let headers = self.headersWithMimeTypeOfFile(at: fileAtPath)
183+
var headers = [String: String]()
184+
if let headersWithMimeType = self.headersWithMimeTypeOfFile(at: fileAtPath) {
185+
headers.merge(headersWithMimeType) { (left, right) -> String in
186+
return right
187+
}
188+
}
189+
if let customResponseHeaders = customResponseHeaders {
190+
headers.merge(customResponseHeaders) { (left, right) -> String in
191+
return right
192+
}
193+
}
194+
176195
let descriptor = stub(condition: isScheme(scheme) && isHost(host) && isPath(path) && isMethod(httpVerb)) { _ in
177196
return OHHTTPStubsResponse(fileAtPath: stubPath,
178197
statusCode: statusCode,

0 commit comments

Comments
 (0)