com.github.tonivade.zeromock.api.Matchers 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.api;
import static com.github.tonivade.zeromock.api.Bytes.asString;
import static com.github.tonivade.zeromock.api.HttpMethod.DELETE;
import static com.github.tonivade.zeromock.api.HttpMethod.GET;
import static com.github.tonivade.zeromock.api.HttpMethod.HEAD;
import static com.github.tonivade.zeromock.api.HttpMethod.OPTIONS;
import static com.github.tonivade.zeromock.api.HttpMethod.PATCH;
import static com.github.tonivade.zeromock.api.HttpMethod.POST;
import static com.github.tonivade.zeromock.api.HttpMethod.PUT;
import com.github.tonivade.purefun.core.Matcher1;
import com.github.tonivade.purefun.data.ImmutableSet;
public final class Matchers {
private Matchers() {}
public static Matcher1 all() {
return request -> true;
}
public static Matcher1 method(HttpMethod method) {
return request -> request.method().equals(method);
}
public static Matcher1 path(String url) {
return request -> request.path().match(HttpPath.from(url));
}
public static Matcher1 startsWith(String url) {
return request -> request.path().startsWith(HttpPath.from(url));
}
public static Matcher1 param(String name) {
return request -> request.params().contains(name);
}
public static Matcher1 param(String name, String value) {
return request -> request.params().get(name).map(value::equals).getOrElse(false);
}
public static Matcher1 header(String key) {
return request -> request.headers().contains(key);
}
public static Matcher1 header(String key, String value) {
return header(key, values -> values.contains(value));
}
public static Matcher1 header(String key, Matcher1> matcher) {
return header(key).and(request -> matcher.match(request.headers().get(key)));
}
public static Matcher1 get() {
return method(GET);
}
public static Matcher1 put() {
return method(PUT);
}
public static Matcher1 post() {
return method(POST);
}
public static Matcher1 delete() {
return method(DELETE);
}
public static Matcher1 patch() {
return method(PATCH);
}
public static Matcher1 head() {
return method(HEAD);
}
public static Matcher1 options() {
return method(OPTIONS);
}
public static Matcher1 equalTo(T value) {
return Extractors.jsonTo(value.getClass()).andThen(value::equals)::apply;
}
public static Matcher1 jsonPath(String jsonPath, Matcher1 matcher) {
return Extractors.extract(jsonPath).andThen(matcher::match)::apply;
}
public static Matcher1 body(String body) {
return request -> asString(request.body()).equals(body);
}
public static Matcher1 accept(String contentType) {
return header("Accept", contentType);
}
public static Matcher1 acceptsXml() {
return accept("text/xml");
}
public static Matcher1 acceptsJson() {
return accept("application/json");
}
public static Matcher1 get(String path) {
return get().and(path(path));
}
public static Matcher1 put(String path) {
return put().and(path(path));
}
public static Matcher1 post(String path) {
return post().and(path(path));
}
public static Matcher1 patch(String path) {
return patch().and(path(path));
}
public static Matcher1 delete(String path) {
return delete().and(path(path));
}
public static Matcher1 head(String path) {
return head().and(path(path));
}
public static Matcher1 options(String path) {
return options().and(path(path));
}
}