Search in sources :

Example 11 with HttpServerRequest

use of io.vertx.core.http.HttpServerRequest in project yyl_example by Relucent.

the class HelloVertxVerticle method start.

/** 运行 */
@Override
public void start() throws Exception {
    Router router = Router.router(vertx);
    router.route().handler(BodyHandler.create());
    router.get("/hello").handler(new Handler<RoutingContext>() {

        public void handle(RoutingContext event) {
            event.response().putHeader("content-type", "text/html").end("Hello Vert.x");
        }
    });
    vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {

        public void handle(HttpServerRequest event) {
            router.accept(event);
        }
    }).listen(//监听端口号
    8080);
}
Also used : RoutingContext(io.vertx.ext.web.RoutingContext) HttpServerRequest(io.vertx.core.http.HttpServerRequest) Router(io.vertx.ext.web.Router) Handler(io.vertx.core.Handler) BodyHandler(io.vertx.ext.web.handler.BodyHandler)

Example 12 with HttpServerRequest

use of io.vertx.core.http.HttpServerRequest in project java-chassis by ServiceComb.

the class TestSimpleBodyHandler method setUp.

@Before
public void setUp() throws Exception {
    context = Mockito.mock(RoutingContext.class);
    HttpServerRequest request = Mockito.mock(HttpServerRequest.class);
    Mockito.when(context.request()).thenReturn(request);
    HttpServerResponse response = Mockito.mock(HttpServerResponse.class);
    Mockito.when(response.setStatusCode(Status.UNSUPPORTED_MEDIA_TYPE.getStatusCode())).thenReturn(response);
    Mockito.when(context.response()).thenReturn(response);
}
Also used : RoutingContext(io.vertx.ext.web.RoutingContext) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpServerResponse(io.vertx.core.http.HttpServerResponse) Before(org.junit.Before)

Example 13 with HttpServerRequest

use of io.vertx.core.http.HttpServerRequest in project java-chassis by ServiceComb.

the class TestGrpcCodec method testDecodeResponse.

@Test
public void testDecodeResponse() {
    boolean status = false;
    try {
        RoutingContext routingContext = Mockito.mock(RoutingContext.class);
        OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
        OperationProtobuf operationProtobuf = Mockito.mock(OperationProtobuf.class);
        WrapSchema schema = ProtobufSchemaUtils.getOrCreateSchema(int.class);
        Mockito.when(operationProtobuf.getRequestSchema()).thenReturn(schema);
        Mockito.when(operationMeta.getExtData("protobuf")).thenReturn(operationProtobuf);
        Buffer bodyBuffer = Mockito.mock(Buffer.class);
        Mockito.when(routingContext.getBody()).thenReturn(bodyBuffer);
        HttpServerRequest request = Mockito.mock(HttpServerRequest.class);
        Mockito.when(routingContext.request()).thenReturn(request);
        Mockito.when(request.getHeader(Const.CSE_CONTEXT)).thenReturn("{\"name\":\"test\"}");
        Invocation invocation = Mockito.mock(Invocation.class);
        Mockito.when(operationProtobuf.getResponseSchema()).thenReturn(Mockito.mock(WrapSchema.class));
        HttpClientResponse httpResponse = Mockito.mock(HttpClientResponse.class);
        Buffer buffer = Mockito.mock(Buffer.class);
        GrpcCodec.decodeResponse(invocation, operationProtobuf, httpResponse, buffer);
    } catch (Exception e) {
        status = true;
    }
    Assert.assertFalse(status);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) RoutingContext(io.vertx.ext.web.RoutingContext) Invocation(io.servicecomb.core.Invocation) OperationProtobuf(io.servicecomb.codec.protobuf.definition.OperationProtobuf) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) OperationMeta(io.servicecomb.core.definition.OperationMeta) WrapSchema(io.servicecomb.codec.protobuf.utils.WrapSchema) Test(org.junit.Test)

