Unittesting $http in AngularJS gives error "No method expectGet" - unit-testing

I am having some issues to test $http in my AngularJS applications. I have tried to create the following example which fails for me:
ControllerJS:
var testController = ['$scope', '$http', function($scope, $http) {
$http.get('/test').success(function(data) {
//TODO
});
}];
ControllerTestJS:
describe("Test Controller", function () {
var scope, httpMock;
beforeEach(module('MailChimpApp'));
beforeEach(inject(function ($injector, $rootScope) {
httpMock = $injector.get('$httpBackend');
scope = $rootScope.$new();
}));
afterEach(function () {
httpMock.verifyNoOutstandingExpectation();
httpMock.verifyNoOutstandingRequest();
});
it('testing post', function () {
httpMock.expectGet('/test').respond([{ test: 'true' }]);
var controller = scope.$new('testController');
httpMock.flush();
});
});
When I run the test in Karma using Jasmin I get the following error:
TypeError: Object function $httpBackend(method, url, data, callback, headers) {
var xhr = new MockXhr(),
expectation = expectations[0],
wasExpected = false;
function prettyPrint(data) {
return (angular.isString(data) || angular.isFunction(data) || data instanceof RegExp)
? data
: angular.toJson(data);
}
if (expectation && expectation.match(method, url)) {
if (!expectation.matchData(data))
throw Error('Expected ' + expectation + ' with different data\n' +
'EXPECTED: ' + prettyPrint(expectation.data) + '\nGOT: ' + data);
if (!expectation.matchHeaders(headers))
throw Error('Expected ' + expectation + ' with different headers\n' +
'EXPECTED: ' + prettyPrint(expectation.headers) + '\nGOT: ' +
prettyPrint(headers));
expectations.shift();
if (expectation.response) {
responses.push(function() {
var response = expectation.response(method, url, data, headers);
xhr.$$respHeaders = response[2];
callback(response[0], response[1], xhr.getAllResponseHeaders());
});
return;
}
wasExpected = true;
}
var i = -1, definition;
while ((definition = definitions[++i])) {
if (definition.match(method, url, data, headers || {})) {
if (definition.response) {
// if $browser specified, we do auto flush all requests
($browser ? $browser.defer : responsesPush)(function() {
var response = definition.response(method, url, data, headers);
xhr.$$respHeaders = response[2];
callback(response[0], response[1], xhr.getAllResponseHeaders());
});
} else if (definition.passThrough) {
$delegate(method, url, data, callback, headers);
} else throw Error('No response defined !');
return;
}
}
throw wasExpected ?
Error('No response defined !') :
Error('Unexpected request: ' + method + ' ' + url + '\n' +
(expectation ? 'Expected ' + expectation : 'No more request expected'));
} has no method 'expectGet'
at null.<anonymous>
The error occurs when I try to do httpMock.expectGET. I have remembered to include angular-mocks.js in my karma config, which looks like this:
JASMINE,
JASMINE_ADAPTER,
'Scripts/jquery-1.10.2.min.js',
'Scripts/angular.js',
'Scripts/angular-mocks.js',
'Scripts/angular-resource.js',
'Scripts/App.js',
'Scripts/Controllers/*.js',
'Unittests/JavaScript/Controllers/*.js'
Are there anything I might be missing to use $httpBackend to mock my $http-requests?

The answer was to change the beforeEach to the following:
beforeEach(inject(function ($injector, $rootScope, $httpBackend) {
httpMock = $httpBackend;
scope = $rootScope.$new();
ctrl = $controller(testController, { $scope: scope });
}));

Be sure that module ngMock is included.
here I see only beforeEach(module('MailChimpApp'));

Related

How to choose different Lambda function while Start Streaming to Amazon Elasticsearch Service

Following this Streaming CloudWatch Logs Data to Amazon Elasticsearch Service, it's working fine to stream cloud watch log to ELK having one log group and one Lambda function.
But now I want to change target lambda function for my other logs group, but I am not able to do that as there is no option in AWS console.
Any Help will be appreciated.
Thanks
I was streaming to ELK using the AWS console option which is Start Streaming to Amazon Elasticsearch Service, But I failed to change or choose different lambda function as there is only lambda function can be selected for any log group using this option.
So, I create new lambda function and set stream target to AWS lambda function,
Here is the code that all you need, Node version for lambda function is 4.* as it was some issue with the new version but the pulse point is it does not require any extra NPM packages.
// v1.1.2
var https = require('https');
var zlib = require('zlib');
var crypto = require('crypto');
var endpoint = 'search-my-test.us-west-2.es.amazonaws.com';
exports.handler = function(input, context) {
// decode input from base64
var zippedInput = new Buffer(input.awslogs.data, 'base64');
// decompress the input
zlib.gunzip(zippedInput, function(error, buffer) {
if (error) { context.fail(error); return; }
// parse the input from JSON
var awslogsData = JSON.parse(buffer.toString('utf8'));
// transform the input to Elasticsearch documents
var elasticsearchBulkData = transform(awslogsData);
// skip control messages
if (!elasticsearchBulkData) {
console.log('Received a control message');
context.succeed('Control message handled successfully');
return;
}
// post documents to the Amazon Elasticsearch Service
post(elasticsearchBulkData, function(error, success, statusCode, failedItems) {
console.log('Response: ' + JSON.stringify({
"statusCode": statusCode
}));
if (error) {
console.log('Error: ' + JSON.stringify(error, null, 2));
if (failedItems && failedItems.length > 0) {
console.log("Failed Items: " +
JSON.stringify(failedItems, null, 2));
}
context.fail(JSON.stringify(error));
} else {
console.log('Success: ' + JSON.stringify(success));
context.succeed('Success');
}
});
});
};
function transform(payload) {
if (payload.messageType === 'CONTROL_MESSAGE') {
return null;
}
var bulkRequestBody = '';
payload.logEvents.forEach(function(logEvent) {
var timestamp = new Date(1 * logEvent.timestamp);
// index name format: cwl-YYYY.MM.DD
var indexName = [
'prod-background-wo-' + timestamp.getUTCFullYear(), // year
('0' + (timestamp.getUTCMonth() + 1)).slice(-2), // month
('0' + timestamp.getUTCDate()).slice(-2) // day
].join('.');
var source = buildSource(logEvent.message, logEvent.extractedFields);
source['response_time'] = source["end"] - source["start"];
source['#id'] = logEvent.id;
source['#timestamp'] = new Date(1 * logEvent.timestamp).toISOString();
source['#message'] = logEvent.message;
source['#owner'] = payload.owner;
source['#log_group'] = payload.logGroup;
source['#log_stream'] = payload.logStream;
var action = { "index": {} };
action.index._index = indexName;
action.index._type = payload.logGroup;
action.index._id = logEvent.id;
bulkRequestBody += [
JSON.stringify(action),
JSON.stringify(source),
].join('\n') + '\n';
});
return bulkRequestBody;
}
function buildSource(message, extractedFields) {
if (extractedFields) {
var source = {};
for (var key in extractedFields) {
if (extractedFields.hasOwnProperty(key) && extractedFields[key]) {
var value = extractedFields[key];
if (isNumeric(value)) {
source[key] = 1 * value;
continue;
}
jsonSubString = extractJson(value);
if (jsonSubString !== null) {
source['$' + key] = JSON.parse(jsonSubString);
}
source[key] = value;
}
}
return source;
}
jsonSubString = extractJson(message);
if (jsonSubString !== null) {
return JSON.parse(jsonSubString);
}
return {};
}
function extractJson(message) {
var jsonStart = message.indexOf('{');
if (jsonStart < 0) return null;
var jsonSubString = message.substring(jsonStart);
return isValidJson(jsonSubString) ? jsonSubString : null;
}
function isValidJson(message) {
try {
JSON.parse(message);
} catch (e) { return false; }
return true;
}
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function post(body, callback) {
var requestParams = buildRequest(endpoint, body);
var request = https.request(requestParams, function(response) {
var responseBody = '';
response.on('data', function(chunk) {
responseBody += chunk;
});
response.on('end', function() {
var info = JSON.parse(responseBody);
var failedItems;
var success;
if (response.statusCode >= 200 && response.statusCode < 299) {
failedItems = info.items.filter(function(x) {
return x.index.status >= 300;
});
success = {
"attemptedItems": info.items.length,
"successfulItems": info.items.length - failedItems.length,
"failedItems": failedItems.length
};
}
var error = response.statusCode !== 200 || info.errors === true ? {
"statusCode": response.statusCode,
"responseBody": responseBody
} : null;
callback(error, success, response.statusCode, failedItems);
});
}).on('error', function(e) {
callback(e);
});
request.end(requestParams.body);
}
function buildRequest(endpoint, body) {
var endpointParts = endpoint.match(/^([^\.]+)\.?([^\.]*)\.?([^\.]*)\.amazonaws\.com$/);
var region = endpointParts[2];
var service = endpointParts[3];
var datetime = (new Date()).toISOString().replace(/[:\-]|\.\d{3}/g, '');
var date = datetime.substr(0, 8);
var kDate = hmac('AWS4' + process.env.AWS_SECRET_ACCESS_KEY, date);
var kRegion = hmac(kDate, region);
var kService = hmac(kRegion, service);
var kSigning = hmac(kService, 'aws4_request');
var request = {
host: endpoint,
method: 'POST',
path: '/_bulk',
body: body,
headers: {
'Content-Type': 'application/json',
'Host': endpoint,
'Content-Length': Buffer.byteLength(body),
'X-Amz-Security-Token': process.env.AWS_SESSION_TOKEN,
'X-Amz-Date': datetime
}
};
var canonicalHeaders = Object.keys(request.headers)
.sort(function(a, b) { return a.toLowerCase() < b.toLowerCase() ? -1 : 1; })
.map(function(k) { return k.toLowerCase() + ':' + request.headers[k]; })
.join('\n');
var signedHeaders = Object.keys(request.headers)
.map(function(k) { return k.toLowerCase(); })
.sort()
.join(';');
var canonicalString = [
request.method,
request.path, '',
canonicalHeaders, '',
signedHeaders,
hash(request.body, 'hex'),
].join('\n');
var credentialString = [ date, region, service, 'aws4_request' ].join('/');
var stringToSign = [
'AWS4-HMAC-SHA256',
datetime,
credentialString,
hash(canonicalString, 'hex')
] .join('\n');
request.headers.Authorization = [
'AWS4-HMAC-SHA256 Credential=' + process.env.AWS_ACCESS_KEY_ID + '/' + credentialString,
'SignedHeaders=' + signedHeaders,
'Signature=' + hmac(kSigning, stringToSign, 'hex')
].join(', ');
return request;
}
function hmac(key, str, encoding) {
return crypto.createHmac('sha256', key).update(str, 'utf8').digest(encoding);
}
function hash(str, encoding) {
return crypto.createHash('sha256').update(str, 'utf8').digest(encoding);
}

