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 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;
}
}