Skip to content

Commit

Permalink
Small fixes; Added build files and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeroto committed Jul 24, 2014
1 parent e1509f4 commit 0741aba
Show file tree
Hide file tree
Showing 11 changed files with 340 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bower_components
node_modules
29 changes: 29 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "z-sails",
"version": "0.0.0",
"homepage": "https://github.com/Zeroto/z-sails",
"authors": [
"Sander Homan <[email protected]>"
],
"description": "A $httpBackend decorator to use sails socket requests",
"main": "dist/z-sails.min.js",
"keywords": [
"Angular",
"Sails",
"$http"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"angular": "~1.2.20"
},
"devDependencies": {
"angular-mocks": "~1.2.20"
}
}
Empty file removed dist/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions dist/maps/z-sails.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions dist/z-sails.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Created by Zerot on 7/23/14.
*/

(function(module){

module.provider('z-sails', [function(){
var self = this;
self.useFileCheck = true;
self.useFallback = false;

this.$get = function(){
return {
useFileCheck: self.useFileCheck,
useFallback: self.useFallback
};
};
}]);
module.config(['$provide', function($provide){

// replace $httpBackend to have it put out sails socket requests instead of XHR
$provide.decorator('$httpBackend', ['$delegate', '$browser', 'z-sails', '$window', function($delegate, $browser, zsails, $window){
var $httpBackend = function(method, url, post, callback, headers, timeout, withCredentials, responseType){
$browser.$$incOutstandingRequestCount();
url = url || $browser.url();

var lowercaseUrl;
if (zsails.useFileCheck)
lowercaseUrl = angular.lowercase(url);

var methodLowercase = angular.lowercase(method);
if ((methodLowercase !== 'get' && methodLowercase !== 'post' && methodLowercase !== 'put' && methodLowercase !== 'delete') ||
(zsails.useFileCheck && lowercaseUrl.length > 5 && (lowercaseUrl[lowercaseUrl.length-5] == '.' || lowercaseUrl[lowercaseUrl.length-4] == '.'))) //check if file. files will have a . at 3rd or 4th last character
{
return $delegate(method, url, post, callback, headers, timeout, withCredentials, responseType);
}
else
{
$window.io.socket[methodLowercase](url, angular.fromJson(post), function(data, jwr){
console.log(jwr);
if (zsails.useFallback && jwr.statusCode != 200)
{
$delegate(method, url, post, callback, headers, timeout, withCredentials, responseType);
}
else
callback(jwr.statusCode, data, jwr.headers, "");
});
}

};

$httpBackend.originalBackend = $delegate; // expose the original backend

return $httpBackend;
}]);

}]);

})(angular.module('z-sails', []));
2 changes: 2 additions & 0 deletions dist/z-sails.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var gulp = require('gulp');

var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var concat = require('gulp-concat');
var karma = require('gulp-karma');
var sourcemaps = require('gulp-sourcemaps');
var del = require('del');

var paths = {
scripts: ['src/**/*.js'],
};

gulp.task('clean', function(cb) {
// You can use multiple globbing patterns as you would with `gulp.src`
del(['dist'], cb);
});

gulp.task('scripts', ['clean'], function() {
// Minify and copy all JavaScript
// with sourcemaps all the way down
return gulp.src(paths.scripts)
.pipe(sourcemaps.init())
.pipe(concat('z-sails.js'))
.pipe(gulp.dest('dist'))
.pipe(rename('z-sails.min.js'))
.pipe(uglify())
.pipe(sourcemaps.write('maps'))
.pipe(gulp.dest('dist'));
});

gulp.task('test', function(){
return gulp.src('foobar') // invalid name so karma still loads from config
.pipe(karma({
configFile: 'karma.conf.js',
action: 'run'
}));
});

gulp.task('build', ['scripts']);
68 changes: 68 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Karma configuration
// Generated on Thu Jul 24 2014 14:07:10 GMT+0200 (W. Europe Daylight Time)

module.exports = function(config) {
config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],


// list of files / patterns to load in the browser
files: [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'dist/z-sails.min.js',
'test/**/*.js'
],


// list of files to exclude
exclude: [
],


// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},


// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],


// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,


// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,


// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],


// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "z-sails",
"version": "0.0.0",
"description": "A $httpBackend decorator to use sails socket requests instead of XHR",
"main": "dist/z-sails.js",
"directories": {
"test": "test"
},
"repository": {
"type": "git",
"url": "git://github.com/Zeroto/z-sails.git"
},
"keywords": [
"Angular",
"Sails",
"$http"
],
"author": "Sander Homan",
"license": "MIT",
"bugs": {
"url": "https://github.com/Zeroto/z-sails/issues"
},
"homepage": "https://github.com/Zeroto/z-sails",
"devDependencies": {
"gulp": "^3.8.6",
"gulp-concat": "^2.3.4",
"gulp-uglify": "^0.3.1",
"gulp-sourcemaps": "^1.1.0",
"del": "^0.1.1",
"gulp-rename": "^1.2.0",
"karma-jasmine": "^0.1.5",
"gulp-karma": "0.0.4",
"gulp-jasmine": "^0.2.0",
"gulp-jshint": "^1.7.1",
"karma": "^0.12.17",
"karma-chrome-launcher": "^0.1.4"
}
}
12 changes: 7 additions & 5 deletions src/z-sails.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
module.config(['$provide', function($provide){

// replace $httpBackend to have it put out sails socket requests instead of XHR
$provide.decorator('$httpBackend', ['$delegate', '$browser', 'z-sails', function($delegate, $browser, zsails){
$provide.decorator('$httpBackend', ['$delegate', '$browser', 'z-sails', '$window', function($delegate, $browser, zsails, $window){
var $httpBackend = function(method, url, post, callback, headers, timeout, withCredentials, responseType){
$browser.$$incOutstandingRequestCount();
url = url || $browser.url();
Expand All @@ -28,15 +28,15 @@
if (zsails.useFileCheck)
lowercaseUrl = angular.lowercase(url);

method = angular.lowercase(method);
if ((method !== 'get' && method !== 'post' && method !== 'put' && method !== 'delete') ||
var methodLowercase = angular.lowercase(method);
if ((methodLowercase !== 'get' && methodLowercase !== 'post' && methodLowercase !== 'put' && methodLowercase !== 'delete') ||
(zsails.useFileCheck && lowercaseUrl.length > 5 && (lowercaseUrl[lowercaseUrl.length-5] == '.' || lowercaseUrl[lowercaseUrl.length-4] == '.'))) //check if file. files will have a . at 3rd or 4th last character
{
return $delegate(method, url, post, callback, headers, timeout, withCredentials, responseType);
}
else
{
io.socket[method](url, angular.fromJson(post), function(data, jwr){
$window.io.socket[methodLowercase](url, angular.fromJson(post), function(data, jwr){
console.log(jwr);
if (zsails.useFallback && jwr.statusCode != 200)
{
Expand All @@ -48,7 +48,9 @@
}

};


$httpBackend.originalBackend = $delegate; // expose the original backend

return $httpBackend;
}]);

Expand Down
94 changes: 94 additions & 0 deletions test/z-sails.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
describe("z-sails", function(){

beforeEach(module('z-sails'));

var $http, zSails, $httpBackend, $window, socket, $rootScope;
beforeEach(inject(['$injector', function($injector){
$http = $injector.get('$http');
$httpBackend = $injector.get('$httpBackend').originalBackend;
$window = $injector.get('$window');
$rootScope = $injector.get('$rootScope');

// set up the mock socket
socket = {
get: function(_, _, cb){cb(null, {statusCode: 404, headers: null})},
post: function(_, _, cb){cb(null, {statusCode: 404, headers: null})},
put: function(_, _, cb){cb(null, {statusCode: 404, headers: null})},
delete: function(_, _, cb){cb(null, {statusCode: 404, headers: null})},
}



$window.io = {
socket: socket
};
}]));

beforeEach(inject(['$injector', function($injector){
zSails = $injector.get('z-sails');
}]));

afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});

it ('should use XHR when requesting a file when using the filecheck strategy', function(){

zSails.useFileCheck = true;
zSails.useFallback = false;

$httpBackend.expectGET('/template.html').respond(200, '');
$http({url: '/template.html', method: 'GET'});

$httpBackend.flush();
});

it ('should use sockets when requesting a resource when using the filecheck strategy', function(){

zSails.useFileCheck = true;
zSails.useFallback = false;

spyOn(socket, "get").andCallThrough();

$http({url: '/resource', method: 'GET'}).finally(function(){
expect(socket.get).toHaveBeenCalled();
});

$rootScope.$digest();
});

it ('should use sockets and XHR when using the fallback strategy', function(){

zSails.useFileCheck = false;
zSails.useFallback = true;

spyOn(socket, "get").andCallThrough();

$httpBackend.expectGET('/template.html').respond(200, '');

$http({url: '/template.html', method: 'GET'}).finally(function(){
expect(socket.get).toHaveBeenCalled();
});

$httpBackend.flush();
$rootScope.$digest();
});

it ('should use sockets and XHR when using both strategies when requesting a resource', function(){

zSails.useFileCheck = true;
zSails.useFallback = true;

spyOn(socket, "get").andCallThrough();

$httpBackend.expectGET('/resource').respond(200, '');

$http({url: '/resource', method: 'GET'}).finally(function(){
expect(socket.get).toHaveBeenCalled();
});

$httpBackend.flush();
$rootScope.$digest();
});
});

0 comments on commit 0741aba

Please sign in to comment.