com.github.tomakehurst.wiremock.extension.responsetemplating.RequestTemplateModel Maven / Gradle / Ivy
package com.github.tomakehurst.wiremock.extension.responsetemplating;
import com.github.tomakehurst.wiremock.common.Urls;
import com.github.tomakehurst.wiremock.http.Cookie;
import com.github.tomakehurst.wiremock.http.MultiValue;
import com.github.tomakehurst.wiremock.http.QueryParameter;
import com.github.tomakehurst.wiremock.http.Request;
import com.google.common.base.Function;
import com.google.common.collect.Maps;
import java.net.URI;
import java.util.Map;
public class RequestTemplateModel {
private final String url;
private final UrlPath path;
private final Map> query;
private final Map> headers;
private final Map> cookies;
private final String body;
public RequestTemplateModel(String url, UrlPath path, Map> query, Map> headers, Map> cookies, String body) {
this.url = url;
this.path = path;
this.query = query;
this.headers = headers;
this.cookies = cookies;
this.body = body;
}
public static RequestTemplateModel from(final Request request) {
URI url = URI.create(request.getUrl());
Map rawQuery = Urls.splitQuery(url);
Map> adaptedQuery = Maps.transformValues(rawQuery, TO_TEMPLATE_MODEL);
Map> adaptedHeaders = Maps.toMap(request.getAllHeaderKeys(), new Function>() {
@Override
public ListOrSingle apply(String input) {
return ListOrSingle.of(request.header(input).values());
}
});
Map> adaptedCookies = Maps.transformValues(request.getCookies(), new Function>() {
@Override
public ListOrSingle apply(Cookie input) {
return ListOrSingle.of(input.getValue());
}
});
UrlPath path = new UrlPath(request.getUrl());
return new RequestTemplateModel(
request.getUrl(),
path,
adaptedQuery,
adaptedHeaders,
adaptedCookies,
request.getBodyAsString()
);
}
public String getUrl() {
return url;
}
public UrlPath getPath() {
return path;
}
public Map> getQuery() {
return query;
}
public Map> getHeaders() {
return headers;
}
public Map> getCookies() {
return cookies;
}
public String getBody() {
return body;
}
private static final Function> TO_TEMPLATE_MODEL = new Function>() {
@Override
public ListOrSingle apply(MultiValue input) {
return ListOrSingle.of(input.values());
}
};
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy