Skip to content

Commit

Permalink
Merge pull request #3 from wheresrhys/DRYer-Blob
Browse files Browse the repository at this point in the history
DRYer
  • Loading branch information
wheresrhys committed May 4, 2015
2 parents 096b91f + 3e3a045 commit aa54467
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 67 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ script: npm test
node_js:
- '0.12'
before_install:
- export CHROME_BIN=chromium-browser
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
- npm install -g bower
- bower install
before_deploy:
Expand Down
34 changes: 4 additions & 30 deletions client.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,10 @@
'use strict';

var Response = window.Response;
var Headers = window.Headers;

function mockResponse (url, config) {
// allow just body to be passed in as this is the commonest use case
if (typeof config === 'string' || !(config.body || config.headers || config.throws || config.status)) {
config = {
body: config
};
}
if (config.throws) {
return Promise.reject(config.throws);
}
var opts = config.opts || {};
opts.url = url;
opts.status = config.status || 200;
opts.headers = config.headers ? new Headers(config.headers) : new Headers();

if (config.body != null) {
var body = config.body;
if (typeof body === 'object') {
body = JSON.stringify(body);
}
}

return Promise.resolve(new Response(body, opts));
}

var FetchMock = require('./src/fetch-mock');

module.exports = new FetchMock({
mockResponse: mockResponse,
theGlobal: window
theGlobal: window,
Response: window.Response,
Headers: window.Headers,
Blob: window.Blob
});
22 changes: 18 additions & 4 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

module.exports = function(karma) {
karma.set({
var configuration = {

frameworks: [ 'mocha', 'chai', 'browserify'],
files: [
Expand All @@ -15,6 +15,20 @@ module.exports = function(karma) {
transform: ['debowerify'],
debug: true
},
browsers: ['PhantomJS'],
});
};
browsers: ['PhantomJS', 'Chrome'],
customLaunchers: {
Chrome_travis_ci: {
base: 'Chrome',
flags: ['--no-sandbox']
}
}
};

if(process.env.TRAVIS){
configuration.browsers = ['PhantomJS', 'Chrome_travis_ci'];
}

karma.set(configuration);

};

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"karma": "^0.12.31",
"karma-browserify": "^4.1.2",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^0.1.8",
"karma-mocha": "^0.1.10",
"karma-phantomjs-launcher": "^0.1.4",
"mocha": "^2.2.4",
Expand Down
35 changes: 4 additions & 31 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,11 @@
var Response = require('node-fetch').Response;
var Headers = require('node-fetch').Headers;
var stream = require('stream');

function mockResponse (url, config) {
// allow just body to be passed in as this is the commonest use case
if (typeof config === 'string' || !(config.body || config.headers || config.throws || config.status)) {
config = {
body: config
};
}
if (config.throws) {
return Promise.reject(config.throws);
}
var opts = config.opts || {};
opts.url = url;
opts.status = config.status || 200;
opts.headers = config.headers ? new Headers(config.headers) : new Headers();

var s = new stream.Readable();
if (config.body != null) {
var body = config.body;
if (typeof body === 'object') {
body = JSON.stringify(body);
}
s.push(body, 'utf-8');
}

s.push(null);
return Promise.resolve(new Response(s, opts));
}

var FetchMock = require('./src/fetch-mock');

module.exports = new FetchMock({
mockResponse: mockResponse,
theGlobal: GLOBAL
theGlobal: GLOBAL,
Response: Response,
Headers: Headers,
stream: stream
});
43 changes: 41 additions & 2 deletions src/fetch-mock.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
'use strict';

var sinon = require('sinon');
var mockResponse;
var Headers;
var Response;
var stream;
var Blob;
var theGlobal;


function mockResponse (url, config) {
// allow just body to be passed in as this is the commonest use case
if (typeof config === 'string' || !(config.body || config.headers || config.throws || config.status)) {
config = {
body: config
};
}
if (config.throws) {
return Promise.reject(config.throws);
}
var opts = config.opts || {};
opts.url = url;
opts.status = config.status || 200;
opts.headers = config.headers ? new Headers(config.headers) : new Headers();


var body = config.body;
if (config.body != null && typeof body === 'object') {
body = JSON.stringify(body);
}

if (stream) {
var s = new stream.Readable();
if (body != null) {
s.push(body, 'utf-8');
}
s.push(null);
body = s;
}
return Promise.resolve(new Response(body, opts));
}

function compileRoute (route) {
if (!route.name) {
throw 'each route must be named';
Expand Down Expand Up @@ -39,7 +75,10 @@ function compileRoute (route) {
}

var FetchMock = function (opts) {
mockResponse = opts.mockResponse;
Headers = opts.Headers;
Response = opts.Response;
stream = opts.stream;
Blob = opts.Blob;
theGlobal = opts.theGlobal;
this.routes = [];
this._calls = {};
Expand Down

0 comments on commit aa54467

Please sign in to comment.