Is there an easy way to whitelist HTTP requests with ngMockE2E

ⅰ亾dé卋堺 提交于 2019-11-30 09:21:10

问题


Due to some infrastructure changes (namely servers & VPNs) there are times I want to run our application in an offline mode. I've been able to implement this with ngMockE2E however it seems to be an all or nothing approach, meaning you have to explicitly set every single HTTP request out of the app.

Is there a way to have it assume that unless you have explicitly set a route/url to be handled that it will automatically call a generic passThrough() operation?

Currently I am doing this:

noSrvc = $location.search().hasOwnProperty 'nosrvc'

#
# templates
#
$httpBackend.whenGET /(\.htm|\.html)$/
.passThrough();

#
# session
#
rqst = $httpBackend.whenGET /(api\/users\/current)$/
if !noSrvc then rqst.passThrough() else rqst.respond {"user":{

# doing something similar for every single service call in the app... gets tedious after about 3-4

Most everything I've read on the subject deals with unit testing and doesn't really address the implied passthrough unless otherwise stated.


回答1:


That's the recipe I've used for whitelisting

app.run(function ($httpBackend) {
    // mocked requests, should come first
    $httpBackend.when('GET', 'mock').respond(200, {});

    // whitelisted real requests, should come last
    angular.forEach(['GET', 'DELETE', 'JSONP', 'HEAD', 'PUT', 'POST', 'PATCH'], function (method) {
        $httpBackend.when(method).passThrough();
    });
});

And I'm quite sure that precedence matters here.



来源:https://stackoverflow.com/questions/33245499/is-there-an-easy-way-to-whitelist-http-requests-with-ngmocke2e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!