如何嘲笑AJAX调用与NSURLProtocol?(How to mock AJAX call wi

2019-07-21 00:52发布

我有一个UIWebView,使得AJAX调用外部服务。 当离线我需要赶论文请求并返回本地JSON。

我实现了一个NSURLProtocol,我设法抓住了AJAX请求,问题是jQuery的总是返回一个0错误代码:

$.ajax({
  url: url,
  dataType: 'json',
  contentType: "application/json",
  success: function(jsonData){
    alert("success :");
  },
  error: function (request, status, error) {
    alert("failure :" + request.status );
  }

});

我总是得到request.status = 0

为了测试我的协议我试图嘲弄我的HTML内的图像和它的伟大工程。

  • HTML请求从google.fr图像=> 工作正常
  • AJAX调用一个JSON在亚马逊=> 失败

这是我的全面实施:

#import "EpubProtocol.h"

@implementation EpubProtocol

#pragma mark - NSURLProtocol

+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
    BOOL awsRequest = [self request:request contains:@"s3.amazonaws.com"];
    BOOL imgRequest = [self request:request contains:@"google.fr"];
    BOOL match = awsRequest || imgRequest;

    return match;
}


+ (NSURLRequest*)canonicalRequestForRequest:(NSURLRequest*)theRequest
{
    return theRequest;
}


- (void)startLoading {
    NSURLRequest *request = [self request];

    //Mock Amazon call
    if([EpubProtocol request:request contains:@"s3.amazonaws.com"]) {
        NSString *path = [[NSBundle bundleForClass:self.class] pathForResource:@"epub1" ofType:@"json"];
        NSData *data = [NSData dataWithContentsOfFile:path];

        [self mockRequest:request mimeType:@"application/json" data:data];
    }
    //Mock image call
    else if([EpubProtocol request:request contains:@"google.fr"]) {
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];

        [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.itespresso.fr/wp-content/gallery/yahoo/1-yahoo-logo.jpg"]] queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
            [self mockRequest:request mimeType:@"image/jpeg" data:data];
        }];
    }
}

- (void)stopLoading
{
    NSLog(@"Did stop loading");
}


#pragma mark - Request utils

+ (BOOL) request:(NSURLRequest*)request contains:(NSString*)domain {
    NSString *str = [[request URL] absoluteString];
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", domain];
    return [pred evaluateWithObject:str];
}


#pragma mark - Mock responses


-(void) mockRequest:(NSURLRequest*)request mimeType:(NSString*)mimeType data:(NSData*)data {
    id client = [self client];

    NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:[request URL] MIMEType:mimeType expectedContentLength:-1 textEncodingName:nil];

    [client URLProtocol:self didReceiveResponse:response
     cacheStoragePolicy:NSURLCacheStorageNotAllowed];
    [client URLProtocol:self didLoadData:data];
    [client URLProtocolDidFinishLoading:self];
}

@end

Answer 1:

该问题来源于webkit的哪些块由于跨域原点请求的响应。 由于我们嘲笑的反应,我们必须强制访问控制允许来源。

然后,我们还需要给力的内容类型的响应。

这里就是奇迹发生:

NSDictionary *headers = @{@"Access-Control-Allow-Origin" : @"*", @"Access-Control-Allow-Headers" : @"Content-Type"};
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:request.URL statusCode:200 HTTPVersion:@"1.1" headerFields:headers];

最终的实施方案中:

#import "EpubProtocol.h"

@implementation EpubProtocol

#pragma mark - NSURLProtocol

+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
    BOOL isAwsRequest = [self request:request contains:@"s3.amazonaws.com"];

    return isAwsRequest;
}

+ (NSURLRequest*)canonicalRequestForRequest:(NSURLRequest*)theRequest
{
    return theRequest;
}

- (void)startLoading {
    NSURLRequest *request = [self request];

    //Mock Amazon call
    if([EpubProtocol request:request contains:@"s3.amazonaws.com"]) {
        NSString *path = [[NSBundle bundleForClass:self.class] pathForResource:@"epub1" ofType:@"json"];
        NSData *data = [NSData dataWithContentsOfFile:path];

        [self mockRequest:request data:data];
    }
}

- (void)stopLoading
{
    NSLog(@"Did stop loading");
}


#pragma mark - Request utils

+ (BOOL) request:(NSURLRequest*)request contains:(NSString*)domain {
    NSString *str = [[request URL] absoluteString];
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", domain];
    return [pred evaluateWithObject:str];
}


#pragma mark - Mock responses


-(void) mockRequest:(NSURLRequest*)request data:(NSData*)data {
    id client = [self client];

    NSDictionary *headers = @{@"Access-Control-Allow-Origin" : @"*", @"Access-Control-Allow-Headers" : @"Content-Type"};
    NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:request.URL statusCode:200 HTTPVersion:@"1.1" headerFields:headers];

    [client URLProtocol:self didReceiveResponse:response
     cacheStoragePolicy:NSURLCacheStorageNotAllowed];
    [client URLProtocol:self didLoadData:data];
    [client URLProtocolDidFinishLoading:self];
}

@end

没什么特别的JS:

function loadJSONDoc()
{
  var url = "https://s3.amazonaws.com/youboox_recette/epub.json";

  $.ajax({
      url: url,
      dataType: 'json',
       contentType: "application/json",
      success: function(jsonData){
        alert('success');
        document.getElementById("myDiv").innerHTML='<p>'+$.param(jsonData)+'</p>';
      },
      error: function (request, status, error) {
        alert("failure :" + request.status );
      }
  });
}


Answer 2:

我不得不做类似的东西,前一段时间。

首先,我设法找到的代码,可以使UIWebViewDelegate赶上Ajax调用,所以在我的web应用程序的一部分我有:

//code to extend XMLHttpRequest, and have ajax call available in uiwebviewdelegate.

var s_ajaxListener = new Object();
s_ajaxListener.tempOpen = XMLHttpRequest.prototype.open;
s_ajaxListener.tempSend = XMLHttpRequest.prototype.send;
s_ajaxListener.callback = function () {
    window.location=this.url;
};

XMLHttpRequest.prototype.open = function(a,b) {
  if (!a) var a='';
  if (!b) var b='';
  s_ajaxListener.tempOpen.apply(this, arguments);
  s_ajaxListener.method = a;  
  s_ajaxListener.url = b;
  if (a.toLowerCase() == 'get') {
    s_ajaxListener.data = b.split('?');
    s_ajaxListener.data = s_ajaxListener.data[1];
  }
}

XMLHttpRequest.prototype.send = function(a,b) {
  if (!a) var a='';
  if (!b) var b='';
  s_ajaxListener.tempSend.apply(this, arguments);
  if(s_ajaxListener.method.toLowerCase() == 'post')s_ajaxListener.data = a;
  s_ajaxListener.callback();
}

然后在iOS中我在UIWebViewDelegate shouldStartLoad不归路(我的条件有点丑):

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([[[request URL] scheme] rangeOfString:@"https"].length > 0) 
    {
        return NO;
    }

    return YES;
}

在最重要的是我必须登记自己的协议:

[NSURLProtocol registerClass:[MyProtocol class]];

随着StartLoad实现。 你也应该有子类的canInitWithRequest

+ (BOOL)canInitWithRequest:(NSURLRequest *)request 
{
    if ([request.URL.scheme rangeOfString:@"https"].length > 0) 
    {
        return YES;
    }

    return NO;
}

要告诉大家,它应该是使用了该请求的协议。

而且不要忘了,当你有网络注销。

如果你不想与NSURLProtocol打扰实现,你可以用我自己的: https://github.com/bcharp/BOURLProtocol ;)

希望能帮助到你 !



文章来源: How to mock AJAX call with NSURLProtocol?