http-backend-proxy
http-backend-proxy copied to clipboard
Unable to override [requestHandler] as per angular documentation
Hi there,
I'm having a bit of difficulty defining a proxy to selectively fail a test on demand, in my E2E protractor testing...
Just for reference, this is how I instantiate the proxy module at the beginning of my tests...
beforeAll(function(){
// Set initial window dimensions
var width = 1280;
var height = 780;
browser.driver.manage().window().setSize(width, height);
// Schedule to attach the mock $httpBackend when app starts
browser.addMockModule('app', function () {
angular.module('app').requires.push('ngMockE2E');
});
// Set global to allow using proxy throughout tests
var proxy = global.proxy = new HttpBackend(browser);
// Allow all GET requests before page loads to allow assets to load
proxy.onLoad.when('GET', /.*/).passThrough();
browser.get(browser.baseUrl).then(function(){
browser.executeScript("localStorage.clear();");
browser.executeScript("config.viewPath = 'views/';");
browser.executeScript("config.version = 'v-0.0.0.0';");
browser.executeScript("window.requestCount = 0;");
browser.executeScript("window.responseCount = 0;");
browser.executeScript("window.lastResponses = [];");
// Returns the config object for use in testing
browser.executeScript("return config").then(function(config){
global.config = config;
});
proxy.onLoad.reset();
});
});
According to the docs over at https://docs.angularjs.org/api/ngMockE2E/service/$httpBackend
...I should be able to do the following to allow me to ad-hoc 'override' an endpoint to allow me to force a failure, then reset that override to allow a normal API response in further tests...
var httpHandler = proxy.whenPUT(/\/register$/);
httpHandler.respond(500);
helper.button.manualBarcode();
helper.trigger.keypad(helper.setup.key());
expect($('.pz-dialog.alert-dialog').isPresent()).toBeTruthy();
httpHandler.passThrough();
I can't for the life of me get this to work. The initial httpHandler.respond(500) sets up the failure response perfectly fine but the second call to httpHandler.passThrough() has no effect. I expect it to effectively cancel the override. The test that runs after this block of code (that expects a 200 response from the API) is still getting the 500 error response from the proxy.
I've got to a point now where I just don't think it's possible without doing a full browser.get() for each test (which would be a massive pain due to the way we have the tests written, but alas, if it's the only way I might just have to go down that approach).
I know I can define a context for each test, then have the initial handler interpret the data and respond accordingly, but it doesn't seem that I can selectively passThrough() these requests and respond with fake data for others. It also isn't ideal as the initial setup needs to know about all the tests that follow; something which doesn't sit right with me.
Fundamentally, I just want to be able to selectively fail a HTTP request on-demand but having this proxy module work as required would be far more flexible. I've thought about creating a HTTP intercept service and injecting that at runtime and injecting a variable using browser.executeScript to change the behaviour when required but that's a last resort at the minute.
Any help would be highly appreciated! Thanks
This directly relates to pull request #10 which was never completed. The functionality you are trying to use was introduced into $httpBackend in 1.3.x which is after I wrote the proxy. It never got rolled into the proxy. I have my eye on it to implement this as a few people are asking for it now, but I don't see me getting to it till fall. You are of course welcome to have a go at it yourself and send in a PR. :)
Thanks for getting back to me so fast Ken. It would seem I have no other options so I'm definitely going to be working on that pull request today! I'm happy I now have a clear solution at least :) :+1:
@kbaltrinic Just FYI, it's quite obvious now that I didn't get round to doing that pull request. We decided to ditch protractor and keep our bespoke jasmine E2E tests instead. Should I close this or leave it open, as it's still a valid issue?
Please leave it open. Thanks.