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

io.restassured.internal.csrf.CsrfInputFieldFinder.groovy Maven / Gradle / Ivy

There is a newer version: 5.5.0
Show newest version
package io.restassured.internal.csrf

import io.restassured.config.CsrfConfig
import io.restassured.response.Response

import static java.lang.String.format

class CsrfInputFieldFinder {
  private static final String FIND_INPUT_TAG_WITH_TYPE = "html.depthFirst().grep { it.name() == 'input' && it.@type == '%s' }.collect { it.@name }"
  private static final String FIND_INPUT_FIELD_WITH_NAME = "html.depthFirst().grep { it.name() == 'input' && it.@name == '%s' }.collect { it.@value }.get(0)"

  static CsrfInputField findInHtml(CsrfConfig csrfConfig, Response pageThatContainsCsrfToken) {
    def htmlPath = pageThatContainsCsrfToken.htmlPath()
    String csrfFieldName = csrfConfig.hasCsrfInputFieldName() ? csrfConfig.csrfInputFieldName : nullIfException {
      htmlPath.getString(format(FIND_INPUT_TAG_WITH_TYPE, "hidden"))
    }
    String csrfValue = nullIfException { htmlPath.getString(format(FIND_INPUT_FIELD_WITH_NAME, csrfFieldName)) }
    if (csrfFieldName == null || csrfValue == null) {
      return null
    }
    return new CsrfInputField(csrfFieldName, csrfValue)
  }

  private static def nullIfException(Closure closure) {
    try {
      closure.call()
    } catch (Exception e) {
      null
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy