• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.servlet;
18 
19 import io.grpc.ManagedChannelBuilder;
20 import io.grpc.ServerBuilder;
21 import io.grpc.netty.InternalNettyChannelBuilder;
22 import io.grpc.netty.NettyChannelBuilder;
23 import io.grpc.testing.integration.AbstractInteropTest;
24 import org.eclipse.jetty.http2.parser.RateControl;
25 import org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory;
26 import org.eclipse.jetty.server.HttpConfiguration;
27 import org.eclipse.jetty.server.Server;
28 import org.eclipse.jetty.server.ServerConnector;
29 import org.eclipse.jetty.servlet.ServletContextHandler;
30 import org.eclipse.jetty.servlet.ServletHolder;
31 import org.junit.After;
32 
33 public class JettyInteropTest extends AbstractInteropTest {
34 
35   private static final String HOST = "localhost";
36   private static final String MYAPP = "/grpc.testing.TestService";
37   private int port;
38   private Server server;
39 
40   @After
41   @Override
tearDown()42   public void tearDown() {
43     super.tearDown();
44     try {
45       server.stop();
46     } catch (Exception e) {
47       throw new AssertionError(e);
48     }
49   }
50 
51   @Override
getServerBuilder()52   protected ServerBuilder<?> getServerBuilder() {
53     return new ServletServerBuilder().maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE);
54   }
55 
56   @Override
startServer(ServerBuilder<?> builer)57   protected void startServer(ServerBuilder<?> builer) {
58     GrpcServlet grpcServlet =
59         new GrpcServlet(((ServletServerBuilder) builer).buildServletAdapter());
60     server = new Server(0);
61     ServerConnector sc = (ServerConnector)server.getConnectors()[0];
62     HTTP2CServerConnectionFactory factory =
63             new HTTP2CServerConnectionFactory(new HttpConfiguration());
64 
65     // Explicitly disable safeguards against malicious clients, as some unit tests trigger this
66     factory.setRateControlFactory(new RateControl.Factory() {});
67 
68     sc.addConnectionFactory(factory);
69     ServletContextHandler context =
70         new ServletContextHandler(ServletContextHandler.SESSIONS);
71     context.setContextPath(MYAPP);
72     context.addServlet(new ServletHolder(grpcServlet), "/*");
73     server.setHandler(context);
74 
75     try {
76       server.start();
77     } catch (Exception e) {
78       throw new AssertionError(e);
79     }
80 
81     port = sc.getLocalPort();
82   }
83 
84   @Override
createChannelBuilder()85   protected ManagedChannelBuilder<?> createChannelBuilder() {
86     NettyChannelBuilder builder = NettyChannelBuilder.forAddress(HOST, port)
87                                   .usePlaintext()
88                                   .maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE);
89     InternalNettyChannelBuilder.setStatsEnabled(builder, false);
90     builder.intercept(createCensusStatsClientInterceptor());
91     return builder;
92   }
93 }
94