All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.tomakehurst.wiremock.extension.responsetemplating.RequestTemplateModel Maven / Gradle / Ivy

There is a newer version: 3.0.1
Show newest version
/*
 * Copyright (C) 2011 Thomas Akehurst
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.tomakehurst.wiremock.extension.responsetemplating;

import com.github.tomakehurst.wiremock.common.ListOrSingle;
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 cookie) {
                return ListOrSingle.of(cookie.getValues());
            }
        });

        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