diff --git a/CHANGELOG.md b/CHANGELOG.md index 78fcd14..5b3038e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/STKTestAppTests/STKPilotableHTTPServerTests.swift b/STKTestAppTests/STKPilotableHTTPServerTests.swift index 84f9114..4d46aaa 100644 --- a/STKTestAppTests/STKPilotableHTTPServerTests.swift +++ b/STKTestAppTests/STKPilotableHTTPServerTests.swift @@ -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 { diff --git a/SwiftiumTestingKit.xcodeproj/project.pbxproj b/SwiftiumTestingKit.xcodeproj/project.pbxproj index f47b646..3579745 100644 --- a/SwiftiumTestingKit.xcodeproj/project.pbxproj +++ b/SwiftiumTestingKit.xcodeproj/project.pbxproj @@ -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 */; @@ -270,6 +277,7 @@ buildRules = ( ); dependencies = ( + 33EB268721AE9CC800945E63 /* PBXTargetDependency */, ); name = STKTestAppTests; productName = STKTestAppTests; @@ -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 */; diff --git a/SwiftiumTestingKit/Info.plist b/SwiftiumTestingKit/Info.plist index c7dc30b..a763074 100644 --- a/SwiftiumTestingKit/Info.plist +++ b/SwiftiumTestingKit/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.1.6 + 0.1.7 CFBundleVersion $(CURRENT_PROJECT_VERSION) diff --git a/SwiftiumTestingKit/STKPilotableHTTPServer.swift b/SwiftiumTestingKit/STKPilotableHTTPServer.swift index 7503969..07efaec 100644 --- a/SwiftiumTestingKit/STKPilotableHTTPServer.swift +++ b/SwiftiumTestingKit/STKPilotableHTTPServer.swift @@ -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 @@ -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) @@ -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,