com.github.tonivade.zeromock.junit4.MockHttpServerRule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zeromock-junit4 Show documentation
Show all versions of zeromock-junit4 Show documentation
Mock Http Server (mostly) without dependencies
/*
* 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;
}
}