InAppBrowser excutescript() method is not returning innerHtml code?

I was integrating the payU payment API in my Ionic app. I was able to get the payment success url of payU but the InAppBrowser excuteScript() method is not returning the innerHtml of the payment success url .
Please provide an relevant solution .
Please find my code below.
onDeviceReadyTest(url) {
console.log('url passed'+ url);
//let browser = new InAppBrowser(url,'_blank');
const browser = this.iab.create(url,'_blank');
browser.on('loadstart').subscribe(
(res) => {
// Handle url checking and body parsing here
console.log('event start with ' + res.url);
},
(error) => {
// Handle error here
console.log(error);
}
);
browser.on('loadstop').subscribe(
(res) => {
// Handle url checking and body parsing here
console.log('event stop with ' + res.url);
if(res.url.match("https://payu.herokuapp.com/success")) {
console.log('event match with ' + res.url);
//alert( browser.innerHTML);
browser.executeScript({code:"document.body.innerText"}),
function(values){
alert( values[ 0 ] );
var a = this.getValue(values[0], 'mihpayid');
var b = this.getValue(values[0], 'status');
var c = this.getValue(values[0], 'unmappedstatus');
console.log(a + b + c);//you can capture values from return
}
}
},
(error) => {
// Handle error here
console.log(error);
}
);
this.iabRef.addEventListener('loadstart', this.iabLoadStart);
this.iabRef.addEventListener('loadstop', this.iabLoadStop);
this.iabRef.addEventListener('loaderror', this.iabLoadError);
this.iabRef.addEventListener('exit', this.iabClose);
}`

How to enableMajor?

How could I enable Major versioning on "Pages" list? My code is not working and I don't get any errors. Any suggestions?....
_spBodyOnLoadFunctionNames.push(onPageLoad());
function onPageLoad() {
ExecuteOrDelayUntilScriptLoaded(enableMajor, 'SP.js')
}
function enableMajor() {
var ctx = new SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle('Pages');
ctx.load(list);
ctx.executeQueryAsync(
function () {
list.enableMajor = true;
},
function (sender, args) {
console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
);
}
UPDATE 06-15
====---------------
Major version will not set? i dont why it is not setted? Any suggestions?
<script>
var list;
function getPublishingPages(success, error) {
var ctx = SP.ClientContext.get_current();
list = ctx.get_web().get_lists().getByTitle('Pages');
var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
ctx.load(items, 'Include(File)');
list.set_e
ctx.executeQueryAsync(function () {
success(items);
},
error);
}
SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function () {
getPublishingPages(printPagesInfo, logError);
});
function printPagesInfo(pages) {
pages.get_data().forEach(function (item) {
var file = item.get_file();
var pageStatus = file.get_level() === SP.FileLevel.published ? 'published' : 'not published';
alert(String.format('Page {0} is {1}', file.get_name(), pageStatus));
list.set_enableVersioning(true);
list.update();
alert('Major versioning enabled');
});
}
function logError(sender, args) {
alert('An error occured: ' + args.get_message());
}
</script>
In order to enable Create major versions the following steps should be performed:
set SP.List.enableVersioning property to true
call SP.List.update Method to update the list
Example
function enableListVersioning(listTitle,success,error) {
var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle(listTitle);
list.set_enableVersioning(true);
list.update();
ctx.executeQueryAsync(
function () {
success();
},
error);
}
//usage
enableListVersioning('Pages',
function(){
console.log('Versioning is enabled');
},
function(sender,args){
console.log('An error occured: ' + args.get_message());
});

IE9: store.find is failing

I can't seem to fetch new data in Internet Explorer 9. For the purpose of an example I test the store this way:
App.__container__.lookup('store:main').find('style')
The only error I receive is the following:
SCRIPT5022: Error: Assertion Failed: [object Object]
Does Ember-data works out of the box (without polyfills, ...) in Internet Explorer 9?
versions:
Ember: 1.9.1
Ember-data: 1.0.0-beta.12
Problem solved. When doing an AJAX request with jQuery, this normally happens through the XMLHttpRequest object.
On IE8-9, this object is not present, instead it uses XDomainRequest. The simplest fix for this is adding: https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest.
ember-data works out of the box with IE8+. According to this issue:
We've been supporting IE8 with our platform (built on Ember) for a
while now. Things I know:
shim/sham is not needed, it's polyfilled by Ember and Ember-Data.
You will need it if you want additional things like .bind() on a function, then you must prepend it to the vendor file (using Brocfile)
and we only include the shim for that purpose, not the sham
Solution Synthesis
Reason :
On IE8-9, this object is not present, instead it uses XDomainRequest.
Solution :
The issue is solved. When using an AJAX request with jQuery. Normally this is done through the XMLHttpRequest object. A simple fix would be using the Open-Source jQuery-ajaxTransport-XDomainRequest
Code : Adding :
jQuery-ajaxTransport-XDomainRequest.js
/*!
* jQuery-ajaxTransport-XDomainRequest - v1.0.4 - 2015-03-05
* https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest
* Copyright (c) 2015 Jason Moon (#JSONMOON)
* Licensed MIT (/blob/master/LICENSE.txt)
*/
(function(factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as anonymous module.
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// CommonJS
module.exports = factory(require('jquery'));
} else {
// Browser globals.
factory(jQuery);
}
}(function($) {
// Only continue if we're on IE8/IE9 with jQuery 1.5+ (contains the ajaxTransport function)
if ($.support.cors || !$.ajaxTransport || !window.XDomainRequest) {
return $;
}
var httpRegEx = /^(https?:)?\/\//i;
var getOrPostRegEx = /^get|post$/i;
var sameSchemeRegEx = new RegExp('^(\/\/|' + location.protocol + ')', 'i');
// ajaxTransport exists in jQuery 1.5+
$.ajaxTransport('* text html xml json', function(options, userOptions, jqXHR) {
// Only continue if the request is: asynchronous, uses GET or POST method, has HTTP or HTTPS protocol, and has the same scheme as the calling page
if (!options.crossDomain || !options.async || !getOrPostRegEx.test(options.type) || !httpRegEx.test(options.url) || !sameSchemeRegEx.test(options.url)) {
return;
}
var xdr = null;
return {
send: function(headers, complete) {
var postData = '';
var userType = (userOptions.dataType || '').toLowerCase();
xdr = new XDomainRequest();
if (/^\d+$/.test(userOptions.timeout)) {
xdr.timeout = userOptions.timeout;
}
xdr.ontimeout = function() {
complete(500, 'timeout');
};
xdr.onload = function() {
var allResponseHeaders = 'Content-Length: ' + xdr.responseText.length + '\r\nContent-Type: ' + xdr.contentType;
var status = {
code: 200,
message: 'success'
};
var responses = {
text: xdr.responseText
};
try {
if (userType === 'html' || /text\/html/i.test(xdr.contentType)) {
responses.html = xdr.responseText;
} else if (userType === 'json' || (userType !== 'text' && /\/json/i.test(xdr.contentType))) {
try {
responses.json = $.parseJSON(xdr.responseText);
} catch(e) {
status.code = 500;
status.message = 'parseerror';
//throw 'Invalid JSON: ' + xdr.responseText;
}
} else if (userType === 'xml' || (userType !== 'text' && /\/xml/i.test(xdr.contentType))) {
var doc = new ActiveXObject('Microsoft.XMLDOM');
doc.async = false;
try {
doc.loadXML(xdr.responseText);
} catch(e) {
doc = undefined;
}
if (!doc || !doc.documentElement || doc.getElementsByTagName('parsererror').length) {
status.code = 500;
status.message = 'parseerror';
throw 'Invalid XML: ' + xdr.responseText;
}
responses.xml = doc;
}
} catch(parseMessage) {
throw parseMessage;
} finally {
complete(status.code, status.message, responses, allResponseHeaders);
}
};
// set an empty handler for 'onprogress' so requests don't get aborted
xdr.onprogress = function(){};
xdr.onerror = function() {
complete(500, 'error', {
text: xdr.responseText
});
};
if (userOptions.data) {
postData = ($.type(userOptions.data) === 'string') ? userOptions.data : $.param(userOptions.data);
}
xdr.open(options.type, options.url);
xdr.send(postData);
},
abort: function() {
if (xdr) {
xdr.abort();
}
}
};
});
return $;
}));

extending EmberDefaultResolver with Ember-App-Kit

I'm making a custom resolver based on the pattern below from Robin Ward [ video / 15sec]
which is a trick to have a mobile device look for "mob_template.hbs" first before loading "template.hbs"
App.Resolver = EmberDefaultResolver.extend({
resolveTemplate: function(parsedName){
var t = this._super(parsedName);
if App.mobileActive){
return this._super('mob_' + parsedName) || t;
}
return t;
}
});
However I'm using Ember App Kit, which uses a special version of the resolver:
I can't really tell what's going on in there or what I would need to do to produce similar functionality. Anyone have any idea?
I've tried something like this but it results in nothing being resolved:
var App = Ember.Application.extend({
//...
Resolver: Ember.DefaultResolver.extend({
resolve: function(fullName) {
var parsedName = this.parseName(fullName),
resolveMethodName = parsedName.resolveMethodName;
if (!(parsedName.name && parsedName.type)) {
throw new TypeError("Invalid fullName: `" + fullName + "`, must be of the form `type:name` ");
}
if (this[resolveMethodName]) {
if (window.screen_type == 'mobile'){
var resolved = this[resolveMethodName](parsedName + '_mobile');
} else{
var resolved = this[resolveMethodName](parsedName);
}
if (resolved) { return resolved; }
}
return this.resolveOther(parsedName);
},
})
});
Apparently parsedName is not a string of the template name in the EAK resolver, it has some props representing the template name though, parsedName.fullNameWithoutType being the one to target:
var CustomResolver = Resolver.extend({
resolveTemplate: function(parsedName){
var resolve = this._super(parsedName);
if (['foo','bar'].indexOf(window.special_prop) > -1){
var orig__parsedName_name = parsedName.name;
parsedName.name = parsedName.name + '_' + window.special_prop;
parsedName.fullName = parsedName.fullName + '_' + window.special_prop;
parsedName.fullNameWithoutType = parsedName.fullNameWithoutType + '_' + window.special_prop;
resolve = this._super(parsedName) || resolve;
}
return resolve;
} });
var App = Ember.Application.extend({ //... Resolver: CustomResolver });