Example 14 with HttpServerRequest

use of io.vertx.core.http.HttpServerRequest in project java-chassis by ServiceComb.

the class TestGrpcCodec method testDecodeRequest.

@Test
public void testDecodeRequest() {
    boolean status = false;
    try {
        RoutingContext routingContext = Mockito.mock(RoutingContext.class);
        OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
        OperationProtobuf operationProtobuf = Mockito.mock(OperationProtobuf.class);
        WrapSchema schema = ProtobufSchemaUtils.getOrCreateSchema(int.class);
        Mockito.when(operationProtobuf.getRequestSchema()).thenReturn(schema);
        Mockito.when(operationMeta.getExtData("protobuf")).thenReturn(operationProtobuf);
        Buffer bodyBuffer = Mockito.mock(Buffer.class);
        Mockito.when(routingContext.getBody()).thenReturn(bodyBuffer);
        HttpServerRequest request = Mockito.mock(HttpServerRequest.class);
        Mockito.when(routingContext.request()).thenReturn(request);
        SchemaMeta schemaMeta = Mockito.mock(SchemaMeta.class);
        Mockito.when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
        Mockito.when(request.getHeader(Const.CSE_CONTEXT)).thenReturn("{\"name\":\"test\"}");
        GrpcCodec.setGrpcTransport(new GrpcTransport());
        GrpcCodec.decodeRequest(routingContext, operationMeta);
    } catch (Exception e) {
        status = true;
    }
    Assert.assertFalse(status);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) RoutingContext(io.vertx.ext.web.RoutingContext) OperationProtobuf(io.servicecomb.codec.protobuf.definition.OperationProtobuf) HttpServerRequest(io.vertx.core.http.HttpServerRequest) SchemaMeta(io.servicecomb.core.definition.SchemaMeta) OperationMeta(io.servicecomb.core.definition.OperationMeta) WrapSchema(io.servicecomb.codec.protobuf.utils.WrapSchema) Test(org.junit.Test)

Example 15 with HttpServerRequest

use of io.vertx.core.http.HttpServerRequest in project java-chassis by ServiceComb.

the class RestBodyHandler method handle.

@Override
public void handle(RoutingContext context) {
    HttpServerRequest request = context.request();
    // we need to keep state since we can be called again on reroute
    Boolean handled = context.get(BODY_HANDLED);
    if (handled == null || !handled) {
        BHandler handler = new BHandler(context);
        request.handler(handler);
        request.endHandler(v -> handler.end());
        context.put(BODY_HANDLED, true);
    } else {
        // on reroute we need to re-merge the form params if that was desired
        if (mergeFormAttributes && request.isExpectMultipart()) {
            request.params().addAll(request.formAttributes());
        }
        context.next();
    }
}
Also used : HttpServerRequest(io.vertx.core.http.HttpServerRequest)

Aggregations

HttpServerRequest (io.vertx.core.http.HttpServerRequest)31 Test (org.junit.Test)27 MultiMap (io.vertx.core.MultiMap)16 IOException (java.io.IOException)16 Buffer (io.vertx.core.buffer.Buffer)14 ArrayList (java.util.ArrayList)14 HttpServerOptions (io.vertx.core.http.HttpServerOptions)13 HashSet (java.util.HashSet)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 Bootstrap (io.netty.bootstrap.Bootstrap)10 ByteBuf (io.netty.buffer.ByteBuf)10 Unpooled (io.netty.buffer.Unpooled)10 Channel (io.netty.channel.Channel)10 ChannelDuplexHandler (io.netty.channel.ChannelDuplexHandler)10 ChannelFuture (io.netty.channel.ChannelFuture)10 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)10 ChannelInitializer (io.netty.channel.ChannelInitializer)10 ChannelPipeline (io.netty.channel.ChannelPipeline)10 EventLoopGroup (io.netty.channel.EventLoopGroup)10 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)10