@@ -47,6 +47,7 @@ describe('Subscription', function() {
47
47
} ;
48
48
var message = 'howdy' ;
49
49
var messageBuffer = new Buffer ( message ) . toString ( 'base64' ) ;
50
+ var messageBinary = new Buffer ( message ) . toString ( 'binary' ) ;
50
51
var messageObj = {
51
52
receivedMessages : [ {
52
53
ackId : 3 ,
@@ -61,6 +62,11 @@ describe('Subscription', function() {
61
62
data : message ,
62
63
id : 7
63
64
} ;
65
+ var expectedMessageAsBinary = {
66
+ ackId : 3 ,
67
+ data : messageBinary ,
68
+ id : 7
69
+ } ;
64
70
65
71
before ( function ( ) {
66
72
mockery . registerMock ( '../common/service-object.js' , FakeServiceObject ) ;
@@ -98,12 +104,14 @@ describe('Subscription', function() {
98
104
name : SUB_NAME ,
99
105
autoAck : true ,
100
106
interval : 100 ,
101
- maxInProgress : 3
107
+ maxInProgress : 3 ,
108
+ encoding : 'binary'
102
109
} ;
103
110
var sub = new Subscription ( PUBSUB , CONFIG ) ;
104
111
assert . strictEqual ( sub . autoAck , CONFIG . autoAck ) ;
105
112
assert . strictEqual ( sub . interval , CONFIG . interval ) ;
106
113
assert . strictEqual ( sub . maxInProgress , 3 ) ;
114
+ assert . strictEqual ( sub . encoding , CONFIG . encoding ) ;
107
115
} ) ;
108
116
109
117
it ( 'should be closed' , function ( ) {
@@ -134,6 +142,10 @@ describe('Subscription', function() {
134
142
assert . strictEqual ( subscription . paused , false ) ;
135
143
} ) ;
136
144
145
+ it ( 'should default encoding to utf-8 if not specified' , function ( ) {
146
+ assert . strictEqual ( subscription . encoding , 'utf-8' ) ;
147
+ } ) ;
148
+
137
149
it ( 'should create an iam object' , function ( ) {
138
150
assert . deepEqual ( subscription . iam . calledWith_ , [
139
151
PUBSUB ,
@@ -219,6 +231,12 @@ describe('Subscription', function() {
219
231
var msg = Subscription . formatMessage_ ( messageObj . receivedMessages [ 0 ] ) ;
220
232
assert . deepEqual ( msg , expectedMessage ) ;
221
233
} ) ;
234
+
235
+ it ( 'should decode buffer to specified encoding' , function ( ) {
236
+ var msg = Subscription
237
+ . formatMessage_ ( messageObj . receivedMessages [ 0 ] , 'binary' ) ;
238
+ assert . deepEqual ( msg , expectedMessageAsBinary ) ;
239
+ } ) ;
222
240
} ) ;
223
241
224
242
describe ( 'formatName_' , function ( ) {
@@ -473,6 +491,42 @@ describe('Subscription', function() {
473
491
subscription . pull ( { } , assert . ifError ) ;
474
492
} ) ;
475
493
494
+ describe ( 'with encoding option' , function ( ) {
495
+ var formatMessageMemo , encodingMemo , onFormatMessage ;
496
+ beforeEach ( function ( ) {
497
+ encodingMemo = subscription . encoding ;
498
+ formatMessageMemo = Subscription . formatMessage_ ;
499
+ Subscription . formatMessage_ = function ( msg , encoding ) {
500
+ onFormatMessage ( msg , encoding ) ;
501
+ return formatMessageMemo ( msg , encoding ) ;
502
+ } ;
503
+ } ) ;
504
+ afterEach ( function ( ) {
505
+ Subscription . formatMessage_ = formatMessageMemo ;
506
+ subscription . encoding = encodingMemo ;
507
+ } ) ;
508
+
509
+ it ( 'should call formatMessage_ with binary' , function ( done ) {
510
+ onFormatMessage = function ( msg , encoding ) {
511
+ assert . equal ( encoding , 'binary' ) ;
512
+ done ( ) ;
513
+ } ;
514
+ subscription . encoding = 'binary' ;
515
+
516
+ subscription . pull ( { } , assert . ifError ) ;
517
+ } ) ;
518
+
519
+ it ( 'should call formatMessage_ with utf-8' , function ( done ) {
520
+ onFormatMessage = function ( msg , encoding ) {
521
+ assert . equal ( encoding , 'utf-8' ) ;
522
+ done ( ) ;
523
+ } ;
524
+ subscription . encoding = 'utf-8' ;
525
+
526
+ subscription . pull ( { } , assert . ifError ) ;
527
+ } ) ;
528
+ } ) ;
529
+
476
530
describe ( 'autoAck false' , function ( ) {
477
531
beforeEach ( function ( ) {
478
532
subscription . autoAck = false ;
0 commit comments