Comments (4)
That's a good idea, but how would you imagine the API for that?
For example if you write:
id<OHHTTPStubsDescriptor> myStub =
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
NSData* stubData = [@"Hello World!" dataUsingEncoding:NSUTF8StringEncoding];
return [OHHTTPStubsResponse responseWithData:stubData statusCode:200 headers:nil];
}];
How would you then trigger the response of a specific/given request, even as this stubs every request (return YES
in first block)? We can't imagine a method like [myStub triggerResponse]
in that case…
Another solution would be to add this kind of API on the OHHTTPStubsResponse
object, like a -(dispatch_block_t)responseTrigger
method on OHHTTPStubsResponse
so you can do something like this:
__block dispatch_block_t sendResponseBlock = nil;
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
NSData* stubData = [@"Hello World!" dataUsingEncoding:NSUTF8StringEncoding];
OHHTTPStubsResponse stubResponse = [OHHTTPStubsResponse responseWithData:stubData statusCode:200 headers:nil];
sendResponseBlock = [stubResponse responseTrigger];
return stubResponse:
}
// some code that send a network request, that will be catched by OHHTTPStubs
// then when you want to trigger the response:
if (sendResponseBlock) sendResponseBlock();
But still that won't solve any problem, as:
- At the time the
if (sendResponseBlock) sendResponseBlock();
line is hit, the RunLoop won't have excuted and the request won't really have been sent, asNSURLConnection
handles networks request using events watching installed onNSRunLoop
(that's why there is ascheduleInRunLoop:modes:
method onNSURLConnection
) - Even so, network requests are typically asynchronous, so you would have to block your Unit Test code anyway to wait for the request to be at least sent (which is done asychronously by
NSURLConnection
orNSURLSession
anyway)
The problem being that Cocoa's URL Loading System has a dedicated thread that manage network requests to send them and manage the response, and is the one responsive for calling -canInitRequest:
then startLoading
(asynchronously, on this dedicated thread) on the NSURLProtocol
classes like the one OHHTTPStubsProtocol
uses.
So I'm afraid we don't have much control over this, as all this process is asynchronous by nature.
But if you have any suggestion on how to implement it and how would the OHHTTPStubs
API could look to make this work, please share as this would be quite an interesting feature!
from ohhttpstubs.
I have to be honest I came up with a solutions similar to the second one you proposed and I stopped because of the problems you listed. I'm afraid that at the moment I do not have a good idea on how to implement that, an this is also the reason why I've been a little generic in my first comment: I was trying to start sort of brainstorming.
Anyway I'll keep a background thread running in my brain to see if I can came up with a proper solution.
I think that is worth to keep this issue open so that other peoples can eventually partecipate to the discussion.
from ohhttpstubs.
Agreed, i'll keep it open and label it accordingly.
from ohhttpstubs.
Closing this as it has been open for too long without any new propositions. Feel free to reopen if any idea to propose this comes to mind but I think this is conceptually impossible.
from ohhttpstubs.
Related Issues (20)
- <!-- Thanks for taking the time to report your issue with OHHTTPStubs! When submitting your issue, please make sure to check the following boxes by putting an x in each appropriate [ ] so we can fully understand the context of your problem and help you better -->
- <!-- Thanks for taking the time to report your issue with OHHTTPStubs! When submitting your issue, please make sure to check the following boxes by putting an x in each appropriate [ ] so we can fully understand the context of your problem and help you better -->
- Xcode 12 - Warnings related to iOS 8 support (Swift Package Manager) HOT 6
- Mocktail support HOT 2
- pod 'AFNetworking', '~> 3.0'
- Incorrect example for onStubActivation on Usage Examples page
- [wiki] OHHTTPStubsResponse to HTTPStubsResponse with Swift HOT 4
- Module 'OHHTTPStubs' has no member named 'removeAllStubs' in 9.0.0 HOT 2
- Xcode Analyze: Logic error
- N HOT 1
- Travis CI has seemed to be moved. HOT 3
- How to use in a UITest HOT 9
- OHHTTPStubs with SPM in Xcode 12.5 HOT 1
- Fluent API for stubbing
- how do i use it HOT 1
- Liscense HOT 1
- 4 da world HOT 1
- - [ ] Submitting a maybeaplle @mtodd []()![image](https://user-images.githubusercontent.com/122186144/230750410-f43c3ffb-f6a7-4bd4-9970-8b065670ea28.jpeg)@craystyle1212
- Won't build with Carthage and Xcode 14.3 HOT 7
- App Crashes on Device When OHHTTPStubsSwift Added to iOS Framework via Swift Package Manager HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ohhttpstubs.