@@ -527,4 +527,98 @@ public void canRegisterTypeAdaptorWithoutAffectingJsonProtocol() {
527527assertEquals (3 , (int )invocationMessage .getArguments ()[0 ]);
528528assertEquals ("four" ,invocationMessage .getArguments ()[1 ]);
529529 }
530+
531+ @ Test
532+ public void canParseInvocationMessageWithHeaders () {
533+ String stringifiedMessage ="{\" type\" :1,\" target\" :\" test\" ,\" arguments\" :[42],\" headers\" :{\" a\" :\" b\" ,\" c\" :\" d\" }}\u001E " ;
534+ ByteBuffer message =TestUtils .stringToByteBuffer (stringifiedMessage );
535+ TestBinder binder =new TestBinder (new Type [] {int .class },null );
536+
537+ List <HubMessage >messages =hubProtocol .parseMessages (message ,binder );
538+
539+ assertNotNull (messages );
540+ assertEquals (1 ,messages .size ());
541+
542+ assertEquals (HubMessageType .INVOCATION ,messages .get (0 ).getMessageType ());
543+ InvocationMessage invocationMessage = (InvocationMessage )messages .get (0 );
544+
545+ assertEquals ("test" ,invocationMessage .getTarget ());
546+ assertEquals (null ,invocationMessage .getInvocationId ());
547+ int messageResult = (int )invocationMessage .getArguments ()[0 ];
548+ assertEquals (42 ,messageResult );
549+ // Headers are parsed but not stored, so we just verify the message was processed successfully
550+ }
551+
552+ @ Test
553+ public void canParseInvocationMessageWithEmptyHeaders () {
554+ String stringifiedMessage ="{\" type\" :1,\" target\" :\" test\" ,\" arguments\" :[42],\" headers\" :{}}\u001E " ;
555+ ByteBuffer message =TestUtils .stringToByteBuffer (stringifiedMessage );
556+ TestBinder binder =new TestBinder (new Type [] {int .class },null );
557+
558+ List <HubMessage >messages =hubProtocol .parseMessages (message ,binder );
559+
560+ assertNotNull (messages );
561+ assertEquals (1 ,messages .size ());
562+
563+ assertEquals (HubMessageType .INVOCATION ,messages .get (0 ).getMessageType ());
564+ InvocationMessage invocationMessage = (InvocationMessage )messages .get (0 );
565+
566+ assertEquals ("test" ,invocationMessage .getTarget ());
567+ int messageResult = (int )invocationMessage .getArguments ()[0 ];
568+ assertEquals (42 ,messageResult );
569+ }
570+
571+ @ Test
572+ public void canParseCompletionMessageWithHeaders () {
573+ String stringifiedMessage ="{\" type\" :3,\" invocationId\" :\" 1\" ,\" result\" :42,\" headers\" :{\" a\" :\" b\" ,\" c\" :\" d\" }}\u001E " ;
574+ ByteBuffer message =TestUtils .stringToByteBuffer (stringifiedMessage );
575+ TestBinder binder =new TestBinder (null ,int .class );
576+
577+ List <HubMessage >messages =hubProtocol .parseMessages (message ,binder );
578+
579+ assertNotNull (messages );
580+ assertEquals (1 ,messages .size ());
581+
582+ assertEquals (HubMessageType .COMPLETION ,messages .get (0 ).getMessageType ());
583+ CompletionMessage completionMessage = (CompletionMessage )messages .get (0 );
584+ assertEquals ("1" ,completionMessage .getInvocationId ());
585+ assertEquals (42 ,completionMessage .getResult ());
586+ assertEquals (null ,completionMessage .getError ());
587+ }
588+
589+ @ Test
590+ public void canParseStreamItemMessageWithHeaders () {
591+ String stringifiedMessage ="{\" type\" :2,\" invocationId\" :\" 1\" ,\" item\" :\" test-item\" ,\" headers\" :{\" a\" :\" b\" }}\u001E " ;
592+ ByteBuffer message =TestUtils .stringToByteBuffer (stringifiedMessage );
593+ TestBinder binder =new TestBinder (null ,String .class );
594+
595+ List <HubMessage >messages =hubProtocol .parseMessages (message ,binder );
596+
597+ assertNotNull (messages );
598+ assertEquals (1 ,messages .size ());
599+
600+ assertEquals (HubMessageType .STREAM_ITEM ,messages .get (0 ).getMessageType ());
601+ StreamItem streamItem = (StreamItem )messages .get (0 );
602+ assertEquals ("1" ,streamItem .getInvocationId ());
603+ assertEquals ("test-item" ,streamItem .getItem ());
604+ }
605+
606+ @ Test
607+ public void canParseMessageWithHeadersInDifferentOrder () {
608+ String stringifiedMessage ="{\" headers\" :{\" First\" :\" value1\" ,\" Second\" :\" value2\" },\" type\" :1,\" target\" :\" test\" ,\" arguments\" :[42]}\u001E " ;
609+ ByteBuffer message =TestUtils .stringToByteBuffer (stringifiedMessage );
610+ TestBinder binder =new TestBinder (new Type [] {int .class },null );
611+
612+ List <HubMessage >messages =hubProtocol .parseMessages (message ,binder );
613+
614+ assertNotNull (messages );
615+ assertEquals (1 ,messages .size ());
616+
617+ assertEquals (HubMessageType .INVOCATION ,messages .get (0 ).getMessageType ());
618+ InvocationMessage invocationMessage = (InvocationMessage )messages .get (0 );
619+
620+ assertEquals ("test" ,invocationMessage .getTarget ());
621+ int messageResult = (int )invocationMessage .getArguments ()[0 ];
622+ assertEquals (42 ,messageResult );
623+ }
530624}