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

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

There is a newer version: 1.0
Show newest version
/*
 * Copyright (c) 2018, Antonio Gabriel Muñoz Conejo 
 * Distributed under the terms of the MIT License
 */
package com.github.tonivade.zeromock.junit4;

import java.util.function.Function;
import java.util.function.Predicate;

import org.junit.rules.ExternalResource;

import com.github.tonivade.zeromock.core.HttpRequest;
import com.github.tonivade.zeromock.core.HttpResponse;
import com.github.tonivade.zeromock.core.HttpService;
import com.github.tonivade.zeromock.core.HttpService.MappingBuilder;
import com.github.tonivade.zeromock.server.MockHttpServer;

public class MockHttpServerRule extends ExternalResource {
  private final MockHttpServer server;

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

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

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

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

  public MockHttpServerRule add(Predicate matcher, 
                                Function handler) {
    server.add(matcher, handler);
    return this;
  }

  public MappingBuilder when(Predicate matcher) {
    return new MappingBuilder<>(this::add).when(matcher);
  }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy