2121import static org .mockito .Mockito .verify ;
2222import static org .mockito .Mockito .when ;
2323
24+ import com .google .protobuf .Message ;
2425import com .tencent .trpc .core .common .ConfigManager ;
2526import com .tencent .trpc .core .common .config .ProviderConfig ;
2627import com .tencent .trpc .core .exception .ErrorCode ;
4546import java .lang .reflect .ParameterizedType ;
4647import java .lang .reflect .Type ;
4748import java .util .Collections ;
49+ import java .util .Enumeration ;
4850import java .util .HashMap ;
4951import java .util .Map ;
5052import java .util .concurrent .CompletableFuture ;
@@ -687,8 +689,95 @@ public void testExecuteWithTransInfo() throws Exception {
687689 verify (response ).setStatus (HttpStatus .SC_OK );
688690 }
689691
692+ // ==================== execute catch branch ====================
693+
694+ @ Test
695+ public void testExecuteCatchBranch () throws Exception {
696+ ProviderInvoker <?> invoker = mock (ProviderInvoker .class );
697+ ProviderConfig config = mockProviderConfig (0 );
698+ when (invoker .getConfig ()).thenReturn (config );
699+
700+ HttpServletRequest request = mockRequest ();
701+ HttpServletResponse response = mock (HttpServletResponse .class );
702+
703+ Method method = BadService .class .getMethod ("hello" , String .class );
704+ RpcMethodInfo methodInfo = new RpcMethodInfo (BadService .class , method );
705+ RpcMethodInfoAndInvoker methodInfoAndInvoker = new RpcMethodInfoAndInvoker ();
706+ methodInfoAndInvoker .setMethodInfo (methodInfo );
707+ methodInfoAndInvoker .setInvoker (invoker );
708+
709+ AbstractHttpExecutor executor = createExecutorWithInvoker (methodInfoAndInvoker );
710+
711+ executor .execute (request , response , methodInfoAndInvoker );
712+
713+ verify (response ).setStatus (HttpStatus .SC_SERVICE_UNAVAILABLE );
714+ }
715+
716+ // ==================== parseRpcParams Protobuf ====================
717+
718+ @ Test
719+ public void testParseRpcParamsProtobuf () throws Exception {
720+ HttpServletRequest request = mock (HttpServletRequest .class );
721+ RpcMethodInfo methodInfo = mock (RpcMethodInfo .class );
722+ when (methodInfo .getParamsTypes ()).thenReturn (
723+ new Type []{RpcContext .class , tests .service .HelloRequestProtocol .HelloRequest .class });
724+
725+ AbstractHttpExecutor executor = createExecutorWithCodec ();
726+ HttpCodec httpCodec = (HttpCodec ) getField (executor , "httpCodec" );
727+ tests .service .HelloRequestProtocol .HelloRequest mockMsg =
728+ tests .service .HelloRequestProtocol .HelloRequest .getDefaultInstance ();
729+ when (httpCodec .convertToPBParam (any (), any ())).thenReturn (mockMsg );
730+
731+ Object [] result = (Object []) invokePrivate (executor , "parseRpcParams" ,
732+ new Class []{HttpServletRequest .class , RpcMethodInfo .class }, request , methodInfo );
733+
734+ assertNotNull (result );
735+ assertEquals (mockMsg , result [0 ]);
736+ }
737+
738+ // ==================== setRpcServerContext header loop ====================
739+
740+ @ Test
741+ public void testExecuteWithHeaders () throws Exception {
742+ ProviderInvoker <?> invoker = mock (ProviderInvoker .class );
743+ ProviderConfig config = mockProviderConfig (0 );
744+ when (invoker .getConfig ()).thenReturn (config );
745+
746+ DefResponse successResponse = new DefResponse ();
747+ successResponse .setValue ("ok" );
748+ when (invoker .invoke (any ())).thenReturn (CompletableFuture .completedFuture (successResponse ));
749+
750+ HttpServletRequest request = mock (HttpServletRequest .class );
751+ when (request .getAttribute (HttpConstants .REQUEST_ATTRIBUTE_TRPC_SERVICE )).thenReturn (TEST_SERVICE );
752+ when (request .getAttribute (HttpConstants .REQUEST_ATTRIBUTE_TRPC_METHOD )).thenReturn (TEST_METHOD );
753+ when (request .getRemoteAddr ()).thenReturn (TEST_IP );
754+ when (request .getRemotePort ()).thenReturn (TEST_PORT );
755+ when (request .getMethod ()).thenReturn ("POST" );
756+ when (request .getRequestURI ()).thenReturn ("/api/test" );
757+ Enumeration <String > headerNames = Collections .enumeration (Collections .singletonList ("X-Custom" ));
758+ when (request .getHeaderNames ()).thenReturn (headerNames );
759+ when (request .getHeader ("X-Custom" )).thenReturn ("custom-value" );
760+ HttpServletResponse response = mock (HttpServletResponse .class );
761+
762+ RpcMethodInfoAndInvoker methodInfoAndInvoker = buildMethodInfoAndInvoker (invoker );
763+ AbstractHttpExecutor executor = createExecutorWithInvoker (methodInfoAndInvoker );
764+ HttpCodec httpCodec = (HttpCodec ) getField (executor , "httpCodec" );
765+ when (httpCodec .convertToJavaBean (any (), any ())).thenReturn ("param" );
766+
767+ executor .execute (request , response , methodInfoAndInvoker );
768+
769+ verify (response ).setStatus (HttpStatus .SC_OK );
770+ }
771+
772+ // ==================== TestService ====================
773+
690774 private interface TestService {
691775
692776 String hello (RpcContext ctx , String req );
693777 }
778+
779+ private interface BadService {
780+
781+ String hello (String req );
782+ }
694783}
0 commit comments