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.5.4
Show newest version
/*
 * Copyright (C) 2016-2023 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.url.PathTemplate;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.RequestMethod;
import com.google.common.collect.Maps;
import java.util.Map;
import java.util.TreeMap;

public class RequestTemplateModel {

  private final RequestLine requestLine;
  private final Map> headers;
  private final Map> cookies;
  private final String body;

  protected RequestTemplateModel(
      RequestLine requestLine,
      Map> headers,
      Map> cookies,
      String body) {
    this.requestLine = requestLine;
    this.headers = headers;
    this.cookies = cookies;
    this.body = body;
  }

  public static RequestTemplateModel from(final Request request) {
    return from(request, null);
  }

  public static RequestTemplateModel from(final Request request, final PathTemplate pathTemplate) {
    RequestLine requestLine = RequestLine.fromRequest(request, pathTemplate);
    Map> adaptedHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    adaptedHeaders.putAll(
        Maps.toMap(
            request.getAllHeaderKeys(), input -> ListOrSingle.of(request.header(input).values())));
    Map> adaptedCookies =
        Maps.transformValues(request.getCookies(), cookie -> ListOrSingle.of(cookie.getValues()));

    return new RequestTemplateModel(
        requestLine, adaptedHeaders, adaptedCookies, request.getBodyAsString());
  }

  public RequestLine getRequestLine() {
    return requestLine;
  }

  public RequestMethod getMethod() {
    return requestLine.getMethod();
  }

  public Object getPathSegments() {
    return requestLine.getPathSegments();
  }

  public Object getPath() {
    return requestLine.getPathSegments();
  }

  public String getUrl() {
    return requestLine.getUrl();
  }

  public Map> getQuery() {
    return requestLine.getQuery();
  }

  public String getScheme() {
    return requestLine.getScheme();
  }

  public String getHost() {
    return requestLine.getHost();
  }

  public int getPort() {
    return requestLine.getPort();
  }

  public String getBaseUrl() {
    return requestLine.getBaseUrl();
  }

  public Map> getHeaders() {
    return headers;
  }

  public Map> getCookies() {
    return cookies;
  }

  public String getBody() {
    return body;
  }

  public String getClientIp() {
    return requestLine.getClientIp();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy