Skip to content

Instantly share code, notes, and snippets.

@lukemelia
Created November 13, 2015 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukemelia/753d93a6c3332f98ead7 to your computer and use it in GitHub Desktop.
Save lukemelia/753d93a6c3332f98ead7 to your computer and use it in GitHub Desktop.
Bridge to mock an API being delivered via websockets using socket.io and hook it up to ember-cli-mirage
// Shared under MIT License by Collectrium
import _ from 'lodash/lodash';
export default {
setup(config = {}) {
/*
This method replaces window.io with a mock Socket.IO client
interface which translates socket API requests to XHR requests
that will be intercepted by Mirage. When the XHR request returns
we process the result and send it back through the fake Socket.IO
client, just as it would come back in the real app.
*/
const MockSocketIO = Ember.Object.extend(Ember.Evented, {
typeFromSocketAddress() {
// Example: https://api-ps.collectriumdev.com:443/collectable_type/
let addressSegments = this.address.split('/');
return addressSegments[addressSegments.length - 2];
},
httpMethodFromRequestType(requestType) {
if (requestType === 'READ' || requestType === 'READ_LIST') {
return 'GET';
} else if (requestType === 'UPDATE') {
return 'PUT';
}
throw new Error('MockSocketIO - Unhandled request type: ' + requestType);
},
requestPath(requestType, type, hash) {
let pluralizedType = Ember.String.pluralize(type);
let memberSegment = '';
let queryString = '';
if (requestType === 'READ') {
memberSegment = `/${hash.id}`;
queryString = '?' + Ember.$.param(_.omit(hash, 'id', 'request_id'));
} else if (requestType === 'READ_LIST') {
queryString = '?' + Ember.$.param(_.omit(hash, 'request_id'));
} else if (requestType === 'UPDATE') {
memberSegment = `/${hash[type].id}`;
}
return `/${pluralizedType}${memberSegment}${queryString}`;
},
requestBody(requestType, type, hash) {
if (requestType === 'UPDATE') {
return hash;
}
},
socketResponseFromAjaxResponse(ajaxResponseData, requestId) {
return _.merge(ajaxResponseData, {
request_id: requestId
});
},
emit(requestType, hash) {
const type = this.typeFromSocketAddress();
const httpMethod = this.httpMethodFromRequestType(requestType);
const requestPath = this.requestPath(requestType, type, hash);
let requestBody = this.requestBody(requestType, type, hash);
Ember.$.ajax({
method: httpMethod,
url: requestPath,
data: requestBody
}).then((data/*, textStatus, jqXHR*/) => {
let socketResponse = this.socketResponseFromAjaxResponse(data, hash.request_id);
this.trigger('message', socketResponse);
}, (jqXHR, textStatus, errorThrown) => {
console.log('MockSocketIO ajax error', jqXHR, textStatus, errorThrown);
});
}
});
const socketIO = window.io;
MockSocketIO.connect = function(address, options) {
const isMock = !config.resourcesToMock || config.resourcesToMock.find(function(resource) {
return address.match(resource);
});
return isMock ? MockSocketIO.create({address, options}) : socketIO.connect(address, options);
};
window.io = MockSocketIO;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment