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 org.junit.rules.ExternalResource;

import com.github.tonivade.zeromock.core.HttpService;
import com.github.tonivade.zeromock.core.HttpService.MappingBuilder;
import com.github.tonivade.zeromock.core.Matcher;
import com.github.tonivade.zeromock.core.RequestHandler;
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(Matcher matcher) {
    server.verify(matcher);
    return this;
  }

  public MockHttpServerRule add(Matcher matcher, RequestHandler handler) {
    server.add(matcher, handler);
    return this;
  }

  public MappingBuilder when(Matcher 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