com.github.tonivade.zeromock.junit4.AbstractMockServerRule Maven / Gradle / Ivy
/*
* Copyright (c) 2018-2024, Antonio Gabriel Muñoz Conejo
* Distributed under the terms of the MIT License
*/
package com.github.tonivade.zeromock.junit4;
import static com.github.tonivade.purefun.core.Precondition.checkNonNull;
import org.junit.rules.ExternalResource;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.core.Matcher1;
import com.github.tonivade.purefun.typeclasses.Monad;
import com.github.tonivade.zeromock.api.HttpRequest;
import com.github.tonivade.zeromock.api.HttpRouteBuilderK;
import com.github.tonivade.zeromock.api.HttpServiceK;
import com.github.tonivade.zeromock.api.RequestHandlerK;
import com.github.tonivade.zeromock.client.HttpClient;
import com.github.tonivade.zeromock.server.MockHttpServerK;
public abstract class AbstractMockServerRule> extends ExternalResource implements HttpRouteBuilderK> {
private final Monad monad;
private final MockHttpServerK server;
protected AbstractMockServerRule(Monad monad, MockHttpServerK server) {
this.monad = checkNonNull(monad);
this.server = checkNonNull(server);
}
@Override
protected void before() {
server.start();
}
@Override
protected void after() {
server.stop();
}
public HttpClient client() {
return HttpClient.connectTo("http://localhost:" + server.getPort());
}
public AbstractMockServerRule verify(Matcher1 matcher) {
server.verify(matcher);
return this;
}
public AbstractMockServerRule verifyNot(Matcher1 matcher) {
server.verifyNot(matcher);
return this;
}
public AbstractMockServerRule addMapping(Matcher1 matcher, RequestHandlerK handler) {
server.when(matcher).then(handler);
return this;
}
@Override
public ThenStepK> when(Matcher1 matcher) {
return new ThenStepK<>(monad, handler -> addMapping(matcher, handler));
}
public AbstractMockServerRule mount(String path, HttpServiceK service) {
server.mount(path, service);
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy