Skip to content

Commit

Permalink
Merge pull request #112 from vadymmarkov/fix/middleware-failure
Browse files Browse the repository at this point in the history
Fix promise not being rejected when middleware fails
  • Loading branch information
vadymmarkov authored Nov 8, 2018
2 parents 039b791 + e1e8372 commit b2afba1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Malibu.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@
DA5B4CEE1C8E5553004E21ED /* StringSerializer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringSerializer.swift; sourceTree = "<group>"; };
DA5B4CF71C8E5936004E21ED /* NetworkError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkError.swift; sourceTree = "<group>"; };
DA5B4CFA1C8E5953004E21ED /* NetworkErrorSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkErrorSpec.swift; sourceTree = "<group>"; };
DA5B4D031C8E59A2004E21ED /* Networking.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Networking.swift; sourceTree = "<group>"; };
DA5B4D031C8E59A2004E21ED /* Networking.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = Networking.swift; sourceTree = "<group>"; tabWidth = 2; };
DA5B4D0E1C8E5A07004E21ED /* NetworkingSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkingSpec.swift; sourceTree = "<group>"; };
DAA50FF01C97738400D01F78 /* Header.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Header.swift; sourceTree = "<group>"; };
DAA50FF31C9786BE00D01F78 /* HeaderSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HeaderSpec.swift; sourceTree = "<group>"; };
Expand Down
7 changes: 0 additions & 7 deletions MalibuTests/Specs/NetworkingSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ final private class ChallengeSenderMock: NSObject, URLAuthenticationChallengeSen
final class NetworkingSpec: QuickSpec {
override func spec() {
describe("Networking") {
describe("#init") {
it("sets default configuration to the session") {
let networking = Networking<TestService>()
expect(networking.session.configuration).to(equal(SessionConfiguration.default.value))
}
}

describe("#request") {
context("when request has a mock") {
it("returns a mock data") {
Expand Down
30 changes: 0 additions & 30 deletions MalibuTests/Specs/Request/ContentTypeSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ final class ContentTypeSpec: QuickSpec {
}
}

describe("#hashValue") {
it("returns a hash value") {
expect(contentType.hashValue).to(equal("query".hashValue))
}
}

describe("#equal") {
it("compares for value equality") {
expect(contentType).toNot(equal(ContentType.json))
Expand Down Expand Up @@ -66,12 +60,6 @@ final class ContentTypeSpec: QuickSpec {
}
}

describe("#hashValue") {
it("returns a hash value of corresponding string value") {
expect(contentType.hashValue).to(equal(contentType.header?.hashValue))
}
}

describe("#equal") {
it("compares for value equality") {
expect(contentType).toNot(equal(ContentType.json))
Expand Down Expand Up @@ -103,12 +91,6 @@ final class ContentTypeSpec: QuickSpec {
}
}

describe("#hashValue") {
it("returns a hash value of corresponding string value") {
expect(contentType.hashValue).to(equal(contentType.header?.hashValue))
}
}

describe("#equal") {
it("compares for value equality") {
expect(contentType).to(equal(ContentType.json))
Expand Down Expand Up @@ -140,12 +122,6 @@ final class ContentTypeSpec: QuickSpec {
}
}

describe("#hashValue") {
it("returns a hash value of corresponding string value") {
expect(contentType.hashValue).to(equal(contentType.header?.hashValue))
}
}

describe("#equal") {
it("compares for value equality") {
expect(contentType).to(equal(ContentType.multipartFormData))
Expand Down Expand Up @@ -178,12 +154,6 @@ final class ContentTypeSpec: QuickSpec {
}
}

describe("#hashValue") {
it("returns a hash value of corresponding string value") {
expect(contentType.hashValue).to(equal(contentType.header?.hashValue))
}
}

describe("#equal") {
it("compares for value equality") {
expect(contentType).toNot(equal(ContentType.json))
Expand Down
61 changes: 33 additions & 28 deletions Sources/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,35 +152,40 @@ extension Networking {
let middlewarePromise = Promise<Void>()
let networkPromise = NetworkPromise()

middlewarePromise.done({ [weak self] in
guard let `self` = self else {
return
}

let operation = self.createOperation(request: request, mockBehavior: mockBehavior)
let responseHandler = ResponseHandler(networkPromise: networkPromise)
operation.handleResponse = responseHandler.handle(urlRequest:data:urlResponse:error:)

networkPromise
.done({ response in
if logger.enabled {
logger.requestLogger.init(level: logger.level).log(
request: request,
urlRequest: response.urlRequest
)
logger.responseLogger.init(level: logger.level).log(response: response.httpUrlResponse)
}
})
.fail(policy: .allErrors, { [weak self] error in
if case PromiseError.cancelled = error {
operation.cancel()
operation.finish()
}
self?.handle(error: error, on: request)
})
middlewarePromise
.done({ [weak self] in
guard let `self` = self else {
return
}

self.queue.addOperation(operation)
})
let operation = self.createOperation(request: request, mockBehavior: mockBehavior)
let responseHandler = ResponseHandler(networkPromise: networkPromise)
operation.handleResponse = responseHandler.handle(urlRequest:data:urlResponse:error:)

networkPromise
.done({ response in
if logger.enabled {
logger.requestLogger.init(level: logger.level).log(
request: request,
urlRequest: response.urlRequest
)
logger.responseLogger.init(level: logger.level).log(response: response.httpUrlResponse)
}
})
.fail(policy: .allErrors, { [weak self] error in
if case PromiseError.cancelled = error {
operation.cancel()
operation.finish()
}
self?.handle(error: error, on: request)
})

self.queue.addOperation(operation)
})
.fail(policy: .allErrors, { [weak self] error in
networkPromise.reject(error)
self?.handle(error: error, on: request)
})

middleware(middlewarePromise)
return networkPromise
Expand Down

0 comments on commit b2afba1

Please sign in to comment.