1662532200
OHHTTPStubs
is a library designed to stub your network requests very easily. It can help you:
It works with NSURLConnection
, NSURLSession
, AFNetworking
, Alamofire
or any networking framework that use Cocoa's URL Loading System.
Documentation & Usage Examples
OHHTTPStubs
headers are fully documented using Appledoc-like / Headerdoc-like comments in the header files. You can also read the online documentation here.
In Objective-C
[HTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return [request.URL.host isEqualToString:@"mywebservice.com"];
} withStubResponse:^HTTPStubsResponse*(NSURLRequest *request) {
// Stub it with our "wsresponse.json" stub file (which is in same bundle as self)
NSString* fixture = OHPathForFile(@"wsresponse.json", self.class);
return [HTTPStubsResponse responseWithFileAtPath:fixture
statusCode:200 headers:@{@"Content-Type":@"application/json"}];
}];
In Swift
This example is using the Swift helpers found in OHHTTPStubsSwift.swift
provided by the OHHTTPStubs/Swift
subspec or OHHTTPStubs
package.
stub(condition: isHost("mywebservice.com")) { _ in
// Stub it with our "wsresponse.json" stub file (which is in same bundle as self)
let stubPath = OHPathForFile("wsresponse.json", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type":"application/json"])
}
Note: if you're using OHHTTPStubs
's Swiftier API (OHHTTPStubsSwift.swift
and the Swift
subspec or OHTTPStubsSwift
package), you can also compose the matcher functions like this: stub(isScheme("http") && isHost("myhost")) { … }
OHHTTPStubs
.Instead of writing the content of the stubs you want to use manually, you can use tools like SWHttpTrafficRecorder to record network requests into files. This way you can later use those files as stub responses.
This tool can record all three formats that are supported by OHHTTPStubs
(the HTTPMessage
format, the simple response boby/content file, and the Mocktail
format).
(There are also other ways to perform a similar task, including using curl -is <url> >foo.response
to generate files compatible with the HTTPMessage
format, or using other network recording libraries similar to SWHttpTrafficRecorder
).
Compatibility
OHHTTPStubs
is compatible with iOS5+, OS X 10.7+, tvOS.OHHTTPStubs
also works with NSURLSession
as well as any network library wrapping them.OHHTTPStubs
is fully compatible with Swift 3.x, 4.x and Swift 5.x.Nullability annotations have also been added to the ObjC API to allow a cleaner API when used from Swift even if you don't use the dedicated Swift API wrapper provided by OHHTTPStubsSwift.swift
.
Updating to Version 9.0+
OH
prefix (OHHHTTPStubs
-> HTTPStubs
, OHHTTPStubsResponse
-> HTTPStubsResponse
, etc).OHPathHelpers
class was renamed HTTPStubsPathHelpers
.Installing in your projects
Using CocoaPods is the recommended way.
OHHTTPStubs
from Objective-C only, add pod 'OHHTTPStubs'
to your Podfile
.OHHTTPStubs
from Swift, add pod 'OHHTTPStubs/Swift'
to your Podfile
instead.pod 'OHHTTPStubs/Swift' # includes the Default subspec, with support for NSURLSession & JSON, and the Swiftier API wrappers
OHHTTPStubs
is split into subspecs so that when using Cocoapods, you can get only what you need, no more, no less.
NSURLSession
, JSON
, and OHPathHelpers
Swift
subspec adds the Swiftier API to that default subspecHTTPMessage
and Mocktail
are opt-in subspecs: list them explicitly if you need themOHPathHelpers
doesn't depend on Core
and can be used independently of OHHTTPStubs
altogetherList of all the subspecs & their dependencies
Here's a list of which subspecs are included for each of the different lines you could use in your Podfile
:
Subspec | Core | NSURLSession | JSON | Swift | OHPathHelpers | HTTPMessage | Mocktail |
---|---|---|---|---|---|---|---|
pod 'OHHTTPStubs' | ✅ | ✅ | ✅ | ✅ | |||
pod 'OHHTTPStubs/Default' | ✅ | ✅ | ✅ | ✅ | |||
pod 'OHHTTPStubs/Swift' | ✅ | ✅ | ✅ | ✅ | ✅ | ||
pod 'OHHTTPStubs/Core' | ✅ | ||||||
pod 'OHHTTPStubs/NSURLSession' | ✅ | ✅ | |||||
pod 'OHHTTPStubs/JSON' | ✅ | ✅ | |||||
pod 'OHHTTPStubs/OHPathHelpers' | ✅ | ||||||
pod 'OHHTTPStubs/HTTPMessage' | ✅ | ✅ | |||||
pod 'OHHTTPStubs/Mocktail' | ✅ | ✅ |
OHHTTPStubs
is compatible with Swift Package Manager, and provides 2 targets for consumption: OHHTTPStubs
and OHHTTPStubsSwift
.
OHHTTPStubs
is equivalent to the OHHTTPStubs
subspec.OHHTTPStubsSwift
is equivalent to the OHHTTPStubs/Swift
subspec.Note: We currently do not have support for the HTTPMessage or Mocktail subspecs in Swift Package Manager. If you are interested in these, please open an issue to explain your needs.
OHHTTPStubs
is also compatible with Carthage. Just add it to your Cartfile
.
Note: The OHHTTPStubs.framework
built with Carthage will include all features of OHHTTPStubs
turned on (in other words, all subspecs of the pod), including NSURLSession
and JSON
support, OHPathHelpers
, HTTPMessage
and Mocktail
support, and the Swiftier API.
OHHTTPStubs
supports Swift 3.0 (Xcode 8+), Swift 3.1 (Xcode 8.3+), Swift 3.2 (Xcode 9.0+), Swift 4.0 (Xcode 9.0+), Swift 4.1 (Xcode 9.3+), Swift 4.2 (Xcode 10+), Swift 5.0 (Xcode 10.2), and Swift 5.1 (Xcode 11) however we are only testing Swift 4.x (using Xcode 9.1 and 10.1) and Swift 5.x (using Xcode 10.2 AND 11) in CI.
Here are some details about the correct setup you need depending on how you integrated OHHTTPStubs
into your project.
CocoaPods: nothing to do
If you use CocoaPods version 1.1.0.beta.1
or later, then CocoaPods will compile OHHTTPStubs
with the right Swift Version matching the one you use for your project automatically. You have nothing to do! 🎉
For more info, see CocoaPods/CocoaPods#5540 and CocoaPods/CocoaPods#5760.
Carthage: choose the right version
The project is set up with SWIFT_VERSION=5.0
on master
.
This means that the framework on master
will build using:
If you want Carthage to build the framework with Swift 3.x you can:
OHHTTPStubs
(6.2.0) — whose master
branch uses 3.0
SWIFT_VERSION
build setting to 3.0
SWIFT_VERSION
to carthage via XCODE_XCCONFIG_FILE=<config file declaring SWIFT_VERSION> carthage build
Special Considerations
OHHTTPStubs
is ideal to write unit tests that normally would perform network requests. But if you use it in your unit tests, don't forget to:
[HTTPStubs removeAllStubs]
in your tearDown
method. see this wiki page for more infoOHHTTPStubs
is automatically loaded and installed (at the time the library is loaded in memory), both for:
NSURLConnection
or [NSURLSession sharedSession]
— thanks to this codeNSURLSession
that was created via [NSURLSession sessionWithConfiguration:…]
and using either [NSURLSessionConfiguration defaultSessionConfiguration]
or [NSURLSessionConfiguration ephemeralSessionConfiguration]
configuration — thanks to method swizzling done here in the code.If you need to disable (and re-enable) OHHTTPStubs
— globally or per NSURLSession
— you can use [HTTPStubs setEnabled:]
/ [HTTPStubs setEnabled:forSessionConfiguration:]
.
OHHTTPStubs
can't work on background sessions (sessions created using [NSURLSessionConfiguration backgroundSessionConfiguration]
) because background sessions don't allow the use of custom NSURLProtocols
and are handled by the iOS Operating System itself.OHHTTPStubs
don't simulate data upload. The NSURLProtocolClient
@protocol
does not provide a way to signal the delegate that data has been sent (only that some has been loaded), so any data in the HTTPBody
or HTTPBodyStream
of an NSURLRequest
, or data provided to -[NSURLSession uploadTaskWithRequest:fromData:];
will be ignored, and more importantly, the -URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:
delegate method will never be called when you stub the request using OHHTTPStubs
.OHTTPStubs
has a known issue with redirects that we believe is an Apple bug. It has been discussed here and here. The actual result of this bug is that redirects with a zero second delay may nondeterministically end up with a null response.As far as I know, there's nothing we can do about those three limitations. Please let me know if you know a solution that would make that possible anyway.
OHHTTPStubs
can be used on apps submitted on the App Store. It does not use any private API and nothing prevents you from shipping it.
But you generally only use stubs during the development phase and want to remove your stubs when submitting to the App Store. So be careful to only include OHHTTPStubs
when needed (only in your test targets, or only inside #if DEBUG
sections, or by using per-Build-Configuration pods) to avoid forgetting to remove it when the time comes that you release for the App Store and you want your requests to hit the real network!
License and Credits
This project and library has been created by Olivier Halligon (@aligatr on Twitter) and is under the MIT License.
It has been inspired by this article from InfiniteLoop.dk.
I would also like to thank:
NSInputStream
If you want to support the development of this library, feel free to. Thanks to all contributors so far!
Author: AliSoftware
Source code: https://github.com/AliSoftware/OHHTTPStubs
License: MIT license
#swift #objective-c
1662532200
OHHTTPStubs
is a library designed to stub your network requests very easily. It can help you:
It works with NSURLConnection
, NSURLSession
, AFNetworking
, Alamofire
or any networking framework that use Cocoa's URL Loading System.
Documentation & Usage Examples
OHHTTPStubs
headers are fully documented using Appledoc-like / Headerdoc-like comments in the header files. You can also read the online documentation here.
In Objective-C
[HTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return [request.URL.host isEqualToString:@"mywebservice.com"];
} withStubResponse:^HTTPStubsResponse*(NSURLRequest *request) {
// Stub it with our "wsresponse.json" stub file (which is in same bundle as self)
NSString* fixture = OHPathForFile(@"wsresponse.json", self.class);
return [HTTPStubsResponse responseWithFileAtPath:fixture
statusCode:200 headers:@{@"Content-Type":@"application/json"}];
}];
In Swift
This example is using the Swift helpers found in OHHTTPStubsSwift.swift
provided by the OHHTTPStubs/Swift
subspec or OHHTTPStubs
package.
stub(condition: isHost("mywebservice.com")) { _ in
// Stub it with our "wsresponse.json" stub file (which is in same bundle as self)
let stubPath = OHPathForFile("wsresponse.json", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type":"application/json"])
}
Note: if you're using OHHTTPStubs
's Swiftier API (OHHTTPStubsSwift.swift
and the Swift
subspec or OHTTPStubsSwift
package), you can also compose the matcher functions like this: stub(isScheme("http") && isHost("myhost")) { … }
OHHTTPStubs
.Instead of writing the content of the stubs you want to use manually, you can use tools like SWHttpTrafficRecorder to record network requests into files. This way you can later use those files as stub responses.
This tool can record all three formats that are supported by OHHTTPStubs
(the HTTPMessage
format, the simple response boby/content file, and the Mocktail
format).
(There are also other ways to perform a similar task, including using curl -is <url> >foo.response
to generate files compatible with the HTTPMessage
format, or using other network recording libraries similar to SWHttpTrafficRecorder
).
Compatibility
OHHTTPStubs
is compatible with iOS5+, OS X 10.7+, tvOS.OHHTTPStubs
also works with NSURLSession
as well as any network library wrapping them.OHHTTPStubs
is fully compatible with Swift 3.x, 4.x and Swift 5.x.Nullability annotations have also been added to the ObjC API to allow a cleaner API when used from Swift even if you don't use the dedicated Swift API wrapper provided by OHHTTPStubsSwift.swift
.
Updating to Version 9.0+
OH
prefix (OHHHTTPStubs
-> HTTPStubs
, OHHTTPStubsResponse
-> HTTPStubsResponse
, etc).OHPathHelpers
class was renamed HTTPStubsPathHelpers
.Installing in your projects
Using CocoaPods is the recommended way.
OHHTTPStubs
from Objective-C only, add pod 'OHHTTPStubs'
to your Podfile
.OHHTTPStubs
from Swift, add pod 'OHHTTPStubs/Swift'
to your Podfile
instead.pod 'OHHTTPStubs/Swift' # includes the Default subspec, with support for NSURLSession & JSON, and the Swiftier API wrappers
OHHTTPStubs
is split into subspecs so that when using Cocoapods, you can get only what you need, no more, no less.
NSURLSession
, JSON
, and OHPathHelpers
Swift
subspec adds the Swiftier API to that default subspecHTTPMessage
and Mocktail
are opt-in subspecs: list them explicitly if you need themOHPathHelpers
doesn't depend on Core
and can be used independently of OHHTTPStubs
altogetherList of all the subspecs & their dependencies
Here's a list of which subspecs are included for each of the different lines you could use in your Podfile
:
Subspec | Core | NSURLSession | JSON | Swift | OHPathHelpers | HTTPMessage | Mocktail |
---|---|---|---|---|---|---|---|
pod 'OHHTTPStubs' | ✅ | ✅ | ✅ | ✅ | |||
pod 'OHHTTPStubs/Default' | ✅ | ✅ | ✅ | ✅ | |||
pod 'OHHTTPStubs/Swift' | ✅ | ✅ | ✅ | ✅ | ✅ | ||
pod 'OHHTTPStubs/Core' | ✅ | ||||||
pod 'OHHTTPStubs/NSURLSession' | ✅ | ✅ | |||||
pod 'OHHTTPStubs/JSON' | ✅ | ✅ | |||||
pod 'OHHTTPStubs/OHPathHelpers' | ✅ | ||||||
pod 'OHHTTPStubs/HTTPMessage' | ✅ | ✅ | |||||
pod 'OHHTTPStubs/Mocktail' | ✅ | ✅ |
OHHTTPStubs
is compatible with Swift Package Manager, and provides 2 targets for consumption: OHHTTPStubs
and OHHTTPStubsSwift
.
OHHTTPStubs
is equivalent to the OHHTTPStubs
subspec.OHHTTPStubsSwift
is equivalent to the OHHTTPStubs/Swift
subspec.Note: We currently do not have support for the HTTPMessage or Mocktail subspecs in Swift Package Manager. If you are interested in these, please open an issue to explain your needs.
OHHTTPStubs
is also compatible with Carthage. Just add it to your Cartfile
.
Note: The OHHTTPStubs.framework
built with Carthage will include all features of OHHTTPStubs
turned on (in other words, all subspecs of the pod), including NSURLSession
and JSON
support, OHPathHelpers
, HTTPMessage
and Mocktail
support, and the Swiftier API.
OHHTTPStubs
supports Swift 3.0 (Xcode 8+), Swift 3.1 (Xcode 8.3+), Swift 3.2 (Xcode 9.0+), Swift 4.0 (Xcode 9.0+), Swift 4.1 (Xcode 9.3+), Swift 4.2 (Xcode 10+), Swift 5.0 (Xcode 10.2), and Swift 5.1 (Xcode 11) however we are only testing Swift 4.x (using Xcode 9.1 and 10.1) and Swift 5.x (using Xcode 10.2 AND 11) in CI.
Here are some details about the correct setup you need depending on how you integrated OHHTTPStubs
into your project.
CocoaPods: nothing to do
If you use CocoaPods version 1.1.0.beta.1
or later, then CocoaPods will compile OHHTTPStubs
with the right Swift Version matching the one you use for your project automatically. You have nothing to do! 🎉
For more info, see CocoaPods/CocoaPods#5540 and CocoaPods/CocoaPods#5760.
Carthage: choose the right version
The project is set up with SWIFT_VERSION=5.0
on master
.
This means that the framework on master
will build using:
If you want Carthage to build the framework with Swift 3.x you can:
OHHTTPStubs
(6.2.0) — whose master
branch uses 3.0
SWIFT_VERSION
build setting to 3.0
SWIFT_VERSION
to carthage via XCODE_XCCONFIG_FILE=<config file declaring SWIFT_VERSION> carthage build
Special Considerations
OHHTTPStubs
is ideal to write unit tests that normally would perform network requests. But if you use it in your unit tests, don't forget to:
[HTTPStubs removeAllStubs]
in your tearDown
method. see this wiki page for more infoOHHTTPStubs
is automatically loaded and installed (at the time the library is loaded in memory), both for:
NSURLConnection
or [NSURLSession sharedSession]
— thanks to this codeNSURLSession
that was created via [NSURLSession sessionWithConfiguration:…]
and using either [NSURLSessionConfiguration defaultSessionConfiguration]
or [NSURLSessionConfiguration ephemeralSessionConfiguration]
configuration — thanks to method swizzling done here in the code.If you need to disable (and re-enable) OHHTTPStubs
— globally or per NSURLSession
— you can use [HTTPStubs setEnabled:]
/ [HTTPStubs setEnabled:forSessionConfiguration:]
.
OHHTTPStubs
can't work on background sessions (sessions created using [NSURLSessionConfiguration backgroundSessionConfiguration]
) because background sessions don't allow the use of custom NSURLProtocols
and are handled by the iOS Operating System itself.OHHTTPStubs
don't simulate data upload. The NSURLProtocolClient
@protocol
does not provide a way to signal the delegate that data has been sent (only that some has been loaded), so any data in the HTTPBody
or HTTPBodyStream
of an NSURLRequest
, or data provided to -[NSURLSession uploadTaskWithRequest:fromData:];
will be ignored, and more importantly, the -URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:
delegate method will never be called when you stub the request using OHHTTPStubs
.OHTTPStubs
has a known issue with redirects that we believe is an Apple bug. It has been discussed here and here. The actual result of this bug is that redirects with a zero second delay may nondeterministically end up with a null response.As far as I know, there's nothing we can do about those three limitations. Please let me know if you know a solution that would make that possible anyway.
OHHTTPStubs
can be used on apps submitted on the App Store. It does not use any private API and nothing prevents you from shipping it.
But you generally only use stubs during the development phase and want to remove your stubs when submitting to the App Store. So be careful to only include OHHTTPStubs
when needed (only in your test targets, or only inside #if DEBUG
sections, or by using per-Build-Configuration pods) to avoid forgetting to remove it when the time comes that you release for the App Store and you want your requests to hit the real network!
License and Credits
This project and library has been created by Olivier Halligon (@aligatr on Twitter) and is under the MIT License.
It has been inspired by this article from InfiniteLoop.dk.
I would also like to thank:
NSInputStream
If you want to support the development of this library, feel free to. Thanks to all contributors so far!
Author: AliSoftware
Source code: https://github.com/AliSoftware/OHHTTPStubs
License: MIT license
#swift #objective-c
1596754901
The shift towards microservices and modular applications makes testing more important and more challenging at the same time. You have to make sure that the microservices running in containers perform well and as intended, but you can no longer rely on conventional testing strategies to get the job done.
This is where new testing approaches are needed. Testing your microservices applications require the right approach, a suitable set of tools, and immense attention to details. This article will guide you through the process of testing your microservices and talk about the challenges you will have to overcome along the way. Let’s get started, shall we?
Traditionally, testing a monolith application meant configuring a test environment and setting up all of the application components in a way that matched the production environment. It took time to set up the testing environment, and there were a lot of complexities around the process.
Testing also requires the application to run in full. It is not possible to test monolith apps on a per-component basis, mainly because there is usually a base code that ties everything together, and the app is designed to run as a complete app to work properly.
Microservices running in containers offer one particular advantage: universal compatibility. You don’t have to match the testing environment with the deployment architecture exactly, and you can get away with testing individual components rather than the full app in some situations.
Of course, you will have to embrace the new cloud-native approach across the pipeline. Rather than creating critical dependencies between microservices, you need to treat each one as a semi-independent module.
The only monolith or centralized portion of the application is the database, but this too is an easy challenge to overcome. As long as you have a persistent database running on your test environment, you can perform tests at any time.
Keep in mind that there are additional things to focus on when testing microservices.
Test containers are the method of choice for many developers. Unlike monolith apps, which lets you use stubs and mocks for testing, microservices need to be tested in test containers. Many CI/CD pipelines actually integrate production microservices as part of the testing process.
As mentioned before, there are many ways to test microservices effectively, but the one approach that developers now use reliably is contract testing. Loosely coupled microservices can be tested in an effective and efficient way using contract testing, mainly because this testing approach focuses on contracts; in other words, it focuses on how components or microservices communicate with each other.
Syntax and semantics construct how components communicate with each other. By defining syntax and semantics in a standardized way and testing microservices based on their ability to generate the right message formats and meet behavioral expectations, you can rest assured knowing that the microservices will behave as intended when deployed.
It is easy to fall into the trap of making testing microservices complicated, but there are ways to avoid this problem. Testing microservices doesn’t have to be complicated at all when you have the right strategy in place.
There are several ways to test microservices too, including:
What’s important to note is the fact that these testing approaches allow for asynchronous testing. After all, asynchronous development is what makes developing microservices very appealing in the first place. By allowing for asynchronous testing, you can also make sure that components or microservices can be updated independently to one another.
#blog #microservices #testing #caylent #contract testing #end-to-end testing #hoverfly #integration testing #microservices #microservices architecture #pact #testing #unit testing #vagrant #vcr
1609840501
Most landscapers think of their website as an online brochure. In reality of consumers have admitted to judging a company’s credibility based on their web design, making your website a virtual sales rep capable of generating massive amounts of leads and sales. If your website isn’t actively increasing leads and new landscaping contracts, it may be time for a redesign.
DataIT Solutions specializes in landscape website designing that are not only beautiful but also rank well in search engine results and convert your visitors into customers. We’ve specialized in the landscaping industry for over 10 years, and we look at your business from an owner’s perspective.
Why use our Landscapes for your landscape design?
Want to talk about your website?
If you are a gardener or have a gardening company please do not hesitate to contact us for a quote.
Need help with your website? Get in touch
#nature landscapes website design #landscapes website design #website design #website designing #website designer #designer
1600430400
Swift is a fast and efficient general-purpose programming language that provides real-time feedback and can be seamlessly incorporated into existing Objective-C code. This is why developers are able to write safer, more reliable code while saving time. It aims to be the best language that can be used for various purposes ranging from systems programming to mobile as well as desktop apps and scaling up to cloud services.
Below here, we list down the 10 best online resources to learn Swift language.
(The list is in no particular order)
#developers corner #free online resources to learn swift language #learn swift #learn swift free #learn swift online free #resources to learn swift #swift language #swift programming
1620983255
Automation and segregation can help you build better software
If you write automated tests and deliver them to the customer, he can make sure the software is working properly. And, at the end of the day, he paid for it.
Ok. We can segregate or separate the tests according to some criteria. For example, “white box” tests are used to measure the internal quality of the software, in addition to the expected results. They are very useful to know the percentage of lines of code executed, the cyclomatic complexity and several other software metrics. Unit tests are white box tests.
#testing #software testing #regression tests #unit tests #integration tests