Skip to content

Commit

Permalink
Add posibility to specified custom headers
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaut-schmitt-enki committed Nov 28, 2018
1 parent a1b038e commit a72a9b8
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 6 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Removed

## [0.1.7](https://github.com/openium/SwiftiumTestingKit/compare/latest...HEAD)
### Added
Allow pilotable server to specify responses headers for all requests and responses headers for each request

### Changed

### Removed

## [0.1.6](https://github.com/openium/SwiftiumTestingKit/compare/latest...HEAD)
### Added

Expand Down
51 changes: 51 additions & 0 deletions STKTestAppTests/STKPilotableHTTPServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,57 @@ class STKPilotableHTTPServerTests: XCTestCase {
XCTAssertTrue(sut.hasServedAllQueuedResponses)
}

func testResponseFileHasSpecifiedHeadersResponses_shouldReturnWithSpecifiedHeaders() {
// Given
let headerContentType = "Content-Type"
let headerContentTypeValue = "application/vnd.openium.v1+json"
let headerCacheControl = "Cache-Control"
let headerCacheControlValue = "no-cache"
let customResponseHeaders = [headerContentType: headerContentTypeValue]
sut.defaultResponseHeaders = [headerCacheControl: headerCacheControlValue]

// When

let url = sut.makeRequest(onPath: "/hello.json", serveContentOfFileAtPath: "hello.json", customResponseHeaders: customResponseHeaders)
let expectation = self.expectation(description: "file hello.json must be served with custom headers content type")
let downloadTask = URLSession.shared.dataTask(with: url) { (data, urlResponse, error) in
if let httpResponse = urlResponse as? HTTPURLResponse,
(httpResponse.allHeaderFields[headerContentType] as? String) == headerContentTypeValue,
(httpResponse.allHeaderFields[headerCacheControl] as? String) == headerCacheControlValue {
expectation.fulfill()
}
}
downloadTask.resume()

// Expect
self.waitForExpectations(timeout: 2.0, handler: nil)
}

func testResponseDataHasSpecifiedHeadersResponses_shouldReturnWithSpecifiedHeaders() {
// Given
let jsonData = dataFromCurrentClassBundleRessource(filename: "hello.json")
let headerContentType = "Content-Type"
let headerContentTypeValue = "application/vnd.openium.v1+json"
let headerCacheControl = "Cache-Control"
let headerCacheControlValue = "no-cache"
let customResponseHeaders = [headerContentType: headerContentTypeValue]
sut.defaultResponseHeaders = [headerCacheControl: headerCacheControlValue]

// When
let url = sut.makeRequest(onPath: "/hello.json", data: jsonData, customResponseHeaders: customResponseHeaders)
let expectation = self.expectation(description: "file hello.json must be served with custom headers content type")
let downloadTask = URLSession.shared.dataTask(with: url) { (data, urlResponse, error) in
if let httpResponse = urlResponse as? HTTPURLResponse,
(httpResponse.allHeaderFields[headerContentType] as? String) == headerContentTypeValue,
(httpResponse.allHeaderFields[headerCacheControl] as? String) == headerCacheControlValue {
expectation.fulfill()
}
}
downloadTask.resume()

// Expect
self.waitForExpectations(timeout: 2.0, handler: nil)
}
}

extension STKPilotableHTTPServerTests: URLSessionTaskDelegate {
Expand Down
13 changes: 13 additions & 0 deletions SwiftiumTestingKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
33EB268621AE9CC800945E63 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = BA6442502152465300E847EC /* Project object */;
proxyType = 1;
remoteGlobalIDString = BA64426E215246CC00E847EC;
remoteInfo = STKTestApp;
};
F9C485292179C70B00A3A25D /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = BA6442502152465300E847EC /* Project object */;
Expand Down Expand Up @@ -270,6 +277,7 @@
buildRules = (
);
dependencies = (
33EB268721AE9CC800945E63 /* PBXTargetDependency */,
);
name = STKTestAppTests;
productName = STKTestAppTests;
Expand Down Expand Up @@ -382,6 +390,11 @@
/* End PBXSourcesBuildPhase section */

/* Begin PBXTargetDependency section */
33EB268721AE9CC800945E63 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = BA64426E215246CC00E847EC /* STKTestApp */;
targetProxy = 33EB268621AE9CC800945E63 /* PBXContainerItemProxy */;
};
F9C4852A2179C70B00A3A25D /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = BA6442582152465300E847EC /* SwiftiumTestingKit */;
Expand Down
2 changes: 1 addition & 1 deletion SwiftiumTestingKit/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.1.6</string>
<string>0.1.7</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
</dict>
Expand Down
29 changes: 24 additions & 5 deletions SwiftiumTestingKit/STKPilotableHTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class STKPilotableHTTPServer: NSObject {
let scheme: String
let host: String
let documentRoot: String
var defaultResponseHeaders: [String: String]?
public var defaultResponseHeaders: [String: String]?

public init(scheme: String, host: String, documentRoot: String) {
self.scheme = scheme
Expand Down Expand Up @@ -157,9 +157,16 @@ public class STKPilotableHTTPServer: NSObject {
data: Data?,
httpVerb: HTTPVerb = .get,
statusCode: Int32 = 200,
serveForever: Bool = false) -> URL {
serveForever: Bool = false,
customResponseHeaders: [String: String]? = nil) -> URL {
let descriptor = stub(condition: isScheme(scheme) && isHost(host) && isPath(path) && isMethod(httpVerb)) { _ in
return OHHTTPStubsResponse(data: data ?? Data(), statusCode: statusCode, headers: nil)
var headers = self.defaultResponseHeaders ?? [String: String]()
if let customResponseHeaders = customResponseHeaders {
headers.merge(customResponseHeaders) { (left, right) -> String in
return right
}
}
return OHHTTPStubsResponse(data: data ?? Data(), statusCode: statusCode, headers: headers)
}
queue(descriptor: descriptor, serveForever: serveForever)
return urlForRequest(onPath: path)
Expand All @@ -170,9 +177,21 @@ public class STKPilotableHTTPServer: NSObject {
serveContentOfFileAtPath fileAtPath: String,
httpVerb: HTTPVerb = .get,
statusCode: Int32 = 200,
serveForever: Bool = false) -> URL {
serveForever: Bool = false,
customResponseHeaders: [String: String]? = nil) -> URL {
let stubPath = self.pathFromDocumentRoot(of: fileAtPath)
let headers = self.headersWithMimeTypeOfFile(at: fileAtPath)
var headers = [String: String]()
if let headersWithMimeType = self.headersWithMimeTypeOfFile(at: fileAtPath) {
headers.merge(headersWithMimeType) { (left, right) -> String in
return right
}
}
if let customResponseHeaders = customResponseHeaders {
headers.merge(customResponseHeaders) { (left, right) -> String in
return right
}
}

let descriptor = stub(condition: isScheme(scheme) && isHost(host) && isPath(path) && isMethod(httpVerb)) { _ in
return OHHTTPStubsResponse(fileAtPath: stubPath,
statusCode: statusCode,
Expand Down

0 comments on commit a72a9b8

Please sign in to comment.