1 /* 2 * Copyright 2022 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 static com.google.common.truth.Truth.assertThat; 20 21 import com.google.common.collect.ImmutableList; 22 import com.google.protobuf.ByteString; 23 import io.grpc.BindableService; 24 import io.grpc.Channel; 25 import io.grpc.ManagedChannelBuilder; 26 import io.grpc.testing.GrpcCleanupRule; 27 import io.grpc.testing.integration.Messages.Payload; 28 import io.grpc.testing.integration.Messages.SimpleRequest; 29 import io.grpc.testing.integration.Messages.SimpleResponse; 30 import io.grpc.testing.integration.TestServiceGrpc; 31 import io.grpc.testing.integration.TestServiceImpl; 32 import java.util.concurrent.Executors; 33 import java.util.concurrent.ScheduledExecutorService; 34 import org.eclipse.jetty.client.HttpClient; 35 import org.eclipse.jetty.client.api.ContentResponse; 36 import org.eclipse.jetty.http2.parser.RateControl; 37 import org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory; 38 import org.eclipse.jetty.server.HttpConfiguration; 39 import org.eclipse.jetty.server.Server; 40 import org.eclipse.jetty.server.ServerConnector; 41 import org.eclipse.jetty.servlet.ServletContextHandler; 42 import org.eclipse.jetty.servlet.ServletHolder; 43 import org.junit.After; 44 import org.junit.Before; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 import org.junit.runners.JUnit4; 48 49 /** Smoke test for {@link GrpcServlet}. */ 50 @RunWith(JUnit4.class) 51 public class GrpcServletSmokeTest { 52 private static final String HOST = "localhost"; 53 private static final String MYAPP = "/grpc.testing.TestService"; 54 55 public final GrpcCleanupRule cleanupRule = new GrpcCleanupRule(); 56 private final ScheduledExecutorService scheduledExecutorService = 57 Executors.newSingleThreadScheduledExecutor(); 58 private int port; 59 private Server server; 60 61 @Before startServer()62 public void startServer() { 63 BindableService service = new TestServiceImpl(scheduledExecutorService); 64 GrpcServlet grpcServlet = new GrpcServlet(ImmutableList.of(service)); 65 server = new Server(0); 66 ServerConnector sc = (ServerConnector)server.getConnectors()[0]; 67 HTTP2CServerConnectionFactory factory = 68 new HTTP2CServerConnectionFactory(new HttpConfiguration()); 69 70 // Explicitly disable safeguards against malicious clients, as some unit tests trigger this 71 factory.setRateControlFactory(new RateControl.Factory() {}); 72 73 sc.addConnectionFactory(factory); 74 ServletContextHandler context = 75 new ServletContextHandler(ServletContextHandler.SESSIONS); 76 context.setContextPath(MYAPP); 77 context.addServlet(new ServletHolder(grpcServlet), "/*"); 78 server.setHandler(context); 79 80 try { 81 server.start(); 82 } catch (Exception e) { 83 throw new AssertionError(e); 84 } 85 86 port = sc.getLocalPort(); 87 } 88 89 @After tearDown()90 public void tearDown() { 91 scheduledExecutorService.shutdown(); 92 try { 93 server.stop(); 94 } catch (Exception e) { 95 throw new AssertionError(e); 96 } 97 } 98 99 @Test unaryCall()100 public void unaryCall() { 101 Channel channel = cleanupRule.register( 102 ManagedChannelBuilder.forAddress(HOST, port).usePlaintext().build()); 103 SimpleResponse response = TestServiceGrpc.newBlockingStub(channel).unaryCall( 104 SimpleRequest.newBuilder() 105 .setResponseSize(1234) 106 .setPayload(Payload.newBuilder().setBody(ByteString.copyFromUtf8("hello foo"))) 107 .build()); 108 assertThat(response.getPayload().getBody().size()).isEqualTo(1234); 109 } 110 111 @Test httpGetRequest()112 public void httpGetRequest() throws Exception { 113 HttpClient httpClient = new HttpClient(); 114 try { 115 httpClient.start(); 116 ContentResponse response = 117 httpClient.GET("http://" + HOST + ":" + port + MYAPP + "/UnaryCall"); 118 assertThat(response.getStatus()).isEqualTo(405); 119 assertThat(response.getContentAsString()).contains("GET method not supported"); 120 } finally { 121 httpClient.stop(); 122 } 123 } 124 } 125