OHHTTPStubs
OHHTTPStubs copied to clipboard
How to stub the request of mp4 and fake to respond it
I use uiwebview to play a video, and try to replace the remote url with local url by the stubRequestsPassingTest method, but, it is always failed
The stubbing of an MP4 file is no different from the stubbing of any other request by any other type of file.
- What did you try? (please give code samples as well as the remote full URL to be stubbed so we can check that your matcher is correct)
- How did you install OHHTTPStubs into your project?
- Are you using Objective-C or Swift?
- What error do you have, if any?
- Do you manage to stub any other request done by your application but have issue only with this one, or do you fail to stub any request?
- Did you read the Wiki examples and troubleshooting pages?
- …
Without any code nor more information, it won't be possible to help.
What did you try? (please give code samples as well as the remote full URL to be stubbed so we can check that your matcher is correct) [wantobe]: I've tried to stub a stream URL to play in webview. Used the responseWithFileURL:statusCode:header: to reponse it. Take an example, the user want to play a with "http://www.ganttcn.com/ouhk/OUHK-IntroductionToTheOpenUniversityofHongKong.mp4", I used the library to respond with a "rvideo-1024x512.mp4"(local).
How did you install OHHTTPStubs into your project? [wantobe]: I installed OHHTTPStubs(version 5.2.2) using Cocoapods(podfile subspec: pod 'OHHTTPStubs'). I have not yet intergated it into my project, and i've just made a test running of your example.
Are you using Objective-C or Swift? **[wantobe]:**Objective-C
What error do you have, if any? [wantobe]: No error, but something unexpect. When i using the simulator to play the web video, the request would be stubbed, but the video never been played successfully, but when i using the real device(iphone6 and 5s, ios10.1), i faild to stub the requests.
Do you manage to stub any other request done by your application but have issue only with this one, or do you fail to stub any request? [wantobe]: only video request can't work well, other request(text, image...) is ok.
Did you read the Wiki examples and troubleshooting pages? [wantobe]: I had read them. The issue like 'https://github.com/AliSoftware/OHHTTPStubs/issues/187', i can't make it still.
By the way, my Xcode version is 8.1.
The code as shown below:
- (void)viewDidLoad
{
[super viewDidLoad];
[self installTextStub:self.installTextStubSwitch];
[self installImageStub:self.installImageStubSwitch];
[OHHTTPStubs onStubActivation:^(NSURLRequest * _Nonnull request, id<OHHTTPStubsDescriptor> _Nonnull stub, OHHTTPStubsResponse * _Nonnull responseStub) {
NSLog(@"[OHHTTPStubs] Request to %@ has been stubbed with %@", request.URL, stub.name);
}];
static id<OHHTTPStubsDescriptor> stub = nil;
stub = [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [request.URL.pathExtension isEqualToString:@"mp4"];
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
NSString *str = OHPathForFile(@"video-1024x512.mp4", self.class);
//cheaking the local file path is correct or not
NSLog(@"the local mp4 file size: %lu", (unsigned long)[NSData dataWithContentsOfFile:str].length);
return [[OHHTTPStubsResponse responseWithFileURL:[NSURL fileURLWithPath:str]
statusCode:206
headers:@{@"Content-Type":@"video/mpeg4"}]
requestTime:0.f
responseTime:OHHTTPStubsDownloadSpeedWifi];
}];
stub.name = @"video stub";
//webview has been appended in xib
[self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.ganttcn.com/ssjy/ouhk"]]];
//geting the response
//the real remote full url, one of the web page above video file url
NSString* urlString = @"http://www.ganttcn.com/ouhk/OUHK-IntroductionToTheOpenUniversityofHongKong.mp4";
NSURLRequest* req = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[NSURLConnection sendAsynchronousRequest:req
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse* resp, NSData* data, NSError* error)
{
NSLog(@"=======================%@",resp);
}];
}
If you put a breakpoint in the first block (return [request.URL.pathExtension isEqualToString:@"mp4"];
) do the code goes in that block?
I wonder if something has changed in iOS10 related to UIWebView not using NSURLConnection
anymore (and if that's the case and it uses a custom NSURLSession
since iOS10, then we're screwed except if iOS10 also allows us to provide custom protocols to UIWebView
…)
Thanks for your answer.
I found that startLoading
was not invoked, so, i'm sure it is an iOS bug.
A similar issue from stackoverflow: Custom NSURLProtocol class for WebView doesn't work when loading video in HTML5 document
Ok, then I'm starting to wonder if something didn't change in the last versions of iOS, like maybe since iOS10 the UIWebView
now use an internal NSURLSession
to do their requests (instead of NSURLConnection
that they used so far).
Which would mean that we would need a way to inject our custom NSURLProtocol
(used by OHHTTPStubs
to intercept the requests and stub them) in the UIWebView
's NSURLSessionConfiguration
before the UIWebView
's internal NSURLSession
is created with that configuration.
Not sure that UIWebView
exposes a way do provide a custom NSURLSessionConfiguration
for it (and thus inject OHHTTPStubs
's custom protocol into it) since they did that change (if that's indeed the reason it doesn't work anymore)…
Could you try on an iOS9 simulator and see if it works? If so, that would confirm that it's indeed due to a change in iOS10. (See also #227 which might suggest the same conclusion…)
I'd also be interested if you'd be able to test it, both in iOS9 and iOS10, with anything other than an mp4, like an image or an HTML webpage… as your StackOverflow link suggests that this is only related to video and multimedia content.
So maybe nothing changed between iOS9 and iOS10 like I though in my previous comment and the stubbing still works even in iOS10… but just not for multimedia content because of that iOS bug.
In any case (iOS10 changing UIWebView's implementation, or bug with multimedia content not going thru the NSURLProtocol
chain), even if it would be an Apple bug in both cases, it would be worth updating the README section "Known limitations" with that info.