All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.tonivade.zeromock.junit4.MockHttpServerRule Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2018-2024, Antonio Gabriel Muñoz Conejo 
 * Distributed under the terms of the MIT License
 */
package com.github.tonivade.zeromock.junit4;

import org.junit.rules.ExternalResource;
import com.github.tonivade.purefun.core.Matcher1;
import com.github.tonivade.zeromock.api.HttpRequest;
import com.github.tonivade.zeromock.api.HttpRouteBuilder;
import com.github.tonivade.zeromock.api.HttpService;
import com.github.tonivade.zeromock.api.RequestHandler;
import com.github.tonivade.zeromock.client.AsyncHttpClient;
import com.github.tonivade.zeromock.client.HttpClient;
import com.github.tonivade.zeromock.client.HttpClientBuilder;
import com.github.tonivade.zeromock.client.IOHttpClient;
import com.github.tonivade.zeromock.client.TaskHttpClient;
import com.github.tonivade.zeromock.client.UIOHttpClient;
import com.github.tonivade.zeromock.server.MockHttpServer;

public class MockHttpServerRule extends ExternalResource implements HttpRouteBuilder {

  private final MockHttpServer server;

  public MockHttpServerRule() {
     this(0);
  }

  public MockHttpServerRule(int port) {
    server = MockHttpServer.listenAt(port);
  }

  @Override
  protected void before() {
    server.start();
  }

  @Override
  protected void after() {
    server.stop();
  }

  public String getBaseUrl() {
    return "http://localhost:" + server.getPort();
  }

  public HttpClient client() {
    return HttpClientBuilder.client().connectTo(getBaseUrl());
  }

  public AsyncHttpClient asyncClient() {
    return HttpClientBuilder.asyncClient().connectTo(getBaseUrl());
  }

  public IOHttpClient ioClient() {
    return HttpClientBuilder.ioClient().connectTo(getBaseUrl());
  }

  public UIOHttpClient uioClient() {
    return HttpClientBuilder.uioClient().connectTo(getBaseUrl());
  }

  public TaskHttpClient taskClient() {
    return HttpClientBuilder.taskClient().connectTo(getBaseUrl());
  }

  public MockHttpServerRule verify(Matcher1 matcher) {
    server.verify(matcher);
    return this;
  }

  public MockHttpServerRule verifyNot(Matcher1 matcher) {
    server.verifyNot(matcher);
    return this;
  }

  public MockHttpServerRule addMapping(Matcher1 matcher, RequestHandler handler) {
    server.when(matcher).then(handler);
    return this;
  }

  @Override
  public ThenStep when(Matcher1 matcher) {
    return handler -> addMapping(matcher, handler);
  }

  public MockHttpServerRule mount(String path, HttpService service) {
    server.mount(path, service);
    return this;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy