11var assert = require ( 'assert' ) ,
22 sinon = require ( 'sinon' ) ,
33 rewire = require ( 'rewire' ) ,
4- events = require ( 'events' ) ;
4+ events = require ( 'events' ) ,
5+ EJSON = require ( 'meteor-ejson' ) ;
56
67var DDPClient = rewire ( "../lib/ddp-client" ) ;
78
@@ -109,23 +110,46 @@ describe("Network errors", function() {
109110
110111
111112describe ( 'EJSON' , function ( ) {
112- beforeEach ( function ( ) {
113- prepareMocks ( ) ;
114- } ) ;
115113
116- var DDPMessage = JSON . stringify (
117- { "msg" :"added" , "collection" :"posts" , "id" :"2trpvcQ4pn32ZYXco" , "fields" :{ "date" :{ "$date" :1371591394454 } } }
118- ) ;
114+ var DDPMessage = '{"msg":"added","collection":"posts","id":"2trpvcQ4pn32ZYXco","fields":{"date":{"$date":1371591394454},"bindata":{"$binary":"QUJDRA=="}}}' ;
115+ var EJSONObject = EJSON . parse ( DDPMessage ) ;
119116
120- it ( 'should not be endabled by default' , function ( done ) {
117+ it ( 'should not be enabled by default' , function ( done ) {
121118 var ddpclient = new DDPClient ( ) ;
122119
123120 assert ( ! ddpclient . use_ejson ) ;
124121
122+ done ( ) ;
123+ } ) ;
124+
125+ it ( 'should not be used when disabled' , function ( done ) {
126+ var ddpclient = new DDPClient ( { use_ejson : false } ) ;
127+
128+ assert ( ! ddpclient . use_ejson ) ;
129+
125130 ddpclient . _message ( DDPMessage ) ;
126131
132+ // ensure received dates not decoded from EJSON
127133 assert . deepEqual ( ddpclient . collections . posts [ '2trpvcQ4pn32ZYXco' ] . date , { "$date" :1371591394454 } ) ;
128134
135+ // ensure received binary data not decoded from EJSON date
136+ assert . deepEqual ( ddpclient . collections . posts [ '2trpvcQ4pn32ZYXco' ] . bindata , { "$binary" :"QUJDRA==" } ) ;
137+
138+ ddpclient . socket = { } ;
139+ ddpclient . socket . send = function ( opts ) {
140+ // ensure sent dates not encoded into EJSON
141+ assert ( opts . indexOf ( "date" ) !== - 1 ) ;
142+ assert ( opts . indexOf ( "$date" ) === - 1 ) ;
143+ assert ( opts . indexOf ( "1371591394454" ) === - 1 ) ;
144+
145+ // ensure sent binary data not encoded into EJSON
146+ assert ( opts . indexOf ( "bindata" ) !== - 1 ) ;
147+ assert ( opts . indexOf ( "$binary" ) === - 1 ) ;
148+ assert ( opts . indexOf ( "QUJDRA==" ) === - 1 ) ;
149+ } ;
150+
151+ ddpclient . _send ( EJSONObject . fields ) ;
152+
129153 done ( ) ;
130154 } ) ;
131155
@@ -138,7 +162,23 @@ describe('EJSON', function() {
138162
139163 assert . deepEqual ( ddpclient . collections . posts [ '2trpvcQ4pn32ZYXco' ] . date , new Date ( 1371591394454 ) ) ;
140164
165+ assert . deepEqual ( ddpclient . collections . posts [ '2trpvcQ4pn32ZYXco' ] . bindata , new Uint8Array ( [ 65 , 66 , 67 , 68 ] ) ) ;
166+
167+ ddpclient . socket = { } ;
168+ ddpclient . socket . send = function ( opts ) {
169+ assert ( opts . indexOf ( "date" ) !== - 1 ) ;
170+ assert ( opts . indexOf ( "$date" ) !== - 1 ) ;
171+ assert ( opts . indexOf ( "1371591394454" ) !== - 1 ) ;
172+
173+ assert ( opts . indexOf ( "bindata" ) !== - 1 ) ;
174+ assert ( opts . indexOf ( "$binary" ) !== - 1 ) ;
175+ assert ( opts . indexOf ( "QUJDRA==" ) !== - 1 ) ;
176+ } ;
177+
178+ ddpclient . _send ( EJSONObject . fields ) ;
179+
141180 done ( ) ;
142181 } ) ;
182+
143183} ) ;
144184
0 commit comments