/** * Set a object mapper that'll be used when serializing and deserializing Java objects to and from it's * document representation (XML, JSON etc). * * @param objectMapper The object mapper to use. */ public static void objectMapper(ObjectMapper objectMapper) { Validate.notNull(objectMapper, "Default object mapper cannot be null"); config = config().objectMapperConfig(objectMapperConfig().defaultObjectMapper(objectMapper)); }
/** * Specify a trust store that'll be used for HTTPS requests. A trust store is a {@link java.security.KeyStore} that has been loaded with the password. * If you wish that REST Assured loads the KeyStore store and applies the password (thus making it a trust store) please see some of the * <code>keystore</code> methods such as {@link #keystore(java.io.File, String)}. * * @param truststore A pre-loaded {@link java.security.KeyStore}. * @see #keystore(String, String) */ public static void trustStore(KeyStore truststore) { Validate.notNull(truststore, "Truststore cannot be null"); config = config().sslConfig(sslConfig().trustStore(truststore)); }
public RequestSpecBuilder() { this.spec = (RequestSpecificationImpl) new RequestSpecificationImpl(baseURI, port, basePath, authentication, Collections.<Filter>emptyList(), null, urlEncodingEnabled, null, new LogRepository(), proxy).config(RestAssured.config()); }
private static void applyKeyStore(Object pathToJks, String password) { RestAssuredConfig restAssuredConfig = config(); final SSLConfig updatedSSLConfig; if (pathToJks instanceof File) { updatedSSLConfig = restAssuredConfig.getSSLConfig().keystore((File) pathToJks, password); } else { updatedSSLConfig = restAssuredConfig.getSSLConfig().keystore((String) pathToJks, password); } config = config().sslConfig(updatedSSLConfig.allowAllHostnames()); // Allow all host names to be backward-compatible }
private static void applyTrustStore(Object pathToJks, String password) { RestAssuredConfig restAssuredConfig = config(); final SSLConfig updatedSSLConfig; if (pathToJks instanceof File) { updatedSSLConfig = restAssuredConfig.getSSLConfig().trustStore((File) pathToJks, password); } else { updatedSSLConfig = restAssuredConfig.getSSLConfig().trustStore((String) pathToJks, password); } config = config().sslConfig(updatedSSLConfig.allowAllHostnames()); // Allow all host names to be backward-compatible }
/** * Use relaxed HTTP validation with a specific protocol. This means that you'll trust all hosts regardless if the SSL certificate is invalid. By using this * method you don't need to specify a keystore (see {@link #keystore(String, String)} or trust store (see {@link #trustStore(java.security.KeyStore)}. * <p> * This is just a shortcut for: * </p> * <pre> * RestAssured.config = RestAssured.config().sslConfig(sslConfig().relaxedHTTPSValidation(<protocol>)); * </pre> * * @param protocol The standard name of the requested protocol. See the SSLContext section in the <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#SSLContext">Java Cryptography Architecture Standard Algorithm Name Documentation</a> for information about standard protocol names. */ public static void useRelaxedHTTPSValidation(String protocol) { config = RestAssured.config().sslConfig(sslConfig().relaxedHTTPSValidation(protocol)); }
private static TestSpecificationImpl createTestSpecification() { if (defaultParser != null) { RESPONSE_PARSER_REGISTRAR.registerDefaultParser(defaultParser); } final ResponseParserRegistrar responseParserRegistrar = new ResponseParserRegistrar(RESPONSE_PARSER_REGISTRAR); applySessionIdIfApplicable(); LogRepository logRepository = new LogRepository(); RestAssuredConfig restAssuredConfig = config(); return new TestSpecificationImpl( new RequestSpecificationImpl(baseURI, port, basePath, authentication, filters, requestSpecification, urlEncodingEnabled, restAssuredConfig, logRepository, proxy), new ResponseSpecificationImpl(rootPath, responseSpecification, responseParserRegistrar, restAssuredConfig, logRepository) ); }
/** * Enable logging of both the request and the response if REST Assureds test validation fails with the specified log detail. * <p/> * <p> * This is just a shortcut for: * </p> * <pre> * RestAssured.config = RestAssured.config().logConfig(logConfig().enableLoggingOfRequestAndResponseIfValidationFails(logDetail)); * </pre> * * @param logDetail The log detail to show in the log */ public static void enableLoggingOfRequestAndResponseIfValidationFails(LogDetail logDetail) { LogConfig logConfig = logConfig().enableLoggingOfRequestAndResponseIfValidationFails(logDetail); config = RestAssured.config().logConfig(logConfig); // Update request specification if already defined otherwise it'll override the configs. // Note that request spec also influence response spec when it comes to logging if validation fails due to the way filters work if (requestSpecification != null && requestSpecification instanceof RequestSpecificationImpl) { RestAssuredConfig restAssuredConfig = ((RequestSpecificationImpl) requestSpecification).getConfig(); if (restAssuredConfig == null) { restAssuredConfig = config; } else { LogConfig logConfigForRequestSpec = restAssuredConfig.getLogConfig().enableLoggingOfRequestAndResponseIfValidationFails(logDetail); restAssuredConfig = restAssuredConfig.logConfig(logConfigForRequestSpec); } requestSpecification.config(restAssuredConfig); } }
/** * Sets a certificate to be used for SSL authentication. See {@link java.lang.Class#getResource(String)} * for how to get a URL from a resource on the classpath. * <p> * Uses SSL settings defined in {@link com.jayway.restassured.config.SSLConfig}. * </p> * * @param certURL URL to a JKS keystore where the certificate is stored. * @param password The password for the keystore * @return The request com.jayway.restassured.specification */ public static AuthenticationScheme certificate(String certURL, String password) { SSLConfig sslConfig = config().getSSLConfig(); return certificate(certURL, password, certAuthSettings().keystoreType(sslConfig.getKeyStoreType()).trustStore(sslConfig.getTrustStore()). keyStore(sslConfig.getKeyStore()).trustStoreType(sslConfig.getTrustStoreType()).x509HostnameVerifier(sslConfig.getX509HostnameVerifier()). port(sslConfig.getPort()).sslSocketFactory(sslConfig.getSSLSocketFactory())); }
/** * 构造方法 */ HttpUtils(String url) { baseURL = url; // 根据需要进行设置 restAssuredConfig = config() .jsonConfig(jsonConfig().numberReturnType(DOUBLE)); }
public Rest() { spec = given().config(RestAssured.config() .headerConfig(headerConfig() .overwriteHeadersWithName("Authorization") .overwriteHeadersWithName("Content-Type"))); }
@Override protected RequestSpecification buildBaseRequestSpecification() { RequestSpecification spec = RestAssured.given(); if (auth != null) { spec.auth().preemptive().basic(auth.get("username"), auth.get("password")); } if (httpLog != null) { spec.config(RestAssured .config() .logConfig(logConfig() .enableLoggingOfRequestAndResponseIfValidationFails() .defaultStream(new PrintStream(new WriterOutputStream(httpLog))) .enablePrettyPrinting(true))); } return spec; }
public boolean isServerStarted() { try { RestAssuredConfig config = RestAssured.config().httpClient(RestAssuredConfig.config().getHttpClientConfig().setParam("CONNECTION_MANAGER_TIMEOUT", 1000)); return RestAssured.given().config(config).baseUri(getBaseUri()).basePath("/mockserver").get("/").statusCode() == 200; } catch (Exception e) { return false; } }