@@ -2913,7 +2913,8 @@ public void TransportAllUsesLongPollingWhenServerOnlySupportLongPolling() {
2913
2913
}
2914
2914
assertTrue (close .blockingAwait (5 , TimeUnit .SECONDS ));
2915
2915
return Single .just (new HttpResponse (204 , "" , TestUtils .emptyByteBuffer ));
2916
- });
2916
+ })
2917
+ .on ("DELETE" , (req ) -> Single .just (new HttpResponse (200 , "" , TestUtils .stringToByteBuffer ("" ))));
2917
2918
2918
2919
HubConnection hubConnection = HubConnectionBuilder
2919
2920
.create ("http://example.com" )
@@ -2969,6 +2970,135 @@ public void ClientThatSelectsLongPollingThrowsWhenLongPollingIsNotAvailable() {
2969
2970
assertEquals (exception .getMessage (), "There were no compatible transports on the server." );
2970
2971
}
2971
2972
2973
+ @ Test
2974
+ public void LongPollingTransportAccessTokenProviderThrowsOnInitialPoll () {
2975
+ TestHttpClient client = new TestHttpClient ()
2976
+ .on ("POST" , (req ) -> {
2977
+ return Single .just (new HttpResponse (200 , "" , TestUtils .stringToByteBuffer ("" )));
2978
+ })
2979
+ .on ("POST" , "http://example.com/negotiate?negotiateVersion=1" ,
2980
+ (req ) -> Single .just (new HttpResponse (200 , "" ,
2981
+ TestUtils .stringToByteBuffer ("{\" connectionId\" :\" bVOiRPG8-6YiJ6d7ZcTOVQ\" ,\" "
2982
+ + "availableTransports\" :[{\" transport\" :\" LongPolling\" ,\" transferFormats\" :[\" Text\" ,\" Binary\" ]}]}" ))))
2983
+ .on ("GET" , (req ) -> {
2984
+ return Single .just (new HttpResponse (200 , "" , TestUtils .stringToByteBuffer ("{}" + RECORD_SEPARATOR )));
2985
+ });
2986
+
2987
+ AtomicInteger accessTokenCount = new AtomicInteger (0 );
2988
+ HubConnection hubConnection = HubConnectionBuilder
2989
+ .create ("http://example.com" )
2990
+ .withTransport (TransportEnum .LONG_POLLING )
2991
+ .withHttpClient (client )
2992
+ .withAccessTokenProvider (Single .defer (() -> {
2993
+ if (accessTokenCount .getAndIncrement () < 1 ) {
2994
+ return Single .just ("" );
2995
+ }
2996
+ return Single .error (new RuntimeException ("Error from accessTokenProvider" ));
2997
+ }))
2998
+ .build ();
2999
+
3000
+ try {
3001
+ hubConnection .start ().timeout (1 , TimeUnit .SECONDS ).blockingAwait ();
3002
+ assertTrue (false );
3003
+ } catch (RuntimeException ex ) {
3004
+ assertEquals ("Error from accessTokenProvider" , ex .getMessage ());
3005
+ }
3006
+ }
3007
+
3008
+ @ Test
3009
+ public void LongPollingTransportAccessTokenProviderThrowsAfterHandshakeClosesConnection () {
3010
+ AtomicInteger requestCount = new AtomicInteger (0 );
3011
+ CompletableSubject blockGet = CompletableSubject .create ();
3012
+ TestHttpClient client = new TestHttpClient ()
3013
+ .on ("POST" , "http://example.com/negotiate?negotiateVersion=1" ,
3014
+ (req ) -> Single .just (new HttpResponse (200 , "" ,
3015
+ TestUtils .stringToByteBuffer ("{\" connectionId\" :\" bVOiRPG8-6YiJ6d7ZcTOVQ\" ,\" "
3016
+ + "availableTransports\" :[{\" transport\" :\" LongPolling\" ,\" transferFormats\" :[\" Text\" ,\" Binary\" ]}]}" ))))
3017
+ .on ("GET" , (req ) -> {
3018
+ if (requestCount .getAndIncrement () > 1 ) {
3019
+ blockGet .blockingAwait ();
3020
+ }
3021
+ return Single .just (new HttpResponse (200 , "" , TestUtils .stringToByteBuffer ("{}" + RECORD_SEPARATOR )));
3022
+ })
3023
+ .on ("POST" , "http://example.com?id=bVOiRPG8-6YiJ6d7ZcTOVQ" , (req ) -> {
3024
+ return Single .just (new HttpResponse (200 , "" , TestUtils .stringToByteBuffer ("" )));
3025
+ });
3026
+
3027
+ AtomicInteger accessTokenCount = new AtomicInteger (0 );
3028
+ HubConnection hubConnection = HubConnectionBuilder
3029
+ .create ("http://example.com" )
3030
+ .withTransport (TransportEnum .LONG_POLLING )
3031
+ .withHttpClient (client )
3032
+ .withAccessTokenProvider (Single .defer (() -> {
3033
+ if (accessTokenCount .getAndIncrement () < 5 ) {
3034
+ return Single .just ("" );
3035
+ }
3036
+ return Single .error (new RuntimeException ("Error from accessTokenProvider" ));
3037
+ }))
3038
+ .build ();
3039
+
3040
+ CompletableSubject closed = CompletableSubject .create ();
3041
+ hubConnection .onClosed ((e ) -> {
3042
+ closed .onComplete ();
3043
+ });
3044
+
3045
+ hubConnection .start ().timeout (1 , TimeUnit .SECONDS ).blockingAwait ();
3046
+ blockGet .onComplete ();
3047
+
3048
+ closed .timeout (1 , TimeUnit .SECONDS ).blockingAwait ();
3049
+ assertEquals (HubConnectionState .DISCONNECTED , hubConnection .getConnectionState ());
3050
+ }
3051
+
3052
+ @ Test
3053
+ public void LongPollingTransportAccessTokenProviderThrowsDuringStop () {
3054
+ AtomicInteger requestCount = new AtomicInteger (0 );
3055
+ CompletableSubject blockGet = CompletableSubject .create ();
3056
+ TestHttpClient client = new TestHttpClient ()
3057
+ .on ("POST" , "http://example.com/negotiate?negotiateVersion=1" ,
3058
+ (req ) -> Single .just (new HttpResponse (200 , "" ,
3059
+ TestUtils .stringToByteBuffer ("{\" connectionId\" :\" bVOiRPG8-6YiJ6d7ZcTOVQ\" ,\" "
3060
+ + "availableTransports\" :[{\" transport\" :\" LongPolling\" ,\" transferFormats\" :[\" Text\" ,\" Binary\" ]}]}" ))))
3061
+ .on ("GET" , (req ) -> {
3062
+ if (requestCount .getAndIncrement () > 1 ) {
3063
+ blockGet .blockingAwait ();
3064
+ }
3065
+ return Single .just (new HttpResponse (200 , "" , TestUtils .stringToByteBuffer ("{}" + RECORD_SEPARATOR )));
3066
+ })
3067
+ .on ("POST" , "http://example.com?id=bVOiRPG8-6YiJ6d7ZcTOVQ" , (req ) -> {
3068
+ return Single .just (new HttpResponse (200 , "" , TestUtils .stringToByteBuffer ("" )));
3069
+ });
3070
+
3071
+ AtomicInteger accessTokenCount = new AtomicInteger (0 );
3072
+ HubConnection hubConnection = HubConnectionBuilder
3073
+ .create ("http://example.com" )
3074
+ .withTransport (TransportEnum .LONG_POLLING )
3075
+ .withHttpClient (client )
3076
+ .withAccessTokenProvider (Single .defer (() -> {
3077
+ if (accessTokenCount .getAndIncrement () < 5 ) {
3078
+ return Single .just ("" );
3079
+ }
3080
+ return Single .error (new RuntimeException ("Error from accessTokenProvider" ));
3081
+ }))
3082
+ .build ();
3083
+
3084
+ CompletableSubject closed = CompletableSubject .create ();
3085
+ hubConnection .onClosed ((e ) -> {
3086
+ closed .onComplete ();
3087
+ });
3088
+
3089
+ hubConnection .start ().timeout (1 , TimeUnit .SECONDS ).blockingAwait ();
3090
+
3091
+ try {
3092
+ hubConnection .stop ().timeout (1 , TimeUnit .SECONDS ).blockingAwait ();
3093
+ assertTrue (false );
3094
+ } catch (Exception ex ) {
3095
+ assertEquals ("Error from accessTokenProvider" , ex .getMessage ());
3096
+ }
3097
+ blockGet .onComplete ();
3098
+ closed .timeout (1 , TimeUnit .SECONDS ).blockingAwait ();
3099
+ assertEquals (HubConnectionState .DISCONNECTED , hubConnection .getConnectionState ());
3100
+ }
3101
+
2972
3102
@ Test
2973
3103
public void receivingServerSentEventsTransportFromNegotiateFails () {
2974
3104
TestHttpClient client = new TestHttpClient ().on ("POST" , "http://example.com/negotiate?negotiateVersion=1" ,
@@ -3265,6 +3395,21 @@ public void authorizationHeaderFromNegotiateGetsSetToNewValue() {
3265
3395
assertEquals ("Bearer secondRedirectToken" , token .get ());
3266
3396
}
3267
3397
3398
+ @ Test
3399
+ public void ErrorInAccessTokenProviderThrowsFromStart () {
3400
+ HubConnection hubConnection = HubConnectionBuilder
3401
+ .create ("http://example.com" )
3402
+ .withAccessTokenProvider (Single .defer (() -> Single .error (new RuntimeException ("Error from accessTokenProvider" ))))
3403
+ .build ();
3404
+
3405
+ try {
3406
+ hubConnection .start ().timeout (1 , TimeUnit .SECONDS ).blockingAwait ();
3407
+ assertTrue (false );
3408
+ } catch (RuntimeException ex ) {
3409
+ assertEquals ("Error from accessTokenProvider" , ex .getMessage ());
3410
+ }
3411
+ }
3412
+
3268
3413
@ Test
3269
3414
public void connectionTimesOutIfServerDoesNotSendMessage () {
3270
3415
HubConnection hubConnection = TestUtils .createHubConnection ("http://example.com" );
0 commit comments