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

restdocs.tool.export.common.variable.HeaderVariablePreprocessor Maven / Gradle / Ivy

Go to download

Generates AsciiDoc snippets via Spring Restdocs that are exports for Insomnia or Postman that can be download and imported.

The newest version!
package restdocs.tool.export.common.variable;

import org.springframework.http.HttpHeaders;
import org.springframework.restdocs.operation.OperationRequest;
import org.springframework.restdocs.operation.OperationRequestFactory;
import org.springframework.restdocs.operation.preprocess.OperationPreprocessorAdapter;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.List;

public class HeaderVariablePreprocessor extends OperationPreprocessorAdapter {

  private final OperationRequestFactory requestFactory = new OperationRequestFactory();

  List headers;
  public HeaderVariablePreprocessor(String... header) {
    Assert.notEmpty(header, "At least one value must be provided");
    headers = Arrays.asList(header);
  }

  @Override
  public OperationRequest preprocess(OperationRequest request) {
    return this.requestFactory.createFrom(request, processHeaders(request.getHeaders()));
  }

  protected HttpHeaders processHeaders(HttpHeaders httpHeaders) {
    HttpHeaders modifiedHeaders = new HttpHeaders();
    modifiedHeaders.putAll(httpHeaders);
    for (String header : headers) {
      modifiedHeaders.forEach((k, v) -> {
        if (k.equalsIgnoreCase(header)) {
          modifiedHeaders.set(k, "<<" + header + VariableKeys.HEADER + ">>");
        }
      });
    }
    return modifiedHeaders;
  }

  public static HeaderVariablePreprocessor replaceHeaderValueWithVariable(String... header) {
    return new HeaderVariablePreprocessor(header);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy