From 7efaf4f9e940003082a009feeed9b8741a791557 Mon Sep 17 00:00:00 2001 From: Luciano Date: Mon, 12 Mar 2018 17:17:26 -0300 Subject: [PATCH 1/7] Adding convenience Range filter. --- Sources/Moya/Response.swift | 16 +++++++++++++++- Sources/RxMoya/Single+Response.swift | 9 ++++++++- Tests/Single+MoyaSpec.swift | 19 ++++++++++++++++++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/Sources/Moya/Response.swift b/Sources/Moya/Response.swift index ee0b259b9..9d09c436f 100644 --- a/Sources/Moya/Response.swift +++ b/Sources/Moya/Response.swift @@ -42,7 +42,7 @@ public final class Response: CustomDebugStringConvertible, Equatable { public extension Response { /** - Returns the `Response` if the `statusCode` falls within the specified range. + Returns the `Response` if the `statusCode` falls within the specified closed range. - parameters: - statusCodes: The range of acceptable status codes. @@ -54,6 +54,20 @@ public extension Response { } return self } + + /** + Returns the `Response` if the `statusCode` falls within the specified range. + + - parameters: + - statusCodes: The range of acceptable status codes. + - throws: `MoyaError.statusCode` when others are encountered. + */ + public func filter(statusCodes: Range) throws -> Response { + guard statusCodes.contains(statusCode) else { + throw MoyaError.statusCode(self) + } + return self + } /** Returns the `Response` if it has the specified `statusCode`. diff --git a/Sources/RxMoya/Single+Response.swift b/Sources/RxMoya/Single+Response.swift index d3c732517..701217118 100644 --- a/Sources/RxMoya/Single+Response.swift +++ b/Sources/RxMoya/Single+Response.swift @@ -7,12 +7,19 @@ import RxSwift /// Extension for processing raw NSData generated by network access. extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Response { - /// Filters out responses that don't fall within the given range, generating errors when others are encountered. + /// Filters out responses that don't fall within the given closed range, generating errors when others are encountered. public func filter(statusCodes: ClosedRange) -> Single { return flatMap { response -> Single in return Single.just(try response.filter(statusCodes: statusCodes)) } } + + /// Filters out responses that don't fall within the given range, generating errors when others are encountered. + public func filter(statusCodes: Range) -> Single { + return flatMap { response -> Single in + return Single.just(try response.filter(statusCodes: statusCodes)) + } + } public func filter(statusCode: Int) -> Single { return flatMap { response -> Single in diff --git a/Tests/Single+MoyaSpec.swift b/Tests/Single+MoyaSpec.swift index e75c7e0cb..f99b1be4e 100644 --- a/Tests/Single+MoyaSpec.swift +++ b/Tests/Single+MoyaSpec.swift @@ -6,7 +6,7 @@ import Nimble final class SingleMoyaSpec: QuickSpec { override func spec() { describe("status codes filtering") { - it("filters out unrequested status codes") { + it("filters out unrequested status codes closed range") { let data = Data() let single = Response(statusCode: 10, data: data).asSingle() @@ -22,6 +22,23 @@ final class SingleMoyaSpec: QuickSpec { expect(errored).to(beTruthy()) } + + it("filters out unrequested status codes with range") { + let data = Data() + let single = Response(statusCode: 10, data: data).asSingle() + + var errored = false + _ = single.filter(statusCodes: 0..<10).subscribe { event in + switch event { + case .success(let object): + fail("called on non-correct status code: \(object)") + case .error: + errored = true + } + } + + expect(errored).to(beTruthy()) + } it("filters out non-successful status codes") { let data = Data() From 008403fc08301b662b7d4c05eaaff7999cc06429 Mon Sep 17 00:00:00 2001 From: Luciano Date: Mon, 12 Mar 2018 17:46:27 -0300 Subject: [PATCH 2/7] Fixing swift lint warnings. --- Sources/Moya/Response.swift | 2 +- Sources/RxMoya/Single+Response.swift | 2 +- Tests/Single+MoyaSpec.swift | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Sources/Moya/Response.swift b/Sources/Moya/Response.swift index 9d09c436f..bc9674d22 100644 --- a/Sources/Moya/Response.swift +++ b/Sources/Moya/Response.swift @@ -54,7 +54,7 @@ public extension Response { } return self } - + /** Returns the `Response` if the `statusCode` falls within the specified range. diff --git a/Sources/RxMoya/Single+Response.swift b/Sources/RxMoya/Single+Response.swift index 701217118..81fcdb32b 100644 --- a/Sources/RxMoya/Single+Response.swift +++ b/Sources/RxMoya/Single+Response.swift @@ -13,7 +13,7 @@ extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Respo return Single.just(try response.filter(statusCodes: statusCodes)) } } - + /// Filters out responses that don't fall within the given range, generating errors when others are encountered. public func filter(statusCodes: Range) -> Single { return flatMap { response -> Single in diff --git a/Tests/Single+MoyaSpec.swift b/Tests/Single+MoyaSpec.swift index f99b1be4e..bc3188a33 100644 --- a/Tests/Single+MoyaSpec.swift +++ b/Tests/Single+MoyaSpec.swift @@ -22,11 +22,11 @@ final class SingleMoyaSpec: QuickSpec { expect(errored).to(beTruthy()) } - + it("filters out unrequested status codes with range") { let data = Data() let single = Response(statusCode: 10, data: data).asSingle() - + var errored = false _ = single.filter(statusCodes: 0..<10).subscribe { event in switch event { @@ -36,7 +36,7 @@ final class SingleMoyaSpec: QuickSpec { errored = true } } - + expect(errored).to(beTruthy()) } From 51e33fb53c20988a7563856166d428899b862877 Mon Sep 17 00:00:00 2001 From: Luciano Date: Mon, 12 Mar 2018 18:12:36 -0300 Subject: [PATCH 3/7] Adding Observable and ReactiveSwift options. Forwarding the calls to Countable range implementations. --- Sources/Moya/Response.swift | 5 +---- .../SignalProducer+Response.swift | 5 +++++ Sources/RxMoya/Observable+Response.swift | 5 +++++ Sources/RxMoya/Single+Response.swift | 4 +--- Tests/Observable+MoyaSpec.swift | 19 +++++++++++++++++++ Tests/SignalProducer+MoyaSpec.swift | 17 +++++++++++++++++ 6 files changed, 48 insertions(+), 7 deletions(-) diff --git a/Sources/Moya/Response.swift b/Sources/Moya/Response.swift index bc9674d22..81d9ff28a 100644 --- a/Sources/Moya/Response.swift +++ b/Sources/Moya/Response.swift @@ -63,10 +63,7 @@ public extension Response { - throws: `MoyaError.statusCode` when others are encountered. */ public func filter(statusCodes: Range) throws -> Response { - guard statusCodes.contains(statusCode) else { - throw MoyaError.statusCode(self) - } - return self + return try filter(statusCodes: statusCodes.lowerBound...statusCodes.upperBound-1) } /** diff --git a/Sources/ReactiveMoya/SignalProducer+Response.swift b/Sources/ReactiveMoya/SignalProducer+Response.swift index 2fbc7633d..876af8867 100644 --- a/Sources/ReactiveMoya/SignalProducer+Response.swift +++ b/Sources/ReactiveMoya/SignalProducer+Response.swift @@ -14,6 +14,11 @@ extension SignalProducerProtocol where Value == Response, Error == MoyaError { } } + /// Filters out responses that don't fall within the given range, generating errors when others are encountered. + public func filter(statusCodes: Range) -> SignalProducer { + return filter(statusCodes: statusCodes.lowerBound...statusCodes.upperBound-1) + } + public func filter(statusCode: Int) -> SignalProducer { return producer.flatMap(.latest) { response -> SignalProducer in return unwrapThrowable { try response.filter(statusCode: statusCode) } diff --git a/Sources/RxMoya/Observable+Response.swift b/Sources/RxMoya/Observable+Response.swift index b8608147e..6f5c57921 100644 --- a/Sources/RxMoya/Observable+Response.swift +++ b/Sources/RxMoya/Observable+Response.swift @@ -14,6 +14,11 @@ extension ObservableType where E == Response { } } + /// Filters out responses that don't fall within the given range, generating errors when others are encountered. + public func filter(statusCodes: Range) -> Observable { + return filter(statusCodes: statusCodes.lowerBound...statusCodes.upperBound-1) + } + public func filter(statusCode: Int) -> Observable { return flatMap { response -> Observable in return Observable.just(try response.filter(statusCode: statusCode)) diff --git a/Sources/RxMoya/Single+Response.swift b/Sources/RxMoya/Single+Response.swift index 81fcdb32b..fc2bc3073 100644 --- a/Sources/RxMoya/Single+Response.swift +++ b/Sources/RxMoya/Single+Response.swift @@ -16,9 +16,7 @@ extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Respo /// Filters out responses that don't fall within the given range, generating errors when others are encountered. public func filter(statusCodes: Range) -> Single { - return flatMap { response -> Single in - return Single.just(try response.filter(statusCodes: statusCodes)) - } + return filter(statusCodes: statusCodes.lowerBound...statusCodes.upperBound-1) } public func filter(statusCode: Int) -> Single { diff --git a/Tests/Observable+MoyaSpec.swift b/Tests/Observable+MoyaSpec.swift index 49f434b92..17694628e 100644 --- a/Tests/Observable+MoyaSpec.swift +++ b/Tests/Observable+MoyaSpec.swift @@ -25,6 +25,25 @@ final class ObservableMoyaSpec: QuickSpec { expect(errored).to(beTruthy()) } + it("filters out unrequested status codes") { + let data = Data() + let observable = Response(statusCode: 10, data: data).asObservable() + + var errored = false + _ = observable.filter(statusCodes: 0..<10).subscribe { event in + switch event { + case .next(let object): + fail("called on non-correct status code: \(object)") + case .error: + errored = true + default: + break + } + } + + expect(errored).to(beTruthy()) + } + it("filters out non-successful status codes") { let data = Data() let observable = Response(statusCode: 404, data: data).asObservable() diff --git a/Tests/SignalProducer+MoyaSpec.swift b/Tests/SignalProducer+MoyaSpec.swift index 479c36adc..be0bc4236 100644 --- a/Tests/SignalProducer+MoyaSpec.swift +++ b/Tests/SignalProducer+MoyaSpec.swift @@ -27,6 +27,23 @@ final class SignalProducerMoyaSpec: QuickSpec { expect(errored).to(beTruthy()) } + it("filters out unrequested status codes range") { + let data = Data() + let signal = signalSendingData(data, statusCode: 10) + + var errored = false + signal.filter(statusCodes: 0..<10).startWithResult { event in + switch event { + case .success(let object): + fail("called on non-correct status code: \(object)") + case .failure: + errored = true + } + } + + expect(errored).to(beTruthy()) + } + it("filters out non-successful status codes") { let data = Data() let signal = signalSendingData(data, statusCode: 404) From 9b515db483ed603b2f0910348df38ea53d6ce978 Mon Sep 17 00:00:00 2001 From: Luciano Date: Mon, 12 Mar 2018 18:39:14 -0300 Subject: [PATCH 4/7] Adding missing docs in some other filters. --- Sources/ReactiveMoya/SignalProducer+Response.swift | 3 +++ Sources/RxMoya/Observable+Response.swift | 3 +++ Sources/RxMoya/Single+Response.swift | 3 +++ 3 files changed, 9 insertions(+) diff --git a/Sources/ReactiveMoya/SignalProducer+Response.swift b/Sources/ReactiveMoya/SignalProducer+Response.swift index 876af8867..f789715a5 100644 --- a/Sources/ReactiveMoya/SignalProducer+Response.swift +++ b/Sources/ReactiveMoya/SignalProducer+Response.swift @@ -19,18 +19,21 @@ extension SignalProducerProtocol where Value == Response, Error == MoyaError { return filter(statusCodes: statusCodes.lowerBound...statusCodes.upperBound-1) } + /// Filters out responses that have the specified `statusCode`. public func filter(statusCode: Int) -> SignalProducer { return producer.flatMap(.latest) { response -> SignalProducer in return unwrapThrowable { try response.filter(statusCode: statusCode) } } } + /// Filters out responses where `statusCode` falls within the range 200 - 299. public func filterSuccessfulStatusCodes() -> SignalProducer { return producer.flatMap(.latest) { response -> SignalProducer in return unwrapThrowable { try response.filterSuccessfulStatusCodes() } } } + /// Filters out responses where `statusCode` falls within the range 200 - 399 public func filterSuccessfulStatusAndRedirectCodes() -> SignalProducer { return producer.flatMap(.latest) { response -> SignalProducer in return unwrapThrowable { try response.filterSuccessfulStatusAndRedirectCodes() } diff --git a/Sources/RxMoya/Observable+Response.swift b/Sources/RxMoya/Observable+Response.swift index 6f5c57921..397dc9f0e 100644 --- a/Sources/RxMoya/Observable+Response.swift +++ b/Sources/RxMoya/Observable+Response.swift @@ -19,18 +19,21 @@ extension ObservableType where E == Response { return filter(statusCodes: statusCodes.lowerBound...statusCodes.upperBound-1) } + /// Filters out responses that has the specified `statusCode`. public func filter(statusCode: Int) -> Observable { return flatMap { response -> Observable in return Observable.just(try response.filter(statusCode: statusCode)) } } + /// Filters out responses where `statusCode` falls within the range 200 - 299. public func filterSuccessfulStatusCodes() -> Observable { return flatMap { response -> Observable in return Observable.just(try response.filterSuccessfulStatusCodes()) } } + /// Filters out responses where `statusCode` falls within the range 200 - 399 public func filterSuccessfulStatusAndRedirectCodes() -> Observable { return flatMap { response -> Observable in return Observable.just(try response.filterSuccessfulStatusAndRedirectCodes()) diff --git a/Sources/RxMoya/Single+Response.swift b/Sources/RxMoya/Single+Response.swift index fc2bc3073..65d1de685 100644 --- a/Sources/RxMoya/Single+Response.swift +++ b/Sources/RxMoya/Single+Response.swift @@ -19,18 +19,21 @@ extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Respo return filter(statusCodes: statusCodes.lowerBound...statusCodes.upperBound-1) } + /// Filters out responses that have the specified `statusCode`. public func filter(statusCode: Int) -> Single { return flatMap { response -> Single in return Single.just(try response.filter(statusCode: statusCode)) } } + /// Filters out responses where `statusCode` falls within the range 200 - 299. public func filterSuccessfulStatusCodes() -> Single { return flatMap { response -> Single in return Single.just(try response.filterSuccessfulStatusCodes()) } } + /// Filters out responses where `statusCode` falls within the range 200 - 399 public func filterSuccessfulStatusAndRedirectCodes() -> Single { return flatMap { response -> Single in return Single.just(try response.filterSuccessfulStatusAndRedirectCodes()) From a8c06294c2cda3259496e4d699b4d4a5a9679cee Mon Sep 17 00:00:00 2001 From: Luciano Date: Sun, 18 Mar 2018 16:07:53 -0300 Subject: [PATCH 5/7] Changing calls to filters --- Sources/ReactiveMoya/SignalProducer+Response.swift | 4 +++- Sources/RxMoya/Observable+Response.swift | 4 +++- Sources/RxMoya/Single+Response.swift | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Sources/ReactiveMoya/SignalProducer+Response.swift b/Sources/ReactiveMoya/SignalProducer+Response.swift index f789715a5..ab871b4d9 100644 --- a/Sources/ReactiveMoya/SignalProducer+Response.swift +++ b/Sources/ReactiveMoya/SignalProducer+Response.swift @@ -16,7 +16,9 @@ extension SignalProducerProtocol where Value == Response, Error == MoyaError { /// Filters out responses that don't fall within the given range, generating errors when others are encountered. public func filter(statusCodes: Range) -> SignalProducer { - return filter(statusCodes: statusCodes.lowerBound...statusCodes.upperBound-1) + return producer.flatMap(.latest) { response -> SignalProducer in + return unwrapThrowable { try response.filter(statusCodes: statusCodes) } + } } /// Filters out responses that have the specified `statusCode`. diff --git a/Sources/RxMoya/Observable+Response.swift b/Sources/RxMoya/Observable+Response.swift index 397dc9f0e..2bffca424 100644 --- a/Sources/RxMoya/Observable+Response.swift +++ b/Sources/RxMoya/Observable+Response.swift @@ -16,7 +16,9 @@ extension ObservableType where E == Response { /// Filters out responses that don't fall within the given range, generating errors when others are encountered. public func filter(statusCodes: Range) -> Observable { - return filter(statusCodes: statusCodes.lowerBound...statusCodes.upperBound-1) + return flatMap { response -> Observable in + return Observable.just(try response.filter(statusCodes: statusCodes)) + } } /// Filters out responses that has the specified `statusCode`. diff --git a/Sources/RxMoya/Single+Response.swift b/Sources/RxMoya/Single+Response.swift index 65d1de685..c174ada18 100644 --- a/Sources/RxMoya/Single+Response.swift +++ b/Sources/RxMoya/Single+Response.swift @@ -16,7 +16,9 @@ extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Respo /// Filters out responses that don't fall within the given range, generating errors when others are encountered. public func filter(statusCodes: Range) -> Single { - return filter(statusCodes: statusCodes.lowerBound...statusCodes.upperBound-1) + return flatMap { response -> Single in + return Single.just(try response.filter(statusCodes: statusCodes)) + } } /// Filters out responses that have the specified `statusCode`. From 8841722fad9a82b95b084d1199f4eaa46c3382ba Mon Sep 17 00:00:00 2001 From: Luciano Date: Sun, 18 Mar 2018 16:30:33 -0300 Subject: [PATCH 6/7] Adding changelog entry. --- Changelog.md | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Changelog.md b/Changelog.md index df400660f..56553770e 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,12 +1,16 @@ # Next +### Added + +- Convenience filter by `Range` on reponses. [#1605](https://github.com/Moya/Moya/pull/1605) by [@LucianoPAlmeida](https://github.com/LucianoPAlmeida). + # [11.0.1] - 2018-02-26 ### Fixed - Fixed Alamofire validation not being performed on `.uploadMultipart` requests. -[#1591](https://github.com/Moya/Moya/pull/1591) by [@SD10](https://github.com/SD10). + [#1591](https://github.com/Moya/Moya/pull/1591) by [@SD10](https://github.com/SD10). - Fixed Alamofire validation not being performed on stubbed requests. -[#1593](https://github.com/Moya/Moya/pull/1593) by [@SD10](https://github.com/sd10). + [#1593](https://github.com/Moya/Moya/pull/1593) by [@SD10](https://github.com/sd10). # [11.0.0] - 2018-02-07 - No changes @@ -22,13 +26,13 @@ ### Changed - **Breaking Change** Updated minimum version of `ReactiveSwift` to 3.0. -[#1470](https://github.com/Moya/Moya/pull/1470) by [@larryonoff](https://github.com/larryonoff). + [#1470](https://github.com/Moya/Moya/pull/1470) by [@larryonoff](https://github.com/larryonoff). - **Breaking Change** Changed the `validate` property of `TargetType` to use new `ValidationType` enum representing valid status codes. [#1505](https://github.com/Moya/Moya/pull/1505) by [@SD10](https://github.com/sd10), [@amaurydavid](https://github.com/amaurydavid). # [10.0.2] - 2018-01-26 ### Fixed - Fixed a bug where modifying `.uploadMultipart`, `.uploadCompositeMultipart`, `.uploadFile`, `.downloadDestination`, and `.downloadParameters` tasks through an `endpointClosure` has no effect on the final request. -[#1550](https://github.com/Moya/Moya/pull/1550) by [@SD10](https://github.com/sd10), [@sunshinejr](https://github.com/sunshinejr). + [#1550](https://github.com/Moya/Moya/pull/1550) by [@SD10](https://github.com/sd10), [@sunshinejr](https://github.com/sunshinejr). - Fixed a bug where `URLEncoding.httpBody` wasn't allowed as `bodyEncoding` in `Task.requestCompositeParameters()`. [#1557](https://github.com/Moya/Moya/pull/1557) by [@sunshinejr](https://github.com/sunshinejr). # [10.0.1] - 2017-11-23 @@ -172,7 +176,7 @@ - **Breaking Change** Renamed `URL` in `Endpoint` to `url`. - **Breaking Change** Renamed `StructTarget` to `MultiTarget`. - Demo project has been updated with new DemoMultiTarget target, new project -structure and more. + structure and more. ### Added - Readded support for iOS 8 and macOS 10.10. From 048f7795b333eebaac99dbd24598b7d80b2415cf Mon Sep 17 00:00:00 2001 From: Luciano Date: Sun, 18 Mar 2018 16:49:43 -0300 Subject: [PATCH 7/7] Adding lowerbound for Range and ClosedRange filters. --- Tests/Observable+MoyaSpec.swift | 42 +++++++++++++++++++++++++++-- Tests/SignalProducer+MoyaSpec.swift | 38 ++++++++++++++++++++++++-- Tests/Single+MoyaSpec.swift | 38 ++++++++++++++++++++++++-- 3 files changed, 112 insertions(+), 6 deletions(-) diff --git a/Tests/Observable+MoyaSpec.swift b/Tests/Observable+MoyaSpec.swift index 17694628e..7041dcedc 100644 --- a/Tests/Observable+MoyaSpec.swift +++ b/Tests/Observable+MoyaSpec.swift @@ -6,7 +6,7 @@ import Nimble final class ObservableMoyaSpec: QuickSpec { override func spec() { describe("status codes filtering") { - it("filters out unrequested status codes") { + it("filters out unrequested status codes closed range upperbound") { let data = Data() let observable = Response(statusCode: 10, data: data).asObservable() @@ -25,7 +25,26 @@ final class ObservableMoyaSpec: QuickSpec { expect(errored).to(beTruthy()) } - it("filters out unrequested status codes") { + it("filters out unrequested status codes closed range lowerbound") { + let data = Data() + let observable = Response(statusCode: -1, data: data).asObservable() + + var errored = false + _ = observable.filter(statusCodes: 0...9).subscribe { event in + switch event { + case .next(let object): + fail("called on non-correct status code: \(object)") + case .error: + errored = true + default: + break + } + } + + expect(errored).to(beTruthy()) + } + + it("filters out unrequested status codes range upperbound") { let data = Data() let observable = Response(statusCode: 10, data: data).asObservable() @@ -44,6 +63,25 @@ final class ObservableMoyaSpec: QuickSpec { expect(errored).to(beTruthy()) } + it("filters out unrequested status codes range lowerbound") { + let data = Data() + let observable = Response(statusCode: -1, data: data).asObservable() + + var errored = false + _ = observable.filter(statusCodes: 0..<10).subscribe { event in + switch event { + case .next(let object): + fail("called on non-correct status code: \(object)") + case .error: + errored = true + default: + break + } + } + + expect(errored).to(beTruthy()) + } + it("filters out non-successful status codes") { let data = Data() let observable = Response(statusCode: 404, data: data).asObservable() diff --git a/Tests/SignalProducer+MoyaSpec.swift b/Tests/SignalProducer+MoyaSpec.swift index be0bc4236..28e12ac46 100644 --- a/Tests/SignalProducer+MoyaSpec.swift +++ b/Tests/SignalProducer+MoyaSpec.swift @@ -10,7 +10,7 @@ private func signalSendingData(_ data: Data, statusCode: Int = 200) -> SignalPro final class SignalProducerMoyaSpec: QuickSpec { override func spec() { describe("status codes filtering") { - it("filters out unrequested status codes") { + it("filters out unrequested status codes closed range upperbound") { let data = Data() let signal = signalSendingData(data, statusCode: 10) @@ -27,7 +27,24 @@ final class SignalProducerMoyaSpec: QuickSpec { expect(errored).to(beTruthy()) } - it("filters out unrequested status codes range") { + it("filters out unrequested status codes closed range lowerbound") { + let data = Data() + let signal = signalSendingData(data, statusCode: -1) + + var errored = false + signal.filter(statusCodes: 0...9).startWithResult { event in + switch event { + case .success(let object): + fail("called on non-correct status code: \(object)") + case .failure: + errored = true + } + } + + expect(errored).to(beTruthy()) + } + + it("filters out unrequested status codes range upperbound") { let data = Data() let signal = signalSendingData(data, statusCode: 10) @@ -44,6 +61,23 @@ final class SignalProducerMoyaSpec: QuickSpec { expect(errored).to(beTruthy()) } + it("filters out unrequested status codes range lowerbound") { + let data = Data() + let signal = signalSendingData(data, statusCode: -1) + + var errored = false + signal.filter(statusCodes: 0..<10).startWithResult { event in + switch event { + case .success(let object): + fail("called on non-correct status code: \(object)") + case .failure: + errored = true + } + } + + expect(errored).to(beTruthy()) + } + it("filters out non-successful status codes") { let data = Data() let signal = signalSendingData(data, statusCode: 404) diff --git a/Tests/Single+MoyaSpec.swift b/Tests/Single+MoyaSpec.swift index bc3188a33..1dc3d5bd4 100644 --- a/Tests/Single+MoyaSpec.swift +++ b/Tests/Single+MoyaSpec.swift @@ -6,7 +6,7 @@ import Nimble final class SingleMoyaSpec: QuickSpec { override func spec() { describe("status codes filtering") { - it("filters out unrequested status codes closed range") { + it("filters out unrequested status codes closed range upperbound") { let data = Data() let single = Response(statusCode: 10, data: data).asSingle() @@ -23,7 +23,24 @@ final class SingleMoyaSpec: QuickSpec { expect(errored).to(beTruthy()) } - it("filters out unrequested status codes with range") { + it("filters out unrequested status codes closed range lowerbound") { + let data = Data() + let single = Response(statusCode: -1, data: data).asSingle() + + var errored = false + _ = single.filter(statusCodes: 0...9).subscribe { event in + switch event { + case .success(let object): + fail("called on non-correct status code: \(object)") + case .error: + errored = true + } + } + + expect(errored).to(beTruthy()) + } + + it("filters out unrequested status codes with range upperbound") { let data = Data() let single = Response(statusCode: 10, data: data).asSingle() @@ -40,6 +57,23 @@ final class SingleMoyaSpec: QuickSpec { expect(errored).to(beTruthy()) } + it("filters out unrequested status codes with range lowerbound") { + let data = Data() + let single = Response(statusCode: -1, data: data).asSingle() + + var errored = false + _ = single.filter(statusCodes: 0..<10).subscribe { event in + switch event { + case .success(let object): + fail("called on non-correct status code: \(object)") + case .error: + errored = true + } + } + + expect(errored).to(beTruthy()) + } + it("filters out non-successful status codes") { let data = Data() let single = Response(statusCode: 404, data: data).asSingle()